Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-pandas-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/vercel": patch
---

Fixes an issue where the serverless function could not respond with a prerendered 404 page.
15 changes: 13 additions & 2 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand All @@ -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
? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';

export default defineConfig({
output: 'server',
adapter: vercel()
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-vercel-prerendered-error-pages",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
export const prerender = true
---
<h1>404</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>One</title>
</head>
<body>
<h1>One</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Two</title>
</head>
<body>
<h1>Two</h1>
</body>
</html>
23 changes: 23 additions & 0 deletions packages/integrations/vercel/test/prerendered-error-pages.test.js
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
2 changes: 1 addition & 1 deletion packages/integrations/vercel/test/static.test.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.