diff --git a/.changeset/stale-pandas-count.md b/.changeset/stale-pandas-count.md new file mode 100644 index 0000000000..b67de833cf --- /dev/null +++ b/.changeset/stale-pandas-count.md @@ -0,0 +1,5 @@ +--- +"@astrojs/vercel": patch +--- + +Fixes an issue where the serverless function could not respond with a prerendered 404 page. diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index dd07e723b6..db7f428051 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -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 ? { diff --git a/packages/integrations/vercel/test/fixtures/prerendered-error-pages/astro.config.mjs b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/astro.config.mjs new file mode 100644 index 0000000000..b3f5bd362b --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import vercel from '@astrojs/vercel/serverless'; + +export default defineConfig({ + output: 'server', + adapter: vercel() +}); diff --git a/packages/integrations/vercel/test/fixtures/prerendered-error-pages/package.json b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/package.json new file mode 100644 index 0000000000..5cfc0e24c4 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/astro-vercel-prerendered-error-pages", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/vercel": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/404.astro b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/404.astro new file mode 100644 index 0000000000..162ff90b68 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/404.astro @@ -0,0 +1,4 @@ +--- +export const prerender = true +--- +