Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-coins-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Return an array of written files when prerendering.
14 changes: 12 additions & 2 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ const REDIRECT = 3;
* fallback?: string;
* all: boolean; // disregard `export const prerender = true`
* }} opts
* @returns {Promise<Array<string>>} returns a promise that resolves to an array of paths corresponding to the files that have been prerendered.
*/
export async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
if (!config.kit.prerender.enabled && !fallback) {
return;
return [];
}

__fetch_polyfill();
Expand All @@ -118,6 +119,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
const error = chooseErrorHandler(log, config.kit.prerender.onError);

const files = new Set([...build_data.static, ...build_data.client]);
const written_files = [];

build_data.static.forEach((file) => {
if (file.endsWith('/index.html')) {
Expand Down Expand Up @@ -188,6 +190,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
if (location) {
log.warn(`${rendered.status} ${decoded_path} -> ${location}`);
writeFileSync(file, `<meta http-equiv="refresh" content="0;url=${encodeURI(location)}">`);
written_files.push(file);
} else {
log.warn(`location header missing on redirect received from ${decoded_path}`);
}
Expand All @@ -198,6 +201,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
if (rendered.status === 200) {
log.info(`${rendered.status} ${decoded_path}`);
writeFileSync(file, rendered.body || '');
written_files.push(file);
} else if (response_type !== OK) {
error({ status: rendered.status, path, referrer, referenceType: 'linked' });
}
Expand All @@ -215,7 +219,10 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
const file = `${out}${parts.join('/')}`;
mkdirp(dirname(file));

if (result.body) writeFileSync(file, result.body);
if (result.body) {
writeFileSync(file, result.body);
written_files.push(file);
}

if (response_type === OK) {
log.info(`${result.status} ${dependency_path}`);
Expand Down Expand Up @@ -309,5 +316,8 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
const file = join(out, fallback);
mkdirp(dirname(file));
writeFileSync(file, rendered.body || '');
written_files.push(file);
}

return written_files;
}