Skip to content

Commit

Permalink
[chore] simplify internal prerender API (#5659)
Browse files Browse the repository at this point in the history
* [chore] simplify internal prerender API

* Update packages/kit/src/core/prerender/prerender.js

Co-authored-by: Rich Harris <hello@rich-harris.dev>

* Update packages/kit/src/core/prerender/prerender.js

Co-authored-by: Rich Harris <hello@rich-harris.dev>

* Update packages/kit/src/core/prerender/prerender.js

Co-authored-by: Rich Harris <hello@rich-harris.dev>

* try to fix windows

* format

* try to fix windows again

* fix

* i clearly haven't slept. also windows :-p

* troubleshooting via CI sucks

* Update packages/kit/src/core/prerender/prerender.js

Co-authored-by: Rich Harris <hello@rich-harris.dev>

* remove changeset

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
benmccann and Rich-Harris authored Jul 21, 2022
1 parent 4d230be commit bfac97a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
15 changes: 11 additions & 4 deletions packages/kit/src/core/prerender/prerender.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { pathToFileURL, URL } from 'url';
import { mkdirp } from '../../utils/filesystem.js';
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
import { installPolyfills } from '../../node/polyfills.js';
import { is_root_relative, resolve } from '../../utils/url.js';
import { queue } from './queue.js';
Expand Down Expand Up @@ -52,12 +52,12 @@ const REDIRECT = 3;
/**
* @param {{
* config: import('types').ValidatedKitConfig;
* entries: string[];
* files: Set<string>;
* client_out_dir: string;
* manifest_path: string;
* log: Logger;
* }} opts
*/
export async function prerender({ config, entries, files, log }) {
export async function prerender({ config, client_out_dir, manifest_path, log }) {
/** @type {import('types').Prerendered} */
const prerendered = {
pages: new Map(),
Expand Down Expand Up @@ -158,6 +158,7 @@ export async function prerender({ config, entries, files, log }) {
return file;
}

const files = new Set(walk(client_out_dir).map(posixify));
const seen = new Set();
const written = new Set();

Expand Down Expand Up @@ -319,6 +320,12 @@ export async function prerender({ config, entries, files, log }) {
if (config.prerender.enabled) {
for (const entry of config.prerender.entries) {
if (entry === '*') {
/** @type {import('types').ManifestData} */
const { routes } = (await import(pathToFileURL(manifest_path).href)).manifest._;
const entries = routes
.map((route) => (route.type === 'page' ? route.path : ''))
.filter(Boolean);

for (const entry of entries) {
enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these?
}
Expand Down
24 changes: 4 additions & 20 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ function kit() {
server
};

const manifest_path = `${paths.output_dir}/server/manifest.js`;
fs.writeFileSync(
`${paths.output_dir}/server/manifest.js`,
manifest_path,
`export const manifest = ${generate_manifest({
build_data,
relative_path: '.',
Expand All @@ -314,27 +315,10 @@ function kit() {
process.env.SVELTEKIT_SERVER_BUILD_COMPLETED = 'true';
log.info('Prerendering');

const static_files = manifest_data.assets.map((asset) => posixify(asset.file));

const files = new Set([
...static_files,
...chunks.map((chunk) => chunk.fileName),
...assets.map((chunk) => chunk.fileName)
]);

// TODO is this right?
static_files.forEach((file) => {
if (file.endsWith('/index.html')) {
files.add(file.slice(0, -11));
}
});

prerendered = await prerender({
config: svelte_config.kit,
entries: manifest_data.routes
.map((route) => (route.type === 'page' ? route.path : ''))
.filter(Boolean),
files,
client_out_dir: vite_config.build.outDir,
manifest_path,
log
});

Expand Down

0 comments on commit bfac97a

Please sign in to comment.