Skip to content

Commit

Permalink
Use Deno API to set Astro.clientAddress (#6651)
Browse files Browse the repository at this point in the history
* Use Deno API to set Astro.clientAddress

* Add types
  • Loading branch information
matthewp authored Apr 6, 2023
1 parent 9e88e0f commit 416ceb9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-avocados-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
---

Use Deno API to set Astro.clientAddress in Netlify Edge
1 change: 1 addition & 0 deletions packages/integrations/netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"astro": "workspace:^2.2.0"
},
"devDependencies": {
"@netlify/edge-functions": "^2.0.0",
"@netlify/edge-handler-types": "^0.34.1",
"@types/node": "^14.18.20",
"astro": "workspace:*",
Expand Down
7 changes: 5 additions & 2 deletions packages/integrations/netlify/src/netlify-edge-functions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { SSRManifest } from 'astro';
import type { Context } from '@netlify/edge-functions';
import { App } from 'astro/app';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

export function createExports(manifest: SSRManifest) {
const app = new App(manifest);

const handler = async (request: Request): Promise<Response | void> => {
const handler = async (request: Request, context: Context): Promise<Response | void> => {
const url = new URL(request.url);

// If this matches a static asset, just return and Netlify will forward it
Expand All @@ -15,7 +16,9 @@ export function createExports(manifest: SSRManifest) {
return;
}
if (app.match(request)) {
const ip = request.headers.get('x-nf-client-connection-ip');
const ip = request.headers.get('x-nf-client-connection-ip')
|| context?.ip
|| (context as any)?.remoteAddr?.hostname;
Reflect.set(request, clientAddressSymbol, ip);
const response = await app.render(request);
if (app.setCookieHeaders) {
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 416ceb9

Please sign in to comment.