Skip to content

Commit

Permalink
add catch-all route for framework 404s rather than platform 404s - cl…
Browse files Browse the repository at this point in the history
…oses #8287
  • Loading branch information
Rich-Harris committed Mar 1, 2023
1 parent 1d45113 commit 34404fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-drinks-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

fix: add catch-all route for 404s
25 changes: 20 additions & 5 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 34404fb

Please sign in to comment.