From 34404fb4ff3e5c01162012a9eb6c29501f789be7 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 1 Mar 2023 11:19:56 -0500 Subject: [PATCH 1/2] add catch-all route for framework 404s rather than platform 404s - closes #8287 --- .changeset/witty-drinks-burn.md | 5 +++++ packages/adapter-vercel/index.js | 25 ++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 .changeset/witty-drinks-burn.md diff --git a/.changeset/witty-drinks-burn.md b/.changeset/witty-drinks-burn.md new file mode 100644 index 000000000000..6bbf9347c06b --- /dev/null +++ b/.changeset/witty-drinks-burn.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': patch +--- + +fix: add catch-all route for 404s diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index d66fc6680739..30454195d558 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -6,6 +6,8 @@ import esbuild from 'esbuild'; const VALID_RUNTIMES = ['edge', 'nodejs16.x', 'nodejs18.x']; +const DEFAULT_FUNCTION_NAME = 'fn'; + const get_default_runtime = () => { const major = process.version.slice(1).split('.')[0]; if (major === '16') return 'nodejs16.x'; @@ -214,7 +216,7 @@ const plugin = function (defaults = {}) { group.config.runtime === 'edge' ? generate_edge_function : generate_serverless_function; // generate one function for the group - const name = singular ? 'fn' : `fn-${group.i}`; + const name = singular ? DEFAULT_FUNCTION_NAME : `fn-${group.i}`; await generate_function( name, @@ -287,12 +289,25 @@ const plugin = function (defaults = {}) { } } - if (singular) { - // Common case: One function for all routes - // Needs to happen after ISR or else regex swallows all other matches - static_config.routes.push({ src: '/.*', dest: `/fn` }); + if (!singular) { + // we need to create a catch-all route so that 404s are handled + // by SvelteKit rather than Vercel + + const runtime = defaults.runtime ?? get_default_runtime(); + const generate_function = + runtime === 'edge' ? generate_edge_function : generate_serverless_function; + + await generate_function( + DEFAULT_FUNCTION_NAME, + /** @type {any} */ ({ runtime, ...defaults }), + [] + ); } + // Catch-all route must come at the end, otherwise it will swallow all other routes, + // including ISR aliases if there is only one function + static_config.routes.push({ src: '/.*', dest: `/fn` }); + builder.log.minor('Copying assets...'); builder.writeClient(dirs.static); From 9960a98d4379cb4e232413f0aa9102d2fc51739f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 1 Mar 2023 11:47:53 -0500 Subject: [PATCH 2/2] Update packages/adapter-vercel/index.js Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --- packages/adapter-vercel/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 30454195d558..0f02652f8e3d 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -306,7 +306,7 @@ const plugin = function (defaults = {}) { // Catch-all route must come at the end, otherwise it will swallow all other routes, // including ISR aliases if there is only one function - static_config.routes.push({ src: '/.*', dest: `/fn` }); + static_config.routes.push({ src: '/.*', dest: `/${DEFAULT_FUNCTION_NAME}` }); builder.log.minor('Copying assets...');