0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(@astrojs/vercel): improve file detection (#7621)

This commit is contained in:
Emanuele Stoppa 2023-07-12 08:06:08 +01:00 committed by GitHub
parent 84e573a781
commit 2ddf342626
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 115 additions and 27 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---
Improve file detection of the middleware file handler

View file

@ -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",

View file

@ -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 })`;

View file

@ -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);
});
});

View file

@ -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;
};
// <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
};
"
`;

View file

@ -1,5 +1,5 @@
{
"name": "@test/vercel-edge-middleware",
"name": "@test/vercel-edge-middleware-with-edge-file",
"version": "0.0.0",
"private": true,
"dependencies": {

View 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'
});

View 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:*"
}
}

View file

@ -0,0 +1,8 @@
/**
* @type {import("astro").MiddlewareResponseHandler}
*/
export const onRequest = async (context, next) => {
const test = 'something';
const response = await next();
return response;
};

View 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);
});

View file

@ -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:*