Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly pass locals from Netlify edge middleware #488

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-laws-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
---

Correctly pass Netlify context in edge middleware
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package-lock.json
.pnpm-store
.idea/
**/fixtures/**/.astro
**/hosted/**/.astro

# ignore top-level vscode settings
/.vscode/settings.json
Expand Down
6 changes: 4 additions & 2 deletions packages/netlify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ export default function netlifyIntegration(
export default async (request, context) => {
const ctx = createContext({
request,
params: {}
params: {},
locals: { netlify: { context } }
});
ctx.locals.netlify = { context }
console.log(ctx.locals)
ascorbic marked this conversation as resolved.
Show resolved Hide resolved
// https://docs.netlify.com/edge-functions/api/#return-a-rewrite
ctx.rewrite = (target) => {
if(target instanceof Request) {
Expand All @@ -313,6 +314,7 @@ export default function netlifyIntegration(
};
const next = () => {
const { netlify, ...otherLocals } = ctx.locals;
console.log({otherLocals})
ascorbic marked this conversation as resolved.
Show resolved Hide resolved
request.headers.set("x-astro-locals", trySerializeLocals(otherLocals));
request.headers.set("x-astro-middleware-secret", "${middlewareSecret}");
return context.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { defineConfig } from 'astro/config';
// https://astro.build/config
export default defineConfig({
output: 'server',
adapter: netlify(),
adapter: netlify({
edgeMiddleware: true,
}),
image: {
remotePatterns: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"dependencies": {
"@astrojs/netlify": "workspace:*",
"astro": "^5.0.0"
"astro": "^5.0.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import https from 'node:https';

export const onRequest = (context, next) => {
console.log(context.netlify);
context.locals.middleware = context?.locals?.netlify?.context?.geo?.country?.code ?? null;
context.locals.runtime = 'Deno' in globalThis ? 'Deno' : 'Node';
context.locals.title = 'Middleware';
context.locals.nodePrefixedImportExists = !!https;

return next();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
const country = Astro.locals.middleware;
---

<h1>{country}</h1>
<h3>{country ? 'has context' : 'no context'}</h3>
<h2>{Astro.locals.runtime}</h2>
11 changes: 9 additions & 2 deletions packages/netlify/test/hosted/hosted.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ describe('Hosted Netlify Tests', () => {
assert.equal(image.status, 200);
});

it('passes context from edge middleware', async () => {
const response = await fetch(`${NETLIFY_TEST_URL}/country`);
const body = await response.text();
assert.match(body, /has context/);
assert.match(body, /Deno/);
});

it('Server returns fresh content', async () => {
const responseOne = await fetch(`${NETLIFY_TEST_URL}/time`);
const responseOne = await fetch(`${NETLIFY_TEST_URL}/time`).then((res) => res.text());

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

assert.notEqual(responseOne.body, responseTwo.body);
});
Expand Down
114 changes: 112 additions & 2 deletions pnpm-lock.yaml

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

Loading