Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit f3739be

Browse files
authored
fix: correctly pass locals from Netlify edge middleware (#488)
* fix: correctly pass locals from Netlify edge middleware * Format * Remove console
1 parent 9d98b8a commit f3739be

File tree

9 files changed

+151
-8
lines changed

9 files changed

+151
-8
lines changed

Diff for: .changeset/violet-laws-wonder.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/netlify': patch
3+
---
4+
5+
Correctly pass Netlify context in edge middleware

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package-lock.json
1212
.pnpm-store
1313
.idea/
1414
**/fixtures/**/.astro
15+
**/hosted/**/.astro
1516

1617
# ignore top-level vscode settings
1718
/.vscode/settings.json

Diff for: packages/netlify/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ export default function netlifyIntegration(
289289
export default async (request, context) => {
290290
const ctx = createContext({
291291
request,
292-
params: {}
292+
params: {},
293+
locals: { netlify: { context } }
293294
});
294-
ctx.locals.netlify = { context }
295295
// https://docs.netlify.com/edge-functions/api/#return-a-rewrite
296296
ctx.rewrite = (target) => {
297297
if(target instanceof Request) {

Diff for: packages/netlify/test/hosted/hosted-astro-project/astro.config.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { defineConfig } from 'astro/config';
44
// https://astro.build/config
55
export default defineConfig({
66
output: 'server',
7-
adapter: netlify(),
7+
adapter: netlify({
8+
edgeMiddleware: true,
9+
}),
810
image: {
911
remotePatterns: [
1012
{

Diff for: packages/netlify/test/hosted/hosted-astro-project/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
},
88
"dependencies": {
99
"@astrojs/netlify": "workspace:*",
10-
"astro": "^5.0.0"
10+
"astro": "^5.0.5"
1111
}
1212
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import https from 'node:https';
2+
3+
export const onRequest = (context, next) => {
4+
console.log(context.netlify);
5+
context.locals.middleware = context?.locals?.netlify?.context?.geo?.country?.code ?? null;
6+
context.locals.runtime = 'Deno' in globalThis ? 'Deno' : 'Node';
7+
context.locals.title = 'Middleware';
8+
context.locals.nodePrefixedImportExists = !!https;
9+
10+
return next();
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
const country = Astro.locals.middleware;
3+
---
4+
5+
<h1>{country}</h1>
6+
<h3>{country ? 'has context' : 'no context'}</h3>
7+
<h2>{Astro.locals.runtime}</h2>

Diff for: packages/netlify/test/hosted/hosted.test.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ describe('Hosted Netlify Tests', () => {
1212
assert.equal(image.status, 200);
1313
});
1414

15+
it('passes context from edge middleware', async () => {
16+
const response = await fetch(`${NETLIFY_TEST_URL}/country`);
17+
const body = await response.text();
18+
assert.match(body, /has context/);
19+
assert.match(body, /Deno/);
20+
});
21+
1522
it('Server returns fresh content', async () => {
16-
const responseOne = await fetch(`${NETLIFY_TEST_URL}/time`);
23+
const responseOne = await fetch(`${NETLIFY_TEST_URL}/time`).then((res) => res.text());
1724

18-
const responseTwo = await fetch(`${NETLIFY_TEST_URL}/time`);
25+
const responseTwo = await fetch(`${NETLIFY_TEST_URL}/time`).then((res) => res.text());
1926

2027
assert.notEqual(responseOne.body, responseTwo.body);
2128
});

Diff for: pnpm-lock.yaml

+112-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)