From 6e049be231086fa62df37a5489193703c9cfe262 Mon Sep 17 00:00:00 2001 From: pawelblaszczyk5 Date: Wed, 4 Jan 2023 12:38:48 +0100 Subject: [PATCH 1/5] Add prerendered assets to Vercel config --- .changeset/old-chicken-cough.md | 6 ++++++ packages/adapter-static/platforms.js | 8 ++++++++ packages/adapter-vercel/index.js | 8 ++++++++ 3 files changed, 22 insertions(+) create mode 100644 .changeset/old-chicken-cough.md diff --git a/.changeset/old-chicken-cough.md b/.changeset/old-chicken-cough.md new file mode 100644 index 000000000000..0c9f201a4867 --- /dev/null +++ b/.changeset/old-chicken-cough.md @@ -0,0 +1,6 @@ +--- +'@sveltejs/adapter-static': patch +'@sveltejs/adapter-vercel': patch +--- + +Add prerendered assets to Vercel config diff --git a/packages/adapter-static/platforms.js b/packages/adapter-static/platforms.js index d92967d09a3c..b9892d470ea8 100644 --- a/packages/adapter-static/platforms.js +++ b/packages/adapter-static/platforms.js @@ -40,6 +40,14 @@ function vercel_routes(builder) { }); } + // prerendered assets (data.json and other non-html pages) + for (const [src] of builder.prerendered.assets) { + routes.push({ + src, + dest: `${builder.config.kit.appDir}/prerendered/${src}` + }); + } + // implicit redirects (trailing slashes) for (const [src] of builder.prerendered.pages) { if (src !== '/') { diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 69ab0a8ba9d5..10f03a70185e 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -54,6 +54,14 @@ const plugin = function ({ external = [], edge, split } = {}) { } } + // prerendered assets (data.json and other non-html pages) + for (const [src] of builder.prerendered.assets) { + prerendered_redirects.push({ + src, + dest: `${builder.config.kit.appDir}/prerendered/${src}` + }); + } + /** @type {any[]} */ const routes = [ ...prerendered_redirects, From 0c68d4eb9ef8c180b47d74150eef097cee842b02 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 5 Jan 2023 17:24:30 -0500 Subject: [PATCH 2/5] use adapter-vercel logic inside adapter-static --- packages/adapter-static/platforms.js | 87 ++++++++------------ packages/adapter-vercel/index.js | 118 ++++++++++++--------------- 2 files changed, 90 insertions(+), 115 deletions(-) diff --git a/packages/adapter-static/platforms.js b/packages/adapter-static/platforms.js index b9892d470ea8..5a278c19d2cc 100644 --- a/packages/adapter-static/platforms.js +++ b/packages/adapter-static/platforms.js @@ -9,21 +9,17 @@ import fs from 'fs'; * }} * Platform */ +// This function is duplicated in adapter-vercel /** @param {import('@sveltejs/kit').Builder} builder */ -function vercel_routes(builder) { +function static_vercel_config(builder) { /** @type {any[]} */ - const routes = [ - { - src: `/${builder.config.kit.appDir}/immutable/.+`, - headers: { - 'cache-control': 'public, immutable, max-age=31536000' - } - } - ]; + const prerendered_redirects = []; + + /** @type {Record} */ + const overrides = {}; - // explicit redirects for (const [src, redirect] of builder.prerendered.redirects) { - routes.push({ + prerendered_redirects.push({ src, headers: { Location: redirect.location @@ -32,40 +28,35 @@ function vercel_routes(builder) { }); } - // prerendered pages - for (const [src, page] of builder.prerendered.pages) { - routes.push({ - src, - dest: `${builder.config.kit.appDir}/prerendered/${page.file}` - }); - } - - // prerendered assets (data.json and other non-html pages) - for (const [src] of builder.prerendered.assets) { - routes.push({ - src, - dest: `${builder.config.kit.appDir}/prerendered/${src}` - }); - } + for (const [path, page] of builder.prerendered.pages) { + if (path.endsWith('/') && path !== '/') { + prerendered_redirects.push( + { src: path, dest: path.slice(0, -1) }, + { src: path.slice(0, -1), status: 308, headers: { Location: path } } + ); - // implicit redirects (trailing slashes) - for (const [src] of builder.prerendered.pages) { - if (src !== '/') { - routes.push({ - src: src.endsWith('/') ? src.slice(0, -1) : src + '/', - headers: { - location: src - }, - status: 308 - }); + overrides[page.file] = { path: path.slice(1, -1) }; + } else { + overrides[page.file] = { path: path.slice(1) }; } } - routes.push({ - handle: 'filesystem' - }); - - return routes; + return { + version: 3, + routes: [ + ...prerendered_redirects, + { + src: `/${builder.getAppPath()}/immutable/.+`, + headers: { + 'cache-control': 'public, immutable, max-age=31536000' + } + }, + { + handle: 'filesystem' + } + ], + overrides + }; } /** @type {Platform[]} */ @@ -73,18 +64,12 @@ export const platforms = [ { name: 'Vercel', test: () => !!process.env.VERCEL, - defaults: (config) => ({ - pages: `.vercel/output/static/${config.kit.appDir}/prerendered`, - assets: '.vercel/output/static' + defaults: () => ({ + pages: '.vercel/output/static' }), done: (builder) => { - fs.writeFileSync( - '.vercel/output/config.json', - JSON.stringify({ - version: 3, - routes: vercel_routes(builder) - }) - ); + const config = static_vercel_config(builder); + fs.writeFileSync('.vercel/output/config.json', JSON.stringify(config, null, ' ')); } } ]; diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 10f03a70185e..529225d7cf28 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -25,56 +25,7 @@ const plugin = function ({ external = [], edge, split } = {}) { functions: `${dir}/functions` }; - /** @type {any[]} */ - const prerendered_redirects = []; - - /** @type {Record} */ - const overrides = {}; - - for (const [src, redirect] of builder.prerendered.redirects) { - prerendered_redirects.push({ - src, - headers: { - Location: redirect.location - }, - status: redirect.status - }); - } - - for (const [path, page] of builder.prerendered.pages) { - if (path.endsWith('/') && path !== '/') { - prerendered_redirects.push( - { src: path, dest: path.slice(0, -1) }, - { src: path.slice(0, -1), status: 308, headers: { Location: path } } - ); - - overrides[page.file] = { path: path.slice(1, -1) }; - } else { - overrides[page.file] = { path: path.slice(1) }; - } - } - - // prerendered assets (data.json and other non-html pages) - for (const [src] of builder.prerendered.assets) { - prerendered_redirects.push({ - src, - dest: `${builder.config.kit.appDir}/prerendered/${src}` - }); - } - - /** @type {any[]} */ - const routes = [ - ...prerendered_redirects, - { - src: `/${builder.getAppPath()}/.+`, - headers: { - 'cache-control': 'public, immutable, max-age=31536000' - } - }, - { - handle: 'filesystem' - } - ]; + const config = static_vercel_config(builder); builder.log.minor('Generating serverless function...'); @@ -105,7 +56,7 @@ const plugin = function ({ external = [], edge, split } = {}) { `nodejs${node_version.major}.x` ); - routes.push({ src: pattern, dest: `/${name}` }); + config.routes.push({ src: pattern, dest: `/${name}` }); } /** @@ -150,7 +101,7 @@ const plugin = function ({ external = [], edge, split } = {}) { }) ); - routes.push({ src: pattern, dest: `/${name}` }); + config.routes.push({ src: pattern, dest: `/${name}` }); } const generate_function = edge ? generate_edge_function : generate_serverless_function; @@ -190,18 +141,7 @@ const plugin = function ({ external = [], edge, split } = {}) { builder.log.minor('Writing routes...'); - write( - `${dir}/config.json`, - JSON.stringify( - { - version: 3, - routes, - overrides - }, - null, - ' ' - ) - ); + write(`${dir}/config.json`, JSON.stringify(config, null, ' ')); } }; }; @@ -233,6 +173,56 @@ function get_node_version() { return { major, full }; } +// This function is duplicated in adapter-static +/** @param {import('@sveltejs/kit').Builder} builder */ +function static_vercel_config(builder) { + /** @type {any[]} */ + const prerendered_redirects = []; + + /** @type {Record} */ + const overrides = {}; + + for (const [src, redirect] of builder.prerendered.redirects) { + prerendered_redirects.push({ + src, + headers: { + Location: redirect.location + }, + status: redirect.status + }); + } + + for (const [path, page] of builder.prerendered.pages) { + if (path.endsWith('/') && path !== '/') { + prerendered_redirects.push( + { src: path, dest: path.slice(0, -1) }, + { src: path.slice(0, -1), status: 308, headers: { Location: path } } + ); + + overrides[page.file] = { path: path.slice(1, -1) }; + } else { + overrides[page.file] = { path: path.slice(1) }; + } + } + + return { + version: 3, + routes: [ + ...prerendered_redirects, + { + src: `/${builder.getAppPath()}/immutable/.+`, + headers: { + 'cache-control': 'public, immutable, max-age=31536000' + } + }, + { + handle: 'filesystem' + } + ], + overrides + }; +} + /** * @param {import('@sveltejs/kit').Builder} builder * @param {string} entry From 56e33df5113af76c5ff632301f32e28d216e4589 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 5 Jan 2023 17:42:34 -0500 Subject: [PATCH 3/5] simplify --- packages/adapter-static/index.js | 4 +--- packages/adapter-static/platforms.js | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index b43956ae2938..8f859dabcd8e 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -71,9 +71,7 @@ See https://kit.svelte.dev/docs/page-options#prerender for more details` assets = pages, fallback, precompress - } = options ?? - platform?.defaults(builder.config) ?? - /** @type {import('./index').AdapterOptions} */ ({}); + } = options ?? platform?.defaults ?? /** @type {import('./index').AdapterOptions} */ ({}); builder.rimraf(assets); builder.rimraf(pages); diff --git a/packages/adapter-static/platforms.js b/packages/adapter-static/platforms.js index 5a278c19d2cc..3ecf37cb30d1 100644 --- a/packages/adapter-static/platforms.js +++ b/packages/adapter-static/platforms.js @@ -4,7 +4,7 @@ import fs from 'fs'; * @typedef {{ * name: string; * test: () => boolean; - * defaults: (config: any) => import('./index').AdapterOptions; // TODO + * defaults: import('./index').AdapterOptions; * done: (builder: import('@sveltejs/kit').Builder) => void; * }} * Platform */ @@ -64,9 +64,9 @@ export const platforms = [ { name: 'Vercel', test: () => !!process.env.VERCEL, - defaults: () => ({ + defaults: { pages: '.vercel/output/static' - }), + }, done: (builder) => { const config = static_vercel_config(builder); fs.writeFileSync('.vercel/output/config.json', JSON.stringify(config, null, ' ')); From 0e0615b184d582d1b1b15f5d5e91ab686866b3bc Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 5 Jan 2023 19:34:34 -0500 Subject: [PATCH 4/5] update changeset --- .changeset/old-chicken-cough.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.changeset/old-chicken-cough.md b/.changeset/old-chicken-cough.md index 0c9f201a4867..a184c6c7376c 100644 --- a/.changeset/old-chicken-cough.md +++ b/.changeset/old-chicken-cough.md @@ -1,6 +1,5 @@ --- '@sveltejs/adapter-static': patch -'@sveltejs/adapter-vercel': patch --- -Add prerendered assets to Vercel config +Match `adapter-vercel` logic for serving prerendered content From 048dcfed2f1cc2a53c6d6ce8526f1a971706730c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 5 Jan 2023 19:35:19 -0500 Subject: [PATCH 5/5] add adapter-vercel changeset --- .changeset/healthy-singers-stare.md | 5 +++++ .changeset/old-chicken-cough.md | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/healthy-singers-stare.md diff --git a/.changeset/healthy-singers-stare.md b/.changeset/healthy-singers-stare.md new file mode 100644 index 000000000000..43bc2b42887b --- /dev/null +++ b/.changeset/healthy-singers-stare.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': patch +--- + +[fix] only apply immutable cache-control headers to immutable assets diff --git a/.changeset/old-chicken-cough.md b/.changeset/old-chicken-cough.md index a184c6c7376c..b3aae3fb3035 100644 --- a/.changeset/old-chicken-cough.md +++ b/.changeset/old-chicken-cough.md @@ -2,4 +2,4 @@ '@sveltejs/adapter-static': patch --- -Match `adapter-vercel` logic for serving prerendered content +[fix] match `adapter-vercel` logic for serving prerendered content