Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] return list of prerendered files when prerendering #2675

Merged
merged 6 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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.
15 changes: 12 additions & 3 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ const OK = 2;
const REDIRECT = 3;

/**
* @param {{
* @type {(opts: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@type is a bit uncommon. I'd make it @param and @return so you can add a description on the return value

Copy link
Member Author

@pngwn pngwn Oct 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this, can you check it is correct? I'm not too familiar with jsdoc types but i think @returns is correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, but I think it'd be nice to add a short description after the type of what it is that's being returned

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, also tweaked the early return value to ensure a consistent return value.

* cwd: string;
* out: string;
* log: Logger;
* config: import('types/config').ValidatedConfig;
* build_data: import('types/internal').BuildData;
* fallback?: string;
* all: boolean; // disregard `export const prerender = true`
* }} opts
* }) => Promise<Array<string> | void>}
*/
export async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
if (!config.kit.prerender.enabled && !fallback) {
Expand All @@ -118,6 +118,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 +189,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 +200,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 +218,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 +315,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;
}