From f3739bef812aa9659ff9bdd10ba9046ac716a3d5 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 16 Dec 2024 15:25:03 +0000 Subject: [PATCH] fix: correctly pass locals from Netlify edge middleware (#488) * fix: correctly pass locals from Netlify edge middleware * Format * Remove console --- .changeset/violet-laws-wonder.md | 5 + .gitignore | 1 + packages/netlify/src/index.ts | 4 +- .../hosted-astro-project/astro.config.mjs | 4 +- .../hosted/hosted-astro-project/package.json | 2 +- .../hosted-astro-project/src/middleware.ts | 11 ++ .../src/pages/country.astro | 7 ++ packages/netlify/test/hosted/hosted.test.js | 11 +- pnpm-lock.yaml | 114 +++++++++++++++++- 9 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 .changeset/violet-laws-wonder.md create mode 100644 packages/netlify/test/hosted/hosted-astro-project/src/middleware.ts create mode 100644 packages/netlify/test/hosted/hosted-astro-project/src/pages/country.astro diff --git a/.changeset/violet-laws-wonder.md b/.changeset/violet-laws-wonder.md new file mode 100644 index 000000000..4a56c6351 --- /dev/null +++ b/.changeset/violet-laws-wonder.md @@ -0,0 +1,5 @@ +--- +'@astrojs/netlify': patch +--- + +Correctly pass Netlify context in edge middleware diff --git a/.gitignore b/.gitignore index d1de55308..7a016a969 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ package-lock.json .pnpm-store .idea/ **/fixtures/**/.astro +**/hosted/**/.astro # ignore top-level vscode settings /.vscode/settings.json diff --git a/packages/netlify/src/index.ts b/packages/netlify/src/index.ts index dc5c90d75..4e87a5f05 100644 --- a/packages/netlify/src/index.ts +++ b/packages/netlify/src/index.ts @@ -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) { diff --git a/packages/netlify/test/hosted/hosted-astro-project/astro.config.mjs b/packages/netlify/test/hosted/hosted-astro-project/astro.config.mjs index 3d45722eb..94cc00f7b 100644 --- a/packages/netlify/test/hosted/hosted-astro-project/astro.config.mjs +++ b/packages/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/netlify/test/hosted/hosted-astro-project/package.json b/packages/netlify/test/hosted/hosted-astro-project/package.json index ad0fb85bb..8d690b9e5 100644 --- a/packages/netlify/test/hosted/hosted-astro-project/package.json +++ b/packages/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/netlify/test/hosted/hosted-astro-project/src/middleware.ts b/packages/netlify/test/hosted/hosted-astro-project/src/middleware.ts new file mode 100644 index 000000000..4ecc924a5 --- /dev/null +++ b/packages/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/netlify/test/hosted/hosted-astro-project/src/pages/country.astro b/packages/netlify/test/hosted/hosted-astro-project/src/pages/country.astro new file mode 100644 index 000000000..cad7116d6 --- /dev/null +++ b/packages/netlify/test/hosted/hosted-astro-project/src/pages/country.astro @@ -0,0 +1,7 @@ +--- +const country = Astro.locals.middleware; +--- + +

{country}

+

{country ? 'has context' : 'no context'}

+

{Astro.locals.runtime}

diff --git a/packages/netlify/test/hosted/hosted.test.js b/packages/netlify/test/hosted/hosted.test.js index 237ba5998..2c40e3a69 100644 --- a/packages/netlify/test/hosted/hosted.test.js +++ b/packages/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); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 57d7c3337..3bdf9ad7a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -328,8 +328,8 @@ importers: specifier: workspace:* version: link:../../.. astro: - specifier: ^5.0.0 - version: 5.0.0(@types/node@22.10.1)(rollup@4.27.4)(typescript@5.7.2)(yaml@2.6.1) + specifier: ^5.0.5 + version: 5.0.5(@types/node@22.10.1)(rollup@4.27.4)(typescript@5.7.2)(yaml@2.6.1) packages/netlify/test/static/fixtures/redirects: dependencies: @@ -759,6 +759,9 @@ packages: '@astrojs/markdown-remark@6.0.0': resolution: {integrity: sha512-Tabo7xM44Pz2Yf9qpdaCCgxRmtaypi2YCinqTUNefDrWUa+OyKW62OuNeCaGwNh/ys+QAd9FUWN5/3HgPWjP4Q==} + '@astrojs/markdown-remark@6.0.1': + resolution: {integrity: sha512-CTSYijj25NfxgZi15TU3CwPwgyD1/7yA3FcdcNmB9p94nydupiUbrIiq3IqeTp2m5kCVzxbPZeC7fTwEOaNyGw==} + '@astrojs/prism@3.2.0': resolution: {integrity: sha512-GilTHKGCW6HMq7y3BUv9Ac7GMe/MO9gi9GW62GzKtth0SwukCu/qp2wLiGpEujhY+VVhaG9v7kv/5vFzvf4NYw==} engines: {node: ^18.17.1 || ^20.3.0 || >=22.0.0} @@ -2289,6 +2292,11 @@ packages: engines: {node: ^18.17.1 || ^20.3.0 || >=22.0.0, npm: '>=9.6.5', pnpm: '>=7.1.0'} hasBin: true + astro@5.0.5: + resolution: {integrity: sha512-xfptdmurDsQcj/Anc7mU+eKlcyV7ppJIlmaSwhX3ZWwK5N/0rGKVmUqnuILgR6MB0XVJiIfublNzDGoyj4Q6BQ==} + engines: {node: ^18.17.1 || ^20.3.0 || >=22.0.0, npm: '>=9.6.5', pnpm: '>=7.1.0'} + hasBin: true + async-sema@3.1.1: resolution: {integrity: sha512-tLRNUXati5MFePdAk8dw7Qt7DpxPB60ofAgn8WRhW6a2rcimZnYBP9oxHiv0OHy+Wz7kPMG+t4LGdt31+4EmGg==} @@ -4913,6 +4921,30 @@ snapshots: transitivePeerDependencies: - supports-color + '@astrojs/markdown-remark@6.0.1': + dependencies: + '@astrojs/prism': 3.2.0 + github-slugger: 2.0.0 + hast-util-from-html: 2.0.3 + hast-util-to-text: 4.0.2 + import-meta-resolve: 4.1.0 + js-yaml: 4.1.0 + mdast-util-definitions: 6.0.0 + rehype-raw: 7.0.0 + rehype-stringify: 10.0.1 + remark-gfm: 4.0.0 + remark-parse: 11.0.0 + remark-rehype: 11.1.1 + remark-smartypants: 3.0.2 + shiki: 1.24.0 + unified: 11.0.5 + unist-util-remove-position: 5.0.0 + unist-util-visit: 5.0.0 + unist-util-visit-parents: 6.0.1 + vfile: 6.0.3 + transitivePeerDependencies: + - supports-color + '@astrojs/prism@3.2.0': dependencies: prismjs: 1.29.0 @@ -6537,6 +6569,84 @@ snapshots: - typescript - yaml + astro@5.0.5(@types/node@22.10.1)(rollup@4.27.4)(typescript@5.7.2)(yaml@2.6.1): + dependencies: + '@astrojs/compiler': 2.10.3 + '@astrojs/internal-helpers': 0.4.2 + '@astrojs/markdown-remark': 6.0.1 + '@astrojs/telemetry': 3.2.0 + '@oslojs/encoding': 1.1.0 + '@rollup/pluginutils': 5.1.3(rollup@4.27.4) + '@types/cookie': 0.6.0 + acorn: 8.14.0 + aria-query: 5.3.2 + axobject-query: 4.1.0 + boxen: 8.0.1 + ci-info: 4.1.0 + clsx: 2.1.1 + common-ancestor-path: 1.0.1 + cookie: 0.7.2 + cssesc: 3.0.0 + debug: 4.3.7 + deterministic-object-hash: 2.0.2 + devalue: 5.1.1 + diff: 5.2.0 + dlv: 1.1.3 + dset: 3.1.4 + es-module-lexer: 1.5.4 + esbuild: 0.21.5 + estree-walker: 3.0.3 + fast-glob: 3.3.2 + flattie: 1.1.1 + github-slugger: 2.0.0 + html-escaper: 3.0.3 + http-cache-semantics: 4.1.1 + js-yaml: 4.1.0 + kleur: 4.1.5 + magic-string: 0.30.14 + magicast: 0.3.5 + micromatch: 4.0.8 + mrmime: 2.0.0 + neotraverse: 0.6.18 + p-limit: 6.1.0 + p-queue: 8.0.1 + preferred-pm: 4.0.0 + prompts: 2.4.2 + rehype: 13.0.2 + semver: 7.6.3 + shiki: 1.24.0 + tinyexec: 0.3.1 + tsconfck: 3.1.4(typescript@5.7.2) + ultrahtml: 1.5.3 + unist-util-visit: 5.0.0 + vfile: 6.0.3 + vite: 6.0.2(@types/node@22.10.1)(yaml@2.6.1) + vitefu: 1.0.4(vite@6.0.2(@types/node@22.10.1)(yaml@2.6.1)) + which-pm: 3.0.0 + xxhash-wasm: 1.1.0 + yargs-parser: 21.1.1 + yocto-spinner: 0.1.1 + zod: 3.23.8 + zod-to-json-schema: 3.23.5(zod@3.23.8) + zod-to-ts: 1.2.0(typescript@5.7.2)(zod@3.23.8) + optionalDependencies: + sharp: 0.33.5 + transitivePeerDependencies: + - '@types/node' + - jiti + - less + - lightningcss + - rollup + - sass + - sass-embedded + - stylus + - sugarss + - supports-color + - terser + - tsx + - typescript + - yaml + async-sema@3.1.1: {} axobject-query@4.1.0: {}