0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-24 23:21:57 -05:00

fix(vercel): fallback to static 404.html (#9648)

This commit is contained in:
Arsh 2024-01-10 14:52:29 +00:00 committed by GitHub
parent a700a20291
commit d7f1903cde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 87 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/vercel": patch
---
Fixes an issue where the serverless function could not respond with a prerendered 404 page.

View file

@ -287,9 +287,15 @@ You can set functionPerRoute: false to prevent surpassing the limit.`
excludeFiles,
maxDuration,
});
routeDefinitions.push({ src: '/.*', dest: 'render' });
for (const route of routes) {
if (route.prerender) continue
routeDefinitions.push({
src: route.pattern.source,
dest: 'render',
})
}
}
const fourOhFourRoute = routes.find((route) => route.pathname === '/404');
// Output configuration
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
await writeJson(new URL(`./config.json`, _config.outDir), {
@ -303,6 +309,11 @@ You can set functionPerRoute: false to prevent surpassing the limit.`
},
{ handle: 'filesystem' },
...routeDefinitions,
...fourOhFourRoute ? [{
src: '/.*',
dest: fourOhFourRoute.prerender ? '/404.html' : 'render',
status: 404,
}] : [],
],
...(imageService || imagesConfig
? {

View file

@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
output: 'server',
adapter: vercel()
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/astro-vercel-prerendered-error-pages",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,4 @@
---
export const prerender = true
---
<h1>404</h1>

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>One</title>
</head>
<body>
<h1>One</h1>
</body>
</html>

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>Two</title>
</head>
<body>
<h1>Two</h1>
</body>
</html>

View file

@ -0,0 +1,23 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
describe('prerendered error pages routing', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/prerendered-error-pages/',
});
await fixture.build();
});
it('falls back to 404.html', async () => {
const deploymentConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
expect(deploymentConfig.routes.at(-1)).to.deep.include({
src: '/.*',
dest: '/404.html',
status: 404,
});
});
});

View file

@ -1,7 +1,7 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
describe('maxDuration', () => {
describe('static routing', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;

9
pnpm-lock.yaml generated
View file

@ -4749,6 +4749,15 @@ importers:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/vercel/test/fixtures/prerendered-error-pages:
dependencies:
'@astrojs/vercel':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/vercel/test/fixtures/redirects:
dependencies:
'@astrojs/vercel':