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

add catch-all route for framework 404s rather than platform 404s #9265

Merged
merged 2 commits into from
Mar 1, 2023
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/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: `/${DEFAULT_FUNCTION_NAME}` });

builder.log.minor('Copying assets...');

builder.writeClient(dirs.static);
Expand Down