diff --git a/.changeset/tidy-actors-relate.md b/.changeset/tidy-actors-relate.md new file mode 100644 index 000000000..50afa24bb --- /dev/null +++ b/.changeset/tidy-actors-relate.md @@ -0,0 +1,5 @@ +--- +'@astrojs/vercel': patch +--- + +Improve file detection of the middleware file handler diff --git a/packages/integrations/vercel/package.json b/packages/integrations/vercel/package.json index b7beb4917..0ed2656e8 100644 --- a/packages/integrations/vercel/package.json +++ b/packages/integrations/vercel/package.json @@ -47,7 +47,7 @@ "build": "astro-scripts build \"src/**/*.ts\" && tsc", "build:ci": "astro-scripts build \"src/**/*.ts\"", "dev": "astro-scripts dev \"src/**/*.ts\"", - "test": "mocha --exit --timeout 20000 test/" + "test": "mocha --exit --timeout 20000 --file \"./test/setup.js\" test/" }, "dependencies": { "@astrojs/internal-helpers": "^0.1.1", diff --git a/packages/integrations/vercel/src/serverless/middleware.ts b/packages/integrations/vercel/src/serverless/middleware.ts index c28425f4c..742740f64 100644 --- a/packages/integrations/vercel/src/serverless/middleware.ts +++ b/packages/integrations/vercel/src/serverless/middleware.ts @@ -50,7 +50,7 @@ function edgeMiddlewareTemplate(middlewarePath: string, vercelEdgeMiddlewareHand const filePathEdgeMiddleware = fileURLToPath(vercelEdgeMiddlewareHandlerPath); let handlerTemplateImport = ''; let handlerTemplateCall = '{}'; - if (existsSync(filePathEdgeMiddleware) + '.js' || existsSync(filePathEdgeMiddleware) + '.ts') { + if (existsSync(filePathEdgeMiddleware + '.js') || existsSync(filePathEdgeMiddleware + '.ts')) { const stringified = JSON.stringify(filePathEdgeMiddleware.replace(/\\/g, '/')); handlerTemplateImport = `import handler from ${stringified}`; handlerTemplateCall = `handler({ request, context })`; diff --git a/packages/integrations/vercel/test/edge-middleware.test.js b/packages/integrations/vercel/test/edge-middleware.test.js index dd4b25b67..c713c5464 100644 --- a/packages/integrations/vercel/test/edge-middleware.test.js +++ b/packages/integrations/vercel/test/edge-middleware.test.js @@ -1,30 +1,33 @@ import { loadFixture } from './test-utils.js'; -import { expect, use } from 'chai'; +import { expect } from 'chai'; import chaiJestSnapshot from 'chai-jest-snapshot'; -use(chaiJestSnapshot); - -describe('Serverless prerender', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - - beforeEach(function () { - chaiJestSnapshot.configureUsingMochaContext(this); - }); - - before(async () => { - chaiJestSnapshot.resetSnapshotRegistry(); - fixture = await loadFixture({ - root: './fixtures/middleware/', +describe('Vercel edge middleware', () => { + it('with edge handle file, should successfully build the middleware', async () => { + const fixture = await loadFixture({ + root: './fixtures/middleware-with-edge-file/', }); - }); - - it('build successfully the middleware edge file', async () => { await fixture.build(); const contents = await fixture.readFile( // this is abysmal... - '../.vercel/output/functions/render.func/packages/integrations/vercel/test/fixtures/middleware/dist/middleware.mjs' + '../.vercel/output/functions/render.func/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/dist/middleware.mjs' ); - expect(contents).to.matchSnapshot(); + expect(contents.includes('title:')).to.be.true; + chaiJestSnapshot.setTestName('Middleware with handler file'); + expect(contents).to.matchSnapshot(true); + }); + + it('with edge handle file, should successfully build the middleware', async () => { + const fixture = await loadFixture({ + root: './fixtures/middleware-without-edge-file/', + }); + await fixture.build(); + const contents = await fixture.readFile( + // this is abysmal... + '../.vercel/output/functions/render.func/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/dist/middleware.mjs' + ); + expect(contents.includes('title:')).to.be.false; + chaiJestSnapshot.setTestName('Middleware without handler file'); + expect(contents).to.matchSnapshot(true); }); }); diff --git a/packages/integrations/vercel/test/edge-middleware.test.js.snap b/packages/integrations/vercel/test/edge-middleware.test.js.snap index fe82ccff9..754aebc8f 100644 --- a/packages/integrations/vercel/test/edge-middleware.test.js.snap +++ b/packages/integrations/vercel/test/edge-middleware.test.js.snap @@ -1,14 +1,14 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Serverless prerender build successfully the middleware edge file 1`] = ` -"// test/fixtures/middleware/src/vercel-edge-middleware.js +exports[`Middleware with handler file 1`] = ` +"// test/fixtures/middleware-with-edge-file/src/vercel-edge-middleware.js function vercel_edge_middleware_default({ request, context }) { return { title: \\"Hello world\\" }; } -// test/fixtures/middleware/dist/middleware2.mjs +// test/fixtures/middleware-with-edge-file/dist/middleware2.mjs var onRequest = async (context, next) => { const response = await next(); return response; @@ -38,3 +38,35 @@ export { }; " `; + +exports[`Middleware without handler file 1`] = ` +"// test/fixtures/middleware-without-edge-file/dist/middleware2.mjs +var onRequest = async (context, next) => { + const response = await next(); + return response; +}; + +// +import { createContext, trySerializeLocals } from \\"astro/middleware\\"; +async function middleware(request, context) { + const url = new URL(request.url); + const ctx = createContext({ + request, + params: {} + }); + ctx.locals = {}; + const next = async () => { + const response = await fetch(url, { + headers: { + \\"x-astro-locals\\": trySerializeLocals(ctx.locals) + } + }); + return response; + }; + return onRequest(ctx, next); +} +export { + middleware as default +}; +" +`; diff --git a/packages/integrations/vercel/test/fixtures/middleware/astro.config.mjs b/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/astro.config.mjs similarity index 100% rename from packages/integrations/vercel/test/fixtures/middleware/astro.config.mjs rename to packages/integrations/vercel/test/fixtures/middleware-with-edge-file/astro.config.mjs diff --git a/packages/integrations/vercel/test/fixtures/middleware/package.json b/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/package.json similarity index 70% rename from packages/integrations/vercel/test/fixtures/middleware/package.json rename to packages/integrations/vercel/test/fixtures/middleware-with-edge-file/package.json index 9ba60852d..2579a8c92 100644 --- a/packages/integrations/vercel/test/fixtures/middleware/package.json +++ b/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/package.json @@ -1,5 +1,5 @@ { - "name": "@test/vercel-edge-middleware", + "name": "@test/vercel-edge-middleware-with-edge-file", "version": "0.0.0", "private": true, "dependencies": { diff --git a/packages/integrations/vercel/test/fixtures/middleware/src/middleware.js b/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/src/middleware.js similarity index 100% rename from packages/integrations/vercel/test/fixtures/middleware/src/middleware.js rename to packages/integrations/vercel/test/fixtures/middleware-with-edge-file/src/middleware.js diff --git a/packages/integrations/vercel/test/fixtures/middleware/src/pages/index.astro b/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/src/pages/index.astro similarity index 100% rename from packages/integrations/vercel/test/fixtures/middleware/src/pages/index.astro rename to packages/integrations/vercel/test/fixtures/middleware-with-edge-file/src/pages/index.astro diff --git a/packages/integrations/vercel/test/fixtures/middleware/src/vercel-edge-middleware.js b/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/src/vercel-edge-middleware.js similarity index 100% rename from packages/integrations/vercel/test/fixtures/middleware/src/vercel-edge-middleware.js rename to packages/integrations/vercel/test/fixtures/middleware-with-edge-file/src/vercel-edge-middleware.js diff --git a/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/astro.config.mjs b/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/astro.config.mjs new file mode 100644 index 000000000..321a8bde3 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/astro.config.mjs @@ -0,0 +1,10 @@ +import {defineConfig} from "astro/config"; +import vercel from "@astrojs/vercel/serverless"; + +export default defineConfig({ + adapter: vercel(), + build: { + excludeMiddleware: true + }, + output: 'server' +}); \ No newline at end of file diff --git a/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/package.json b/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/package.json new file mode 100644 index 000000000..d5638ce16 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/vercel-edge-middleware-without-edge-file", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/vercel": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/src/middleware.js b/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/src/middleware.js new file mode 100644 index 000000000..349a0aa79 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/src/middleware.js @@ -0,0 +1,8 @@ +/** + * @type {import("astro").MiddlewareResponseHandler} + */ +export const onRequest = async (context, next) => { + const test = 'something'; + const response = await next(); + return response; +}; diff --git a/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/src/pages/index.astro b/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/src/pages/index.astro new file mode 100644 index 000000000..e69de29bb diff --git a/packages/integrations/vercel/test/setup.js b/packages/integrations/vercel/test/setup.js new file mode 100644 index 000000000..c53aa9894 --- /dev/null +++ b/packages/integrations/vercel/test/setup.js @@ -0,0 +1,12 @@ +import { use } from 'chai'; +import chaiJestSnapshot from 'chai-jest-snapshot'; + +use(chaiJestSnapshot); + +before(function () { + chaiJestSnapshot.resetSnapshotRegistry(); +}); + +beforeEach(function () { + chaiJestSnapshot.configureUsingMochaContext(this); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 54fa7ad4d..ee1bf68e2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4944,7 +4944,16 @@ importers: specifier: workspace:* version: link:../../../../../astro - packages/integrations/vercel/test/fixtures/middleware: + packages/integrations/vercel/test/fixtures/middleware-with-edge-file: + dependencies: + '@astrojs/vercel': + specifier: workspace:* + version: link:../../.. + astro: + specifier: workspace:* + version: link:../../../../../astro + + packages/integrations/vercel/test/fixtures/middleware-without-edge-file: dependencies: '@astrojs/vercel': specifier: workspace:*