Skip to content

Commit 416ceb9

Browse files
authored
Use Deno API to set Astro.clientAddress (#6651)
* Use Deno API to set Astro.clientAddress * Add types
1 parent 9e88e0f commit 416ceb9

4 files changed

Lines changed: 31 additions & 2 deletions

File tree

.changeset/silver-avocados-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/netlify': patch
3+
---
4+
5+
Use Deno API to set Astro.clientAddress in Netlify Edge

packages/integrations/netlify/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"astro": "workspace:^2.2.0"
4343
},
4444
"devDependencies": {
45+
"@netlify/edge-functions": "^2.0.0",
4546
"@netlify/edge-handler-types": "^0.34.1",
4647
"@types/node": "^14.18.20",
4748
"astro": "workspace:*",

packages/integrations/netlify/src/netlify-edge-functions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { SSRManifest } from 'astro';
2+
import type { Context } from '@netlify/edge-functions';
23
import { App } from 'astro/app';
34

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

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

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

1213
// If this matches a static asset, just return and Netlify will forward it
@@ -15,7 +16,9 @@ export function createExports(manifest: SSRManifest) {
1516
return;
1617
}
1718
if (app.match(request)) {
18-
const ip = request.headers.get('x-nf-client-connection-ip');
19+
const ip = request.headers.get('x-nf-client-connection-ip')
20+
|| context?.ip
21+
|| (context as any)?.remoteAddr?.hostname;
1922
Reflect.set(request, clientAddressSymbol, ip);
2023
const response = await app.render(request);
2124
if (app.setCookieHeaders) {

pnpm-lock.yaml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)