Skip to content

Commit 33a8fad

Browse files
authored
fix: avoid suppressing errors when loading pages for ssr fails (#8813)
* fix: output errors if pages fail to compile * stupid javascript try catch syntax * changeset * less blatant copy paste, more code reuse * use superior casing and wrap a few more places * why did I change this
1 parent 8b0b92b commit 33a8fad

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

.changeset/curly-jobs-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: output errors properly if pages fail to compile

packages/kit/src/exports/vite/dev/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33
import { URL } from 'node:url';
44
import colors from 'kleur';
55
import sirv from 'sirv';
6-
import { isCSSRequest, loadEnv } from 'vite';
6+
import { isCSSRequest, loadEnv, buildErrorMessage } from 'vite';
77
import { getRequest, setResponse } from '../../../exports/node/index.js';
88
import { installPolyfills } from '../../../exports/node/polyfills.js';
99
import { coalesce_to_error } from '../../../utils/error.js';
@@ -50,11 +50,25 @@ export async function dev(vite, vite_config, svelte_config) {
5050
/** @type {Error | null} */
5151
let manifest_error = null;
5252

53+
/** @param {string} url */
54+
async function loud_ssr_load_module(url) {
55+
try {
56+
return await vite.ssrLoadModule(url);
57+
} catch (/** @type {any} */ err) {
58+
const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]);
59+
60+
vite.config.logger.error(msg, { error: err });
61+
vite.ws.send({ type: 'error', err: err });
62+
63+
throw err;
64+
}
65+
}
66+
5367
/** @param {string} id */
5468
async function resolve(id) {
5569
const url = id.startsWith('..') ? `/@fs${path.posix.resolve(id)}` : `/${id}`;
5670

57-
const module = await vite.ssrLoadModule(url);
71+
const module = await loud_ssr_load_module(url);
5872

5973
const module_node = await vite.moduleGraph.getModuleByUrl(url);
6074
if (!module_node) throw new Error(`Could not find node for ${url}`);
@@ -161,7 +175,7 @@ export async function dev(vite, vite_config, svelte_config) {
161175
(query.has('svelte') && query.get('type') === 'style')
162176
) {
163177
try {
164-
const mod = await vite.ssrLoadModule(dep.url);
178+
const mod = await loud_ssr_load_module(dep.url);
165179
styles[dep.url] = mod.default;
166180
} catch {
167181
// this can happen with dynamically imported modules, I think
@@ -191,7 +205,7 @@ export async function dev(vite, vite_config, svelte_config) {
191205
endpoint: endpoint
192206
? async () => {
193207
const url = path.resolve(cwd, endpoint.file);
194-
return await vite.ssrLoadModule(url);
208+
return await loud_ssr_load_module(url);
195209
}
196210
: null,
197211
endpoint_id: endpoint?.file

0 commit comments

Comments
 (0)