Skip to content

Commit

Permalink
fix: correctly pass locals from Netlify edge middleware (#488)
Browse files Browse the repository at this point in the history
* fix: correctly pass locals from Netlify edge middleware

* Format

* Remove console
  • Loading branch information
ascorbic authored Dec 16, 2024
1 parent 9d98b8a commit f3739be
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 8 deletions.
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
4 changes: 2 additions & 2 deletions packages/netlify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ export default function netlifyIntegration(
export default async (request, context) => {
const ctx = createContext({
request,
params: {}
params: {},
locals: { netlify: { context } }
});
ctx.locals.netlify = { context }
// https://docs.netlify.com/edge-functions/api/#return-a-rewrite
ctx.rewrite = (target) => {
if(target instanceof Request) {
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.

0 comments on commit f3739be

Please sign in to comment.