From b05c9f13ec4b61053962b99a62794ee3831bf345 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 10 Feb 2022 15:48:12 -0500 Subject: [PATCH] Add prerendered pages to routes config (#3822) * empty commit * comment out redirect stuff * try this * add back redirects * ugh vscode auto-import is the dumbest thing * changeset --- .changeset/khaki-plants-compare.md | 5 +++++ packages/adapter-vercel/index.js | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 .changeset/khaki-plants-compare.md diff --git a/.changeset/khaki-plants-compare.md b/.changeset/khaki-plants-compare.md new file mode 100644 index 000000000000..e98c38a8d627 --- /dev/null +++ b/.changeset/khaki-plants-compare.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': patch +--- + +Add prerendered routes to config diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index e938b5353be6..dada84e1384d 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -1,4 +1,4 @@ -import { writeFileSync } from 'fs'; +import { existsSync, writeFileSync } from 'fs'; import { posix } from 'path'; import { fileURLToPath } from 'url'; import esbuild from 'esbuild'; @@ -102,7 +102,7 @@ export default function ({ external = [] } = {}) { builder.log.minor('Prerendering static pages...'); - await builder.prerender({ + const prerendered = await builder.prerender({ dest: `${dir}/static` }); @@ -143,10 +143,28 @@ export default function ({ external = [] } = {}) { builder.log.minor('Writing routes...'); builder.mkdirp(`${dir}/config`); + + const prerendered_routes = prerendered.paths + .map((path) => { + const file = + path === '/' + ? '/index.html' + : path + (builder.trailingSlash === 'always' ? '/index.html' : '.html'); + + if (existsSync(`${dir}/static${file}`)) { + return { + src: path, + dest: file + }; + } + }) + .filter(Boolean); + writeFileSync( `${dir}/config/routes.json`, JSON.stringify([ ...redirects[builder.trailingSlash], + ...prerendered_routes, { src: `/${builder.appDir}/.+`, headers: {