From 3be9b6486cbcdcf79ff034f5c10b43effef1d149 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:30:36 +0000 Subject: [PATCH 1/3] allow 404.html to be the catch-all route --- .../integrations/vercel/src/serverless/adapter.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index dd07e723b644..db7f428051e1 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 ? { From 13eab6d0b53e01734434a1d8bdd2efa8947b97bc Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Mon, 8 Jan 2024 23:06:35 +0000 Subject: [PATCH 2/3] add test --- .../prerendered-error-pages/astro.config.mjs | 7 ++++++ .../prerendered-error-pages/package.json | 9 ++++++++ .../src/pages/404.astro | 4 ++++ .../src/pages/one.astro | 8 +++++++ .../src/pages/two.astro | 8 +++++++ .../test/prerendered-error-pages.test.js | 23 +++++++++++++++++++ .../integrations/vercel/test/static.test.js | 2 +- pnpm-lock.yaml | 9 ++++++++ 8 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 packages/integrations/vercel/test/fixtures/prerendered-error-pages/astro.config.mjs create mode 100644 packages/integrations/vercel/test/fixtures/prerendered-error-pages/package.json create mode 100644 packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/404.astro create mode 100644 packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/one.astro create mode 100644 packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/two.astro create mode 100644 packages/integrations/vercel/test/prerendered-error-pages.test.js 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 000000000000..b3f5bd362bae --- /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 000000000000..5cfc0e24c44d --- /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 000000000000..162ff90b6820 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/404.astro @@ -0,0 +1,4 @@ +--- +export const prerender = true +--- +

404

diff --git a/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/one.astro b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/one.astro new file mode 100644 index 000000000000..0c7fb90a735e --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/one.astro @@ -0,0 +1,8 @@ + + + One + + +

One

+ + diff --git a/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/two.astro b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/two.astro new file mode 100644 index 000000000000..e7ba9910e2a6 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/prerendered-error-pages/src/pages/two.astro @@ -0,0 +1,8 @@ + + + Two + + +

Two

+ + diff --git a/packages/integrations/vercel/test/prerendered-error-pages.test.js b/packages/integrations/vercel/test/prerendered-error-pages.test.js new file mode 100644 index 000000000000..9085662238d8 --- /dev/null +++ b/packages/integrations/vercel/test/prerendered-error-pages.test.js @@ -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, + }); + }); +}); diff --git a/packages/integrations/vercel/test/static.test.js b/packages/integrations/vercel/test/static.test.js index fc97e46ad7e9..a3d0ea738272 100644 --- a/packages/integrations/vercel/test/static.test.js +++ b/packages/integrations/vercel/test/static.test.js @@ -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; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7dd02558ad6a..69b6abd703bf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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': From 6a356c63f7b6a7f5ea654c02d51dadac37dfb938 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:36:03 +0000 Subject: [PATCH 3/3] add changeset --- .changeset/stale-pandas-count.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/stale-pandas-count.md diff --git a/.changeset/stale-pandas-count.md b/.changeset/stale-pandas-count.md new file mode 100644 index 000000000000..b67de833cf80 --- /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.