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

more robust recovery from manifest errors #8095

Merged
merged 5 commits into from
Dec 12, 2022
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/shy-toys-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

More robust manifest error recovery
46 changes: 36 additions & 10 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export async function dev(vite, vite_config, svelte_config) {
/** @type {import('types').SSRManifest} */
let manifest;

/** @type {Error | null} */
let manifest_error = null;

/** @param {string} id */
async function resolve(id) {
const url = id.startsWith('..') ? `/@fs${path.posix.resolve(id)}` : `/${id}`;
Expand All @@ -51,18 +54,24 @@ export async function dev(vite, vite_config, svelte_config) {
async function update_manifest() {
try {
({ manifest_data } = await sync.create(svelte_config));

if (manifest_error) {
manifest_error = null;
vite.ws.send({ type: 'full-reload' });
}
} catch (error) {
console.error(colors.bold().red('Failed to update manifest'));
manifest_error = /** @type {Error} */ (error);

console.error(colors.bold().red('Invalid routes'));
Copy link
Member

Choose a reason for hiding this comment

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

This could be more than invalid routes (matchers for example) - I chose "manifest" because it was a more generic while not being completely opaque to the user

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's still okay — matchers are part of your routes

console.error(error);
vite.ws.send({
type: 'error',
err: {
message: `Failed to udpate manifest: ${
/** @type {Error} */ (error)?.message ?? 'Unknown error'
}`,
stack: /** @type {Error} */ (error)?.stack ?? ''
message: manifest_error.message ?? 'Invalid routes',
stack: ''
}
});

return;
}

Expand Down Expand Up @@ -447,6 +456,27 @@ export async function dev(vite, vite_config, svelte_config) {
const template = load_template(cwd, svelte_config);
const error_page = load_error_page(svelte_config);

/** @param {{ status: number; message: string }} opts */
const error_template = ({ status, message }) => {
return error_page
.replace(/%sveltekit\.status%/g, String(status))
.replace(/%sveltekit\.error\.message%/g, message);
};

if (manifest_error) {
console.error(colors.bold().red('Invalid routes'));
console.error(manifest_error);

res.writeHead(500, {
'Content-Type': 'text/html; charset=utf-8'
});
res.end(
error_template({ status: 500, message: manifest_error.message ?? 'Invalid routes' })
);

return;
}

const rendered = await respond(
request,
{
Expand Down Expand Up @@ -501,11 +531,7 @@ export async function dev(vite, vite_config, svelte_config) {
);
},
app_template_contains_nonce: template.includes('%sveltekit.nonce%'),
error_template: ({ status, message }) => {
return error_page
.replace(/%sveltekit\.status%/g, String(status))
.replace(/%sveltekit\.error\.message%/g, message);
},
error_template,
service_worker:
svelte_config.kit.serviceWorker.register &&
!!resolve_entry(svelte_config.kit.files.serviceWorker),
Expand Down