From e685c5aecdefbc57fcbec5624ed43fd7dd01a037 Mon Sep 17 00:00:00 2001 From: Gal Schlezinger Date: Mon, 16 May 2022 16:54:50 +0300 Subject: [PATCH] Use general purpose edge functions for vercel-adapter (#4883) * Use `dest` instead of `middlewarePath` This will use general purpose edge functions instead of our Middleware infrastructure. This will support Cache-Control and also streamlines the routing. * Fix routing in `split: true` * Add changeset * Update packages/adapter-vercel/index.js * Update packages/adapter-vercel/index.js Co-authored-by: Rich Harris --- .changeset/rich-singers-taste.md | 5 +++++ packages/adapter-vercel/index.js | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 .changeset/rich-singers-taste.md diff --git a/.changeset/rich-singers-taste.md b/.changeset/rich-singers-taste.md new file mode 100644 index 000000000000..4fc754089375 --- /dev/null +++ b/.changeset/rich-singers-taste.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': patch +--- + +Use general purpose Edge Functions instead of piggybacking Middleware for Edge Deployment + fix split mode diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 0cd43b209af3..6c89940d8952 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -329,7 +329,7 @@ async function v3(builder, external, edge, split) { }) ); - routes.push({ src: pattern, middlewarePath: name }); + routes.push({ src: pattern, dest: `/${name}` }); } const generate_function = edge ? generate_edge_function : generate_serverless_function; @@ -340,10 +340,19 @@ async function v3(builder, external, edge, split) { id: route.pattern.toString(), // TODO is `id` necessary? filter: (other) => route.pattern.toString() === other.pattern.toString(), complete: async (entry) => { - const src = `${route.pattern + let sliced_pattern = route.pattern .toString() - .slice(1, -2) // remove leading / and trailing $/ - .replace(/\\\//g, '/')}(?:/__data.json)?$`; // TODO adding /__data.json is a temporary workaround — those endpoints should be treated as distinct routes + // remove leading / and trailing $/ + .slice(1, -2) + // replace escaped \/ with / + .replace(/\\\//g, '/'); + + // replace the root route "^/" with "^/?" + if (sliced_pattern === '^/') { + sliced_pattern = '^/?'; + } + + const src = `${sliced_pattern}(?:/__data.json)?$`; // TODO adding /__data.json is a temporary workaround — those endpoints should be treated as distinct routes await generate_function(route.id || 'index', src, entry.generateManifest); }