diff options
author | 2024-12-16 15:25:03 +0000 | |
---|---|---|
committer | 2024-12-16 15:25:03 +0000 | |
commit | e3959b946efc476be72ae0dd353ac2ac37256ac6 (patch) | |
tree | 416b6420277bab34ca7f9056e7e87863dfd4509b /packages/integrations/netlify/test | |
parent | 60f8d4a27d82869a482d2c20b7dfeb8e606b6a76 (diff) | |
download | astro-e3959b946efc476be72ae0dd353ac2ac37256ac6.tar.gz astro-e3959b946efc476be72ae0dd353ac2ac37256ac6.tar.zst astro-e3959b946efc476be72ae0dd353ac2ac37256ac6.zip |
fix: correctly pass locals from Netlify edge middleware (#488)
* fix: correctly pass locals from Netlify edge middleware
* Format
* Remove console
Diffstat (limited to 'packages/integrations/netlify/test')
5 files changed, 31 insertions, 4 deletions
diff --git a/packages/integrations/netlify/test/hosted/hosted-astro-project/astro.config.mjs b/packages/integrations/netlify/test/hosted/hosted-astro-project/astro.config.mjs index 3d45722eb..94cc00f7b 100644 --- a/packages/integrations/netlify/test/hosted/hosted-astro-project/astro.config.mjs +++ b/packages/integrations/netlify/test/hosted/hosted-astro-project/astro.config.mjs @@ -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: [ { diff --git a/packages/integrations/netlify/test/hosted/hosted-astro-project/package.json b/packages/integrations/netlify/test/hosted/hosted-astro-project/package.json index ad0fb85bb..8d690b9e5 100644 --- a/packages/integrations/netlify/test/hosted/hosted-astro-project/package.json +++ b/packages/integrations/netlify/test/hosted/hosted-astro-project/package.json @@ -7,6 +7,6 @@ }, "dependencies": { "@astrojs/netlify": "workspace:*", - "astro": "^5.0.0" + "astro": "^5.0.5" } } diff --git a/packages/integrations/netlify/test/hosted/hosted-astro-project/src/middleware.ts b/packages/integrations/netlify/test/hosted/hosted-astro-project/src/middleware.ts new file mode 100644 index 000000000..4ecc924a5 --- /dev/null +++ b/packages/integrations/netlify/test/hosted/hosted-astro-project/src/middleware.ts @@ -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(); +}; diff --git a/packages/integrations/netlify/test/hosted/hosted-astro-project/src/pages/country.astro b/packages/integrations/netlify/test/hosted/hosted-astro-project/src/pages/country.astro new file mode 100644 index 000000000..cad7116d6 --- /dev/null +++ b/packages/integrations/netlify/test/hosted/hosted-astro-project/src/pages/country.astro @@ -0,0 +1,7 @@ +--- +const country = Astro.locals.middleware; +--- + +<h1>{country}</h1> +<h3>{country ? 'has context' : 'no context'}</h3> +<h2>{Astro.locals.runtime}</h2> diff --git a/packages/integrations/netlify/test/hosted/hosted.test.js b/packages/integrations/netlify/test/hosted/hosted.test.js index 237ba5998..2c40e3a69 100644 --- a/packages/integrations/netlify/test/hosted/hosted.test.js +++ b/packages/integrations/netlify/test/hosted/hosted.test.js @@ -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); }); |