Skip to content

Commit

Permalink
prerendering overhaul (#3858)
Browse files Browse the repository at this point in the history
* add new Prerendered interface

* rendered -> response

* rename path/decoded_path to encoded/decoded, for clarity

* tidy up

* add some failing tests

* tidy up

* keep track of encoded vs decoded paths a bit better

* handle encoded dependency paths

* tidy up a bit

* DRY out saving logic

* change return value of prerender

* switch to using prerendered.pages

* i bet eslint is fun at parties

* robustify/simplify logic

* lint

* dry out path normalizing logic

* fix logic for excluding prerendered pages from manifest

* fix

* add prerendered.paths array

* simplify

* lint

* oops
  • Loading branch information
Rich-Harris authored Feb 12, 2022
1 parent c13e113 commit 8fe3ea3
Show file tree
Hide file tree
Showing 22 changed files with 346 additions and 199 deletions.
7 changes: 7 additions & 0 deletions packages/adapter-cloudflare-workers/files/entry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ declare module 'MANIFEST' {
export const manifest: SSRManifest;
export const prerendered: Set<string>;
}

declare abstract class FetchEvent extends Event {
readonly request: Request;
respondWith(promise: Response | Promise<Response>): void;
passThroughOnException(): void;
waitUntil(promise: Promise<any>): void;
}
12 changes: 7 additions & 5 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export default function () {
async adapt(builder) {
const { site } = validate_config(builder);

const bucket = site.bucket;
// @ts-ignore
const { bucket } = site;

// @ts-ignore
const entrypoint = site['entry-point'] || 'workers-site';

const files = fileURLToPath(new URL('./files', import.meta.url));
Expand All @@ -23,9 +26,7 @@ export default function () {
builder.rimraf(entrypoint);

builder.log.info('Prerendering static pages...');
const { paths } = await builder.prerender({
dest: bucket
});
const prerendered = await builder.prerender({ dest: bucket });

builder.log.info('Installing worker dependencies...');
builder.copy(`${files}/_package.json`, `${tmp}/package.json`);
Expand All @@ -48,7 +49,7 @@ export default function () {
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath
})};\n\nexport const prerendered = new Set(${JSON.stringify(paths)});\n`
})};\n\nexport const prerendered = new Set(${JSON.stringify(prerendered.paths)});\n`
);

await esbuild.build({
Expand Down Expand Up @@ -80,6 +81,7 @@ function validate_config(builder) {
throw err;
}

// @ts-ignore
if (!wrangler_config.site || !wrangler_config.site.bucket) {
throw new Error(
'You must specify site.bucket in wrangler.toml. Consult https://developers.cloudflare.com/workers/platform/sites/configuration'
Expand Down
1 change: 0 additions & 1 deletion packages/adapter-cloudflare-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
},
"devDependencies": {
"@cloudflare/kv-asset-handler": "^0.2.0",
"@cloudflare/workers-types": "^3.3.0",
"@sveltejs/kit": "workspace:*"
}
}
3 changes: 1 addition & 2 deletions packages/adapter-cloudflare-workers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"target": "es2020",
"module": "es2020",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"types": ["@cloudflare/workers-types"]
"allowSyntheticDefaultImports": true
},
"include": ["./index.js", "files"]
}
4 changes: 4 additions & 0 deletions packages/adapter-cloudflare/files/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const app = new App(manifest);
const prefix = `/${manifest.appDir}/`;

export default {
/**
* @param {Request} req
* @param {any} env
*/
async fetch(req, env) {
const url = new URL(req.url);

Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export default function (options = {}) {
builder.writeStatic(dest);
builder.writeClient(dest);

const { paths } = await builder.prerender({ dest });
const prerendered = await builder.prerender({ dest });

const relativePath = posix.relative(tmp, builder.getServerDirectory());

writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath
})};\n\nexport const prerendered = new Set(${JSON.stringify(paths)});\n`
})};\n\nexport const prerendered = new Set(${JSON.stringify(prerendered.paths)});\n`
);

builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, {
Expand Down
32 changes: 15 additions & 17 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, writeFileSync } from 'fs';
import { writeFileSync } from 'fs';
import { posix } from 'path';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';
Expand Down Expand Up @@ -144,27 +144,25 @@ export default function ({ external = [] } = {}) {

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);
const prerendered_pages = Array.from(prerendered.pages, ([src, page]) => ({
src,
dest: page.file
}));

const prerendered_redirects = Array.from(prerendered.redirects, ([src, redirect]) => ({
src,
headers: {
Location: redirect.location
},
status: redirect.status
}));

writeFileSync(
`${dir}/config/routes.json`,
JSON.stringify([
...redirects[builder.trailingSlash],
...prerendered_routes,
...prerendered_pages,
...prerendered_redirects,
{
src: `/${builder.appDir}/.+`,
headers: {
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/src/core/adapt/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import { generate_manifest } from '../generate_manifest/index.js';
*/
export function create_builder({ cwd, config, build_data, log }) {
/** @type {Set<string>} */
const prerendered_paths = new Set();
let prerendered_paths;

let generated_manifest = false;

/** @param {import('types/internal').RouteData} route */
function not_prerendered(route) {
if (!prerendered_paths) return true;

if (route.type === 'page' && route.path) {
return !prerendered_paths.has(route.path);
}
Expand Down Expand Up @@ -163,10 +166,7 @@ export function create_builder({ cwd, config, build_data, log }) {
log
});

prerendered.paths.forEach((path) => {
prerendered_paths.add(path);
prerendered_paths.add(path + '/');
});
prerendered_paths = new Set(prerendered.paths);

return prerendered;
}
Expand Down
Loading

0 comments on commit 8fe3ea3

Please sign in to comment.