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

make 404 more helpful if paths.base is missing #5622

Merged
merged 6 commits into from
Jul 20, 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/clean-schools-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Make 404 error more helpful if paths.base is missing
33 changes: 22 additions & 11 deletions packages/kit/src/core/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,39 @@ import { escape_html_attr } from '../../utils/escape.js';

/**
* @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
* @typedef {import('types').PrerenderOnErrorValue} OnError
* @typedef {import('types').Logger} Logger
*/

/** @type {(details: Parameters<PrerenderErrorHandler>[0] ) => string} */
function format_error({ status, path, referrer, referenceType }) {
return `${status} ${path}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
/**
* @param {Parameters<PrerenderErrorHandler>[0]} details
* @param {import('types').ValidatedKitConfig} config
*/
function format_error({ status, path, referrer, referenceType }, config) {
const message =
status === 404 && !path.startsWith(config.paths.base)
? `${path} does not begin with \`base\`, which is configured in \`paths.base\` and can be imported from \`$app/paths\``
: path;

return `${status} ${message}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
}

/** @type {(log: Logger, onError: OnError) => PrerenderErrorHandler} */
function normalise_error_handler(log, onError) {
Copy link
Member

Choose a reason for hiding this comment

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

Personal preference: I liked it better when this was a separate function (even if it's only used in one place). What was your reasoning for inlining it?

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason was that it needed to pass config to format_details... though I don't know why I didn't just pass config as an argument. fixing

switch (onError) {
/**
* @param {Logger} log
* @param {import('types').ValidatedKitConfig} config
* @returns {PrerenderErrorHandler}
*/
function normalise_error_handler(log, config) {
switch (config.prerender.onError) {
case 'continue':
return (details) => {
log.error(format_error(details));
log.error(format_error(details, config));
};
case 'fail':
return (details) => {
throw new Error(format_error(details));
throw new Error(format_error(details, config));
};
default:
return onError;
return config.prerender.onError;
}
}

Expand Down Expand Up @@ -75,7 +86,7 @@ export async function prerender({ config, entries, files, log }) {

const server = new Server(manifest);

const error = normalise_error_handler(log, config.prerender.onError);
const error = normalise_error_handler(log, config);

const q = queue(config.prerender.concurrency);

Expand Down