fix(@astrojs/vercel): improve file detection (#7621)
This commit is contained in:
parent
84e573a781
commit
2ddf342626
16 changed files with 115 additions and 27 deletions
5
.changeset/tidy-actors-relate.md
Normal file
5
.changeset/tidy-actors-relate.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/vercel': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve file detection of the middleware file handler
|
|
@ -47,7 +47,7 @@
|
||||||
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
||||||
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
||||||
"dev": "astro-scripts dev \"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": {
|
"dependencies": {
|
||||||
"@astrojs/internal-helpers": "^0.1.1",
|
"@astrojs/internal-helpers": "^0.1.1",
|
||||||
|
|
|
@ -50,7 +50,7 @@ function edgeMiddlewareTemplate(middlewarePath: string, vercelEdgeMiddlewareHand
|
||||||
const filePathEdgeMiddleware = fileURLToPath(vercelEdgeMiddlewareHandlerPath);
|
const filePathEdgeMiddleware = fileURLToPath(vercelEdgeMiddlewareHandlerPath);
|
||||||
let handlerTemplateImport = '';
|
let handlerTemplateImport = '';
|
||||||
let handlerTemplateCall = '{}';
|
let handlerTemplateCall = '{}';
|
||||||
if (existsSync(filePathEdgeMiddleware) + '.js' || existsSync(filePathEdgeMiddleware) + '.ts') {
|
if (existsSync(filePathEdgeMiddleware + '.js') || existsSync(filePathEdgeMiddleware + '.ts')) {
|
||||||
const stringified = JSON.stringify(filePathEdgeMiddleware.replace(/\\/g, '/'));
|
const stringified = JSON.stringify(filePathEdgeMiddleware.replace(/\\/g, '/'));
|
||||||
handlerTemplateImport = `import handler from ${stringified}`;
|
handlerTemplateImport = `import handler from ${stringified}`;
|
||||||
handlerTemplateCall = `handler({ request, context })`;
|
handlerTemplateCall = `handler({ request, context })`;
|
||||||
|
|
|
@ -1,30 +1,33 @@
|
||||||
import { loadFixture } from './test-utils.js';
|
import { loadFixture } from './test-utils.js';
|
||||||
import { expect, use } from 'chai';
|
import { expect } from 'chai';
|
||||||
import chaiJestSnapshot from 'chai-jest-snapshot';
|
import chaiJestSnapshot from 'chai-jest-snapshot';
|
||||||
|
|
||||||
use(chaiJestSnapshot);
|
describe('Vercel edge middleware', () => {
|
||||||
|
it('with edge handle file, should successfully build the middleware', async () => {
|
||||||
describe('Serverless prerender', () => {
|
const fixture = await loadFixture({
|
||||||
/** @type {import('./test-utils').Fixture} */
|
root: './fixtures/middleware-with-edge-file/',
|
||||||
let fixture;
|
|
||||||
|
|
||||||
beforeEach(function () {
|
|
||||||
chaiJestSnapshot.configureUsingMochaContext(this);
|
|
||||||
});
|
|
||||||
|
|
||||||
before(async () => {
|
|
||||||
chaiJestSnapshot.resetSnapshotRegistry();
|
|
||||||
fixture = await loadFixture({
|
|
||||||
root: './fixtures/middleware/',
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('build successfully the middleware edge file', async () => {
|
|
||||||
await fixture.build();
|
await fixture.build();
|
||||||
const contents = await fixture.readFile(
|
const contents = await fixture.readFile(
|
||||||
// this is abysmal...
|
// 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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
exports[`Serverless prerender build successfully the middleware edge file 1`] = `
|
exports[`Middleware with handler file 1`] = `
|
||||||
"// test/fixtures/middleware/src/vercel-edge-middleware.js
|
"// test/fixtures/middleware-with-edge-file/src/vercel-edge-middleware.js
|
||||||
function vercel_edge_middleware_default({ request, context }) {
|
function vercel_edge_middleware_default({ request, context }) {
|
||||||
return {
|
return {
|
||||||
title: \\"Hello world\\"
|
title: \\"Hello world\\"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// test/fixtures/middleware/dist/middleware2.mjs
|
// test/fixtures/middleware-with-edge-file/dist/middleware2.mjs
|
||||||
var onRequest = async (context, next) => {
|
var onRequest = async (context, next) => {
|
||||||
const response = await next();
|
const response = await next();
|
||||||
return response;
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
// <stdin>
|
||||||
|
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
|
||||||
|
};
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "@test/vercel-edge-middleware",
|
"name": "@test/vercel-edge-middleware-with-edge-file",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
10
packages/integrations/vercel/test/fixtures/middleware-without-edge-file/astro.config.mjs
vendored
Normal file
10
packages/integrations/vercel/test/fixtures/middleware-without-edge-file/astro.config.mjs
vendored
Normal file
|
@ -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'
|
||||||
|
});
|
9
packages/integrations/vercel/test/fixtures/middleware-without-edge-file/package.json
vendored
Normal file
9
packages/integrations/vercel/test/fixtures/middleware-without-edge-file/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "@test/vercel-edge-middleware-without-edge-file",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@astrojs/vercel": "workspace:*",
|
||||||
|
"astro": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
8
packages/integrations/vercel/test/fixtures/middleware-without-edge-file/src/middleware.js
vendored
Normal file
8
packages/integrations/vercel/test/fixtures/middleware-without-edge-file/src/middleware.js
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
/**
|
||||||
|
* @type {import("astro").MiddlewareResponseHandler}
|
||||||
|
*/
|
||||||
|
export const onRequest = async (context, next) => {
|
||||||
|
const test = 'something';
|
||||||
|
const response = await next();
|
||||||
|
return response;
|
||||||
|
};
|
12
packages/integrations/vercel/test/setup.js
Normal file
12
packages/integrations/vercel/test/setup.js
Normal file
|
@ -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);
|
||||||
|
});
|
|
@ -4944,7 +4944,16 @@ importers:
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../../../../../astro
|
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:
|
dependencies:
|
||||||
'@astrojs/vercel':
|
'@astrojs/vercel':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
|
|
Loading…
Reference in a new issue