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/shaggy-ghosts-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: fail prerendering when remote function fails
8 changes: 7 additions & 1 deletion packages/kit/src/runtime/server/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,19 @@ async function handle_remote_call_internal(event, state, options, manifest, id)
});
}

const status =
error instanceof HttpError || error instanceof SvelteKitError ? error.status : 500;

return json(
/** @type {RemoteFunctionResponse} */ ({
type: 'error',
error: await handle_error_and_jsonify(event, state, options, error),
status: error instanceof HttpError || error instanceof SvelteKitError ? error.status : 500
status
}),
{
// By setting a non-200 during prerendering we fail the prerender process (unless handleHttpError handles it).
// Errors at runtime will be passed to the client and are handled there
status: state.prerendering ? status : undefined,
headers: {
'cache-control': 'private, no-store'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "prerenderable-remote-function-error",
"private": true,
"version": "0.0.1",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "svelte-kit sync",
"check": "svelte-kit sync && tsc && svelte-check"
},
"devDependencies": {
"@sveltejs/adapter-auto": "workspace:^",
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"svelte": "^5.38.5",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
"vite": "catalog:"
},
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body>
<div>%sveltekit.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const prerender = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { throws } from './data.remote.js';
</script>

{#await throws() then value}
{value}
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { prerender } from '$app/server';

export const throws = prerender(() => {
throw new Error('remote function blew up');
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import adapter from '../../../../../adapter-auto/index.js';

/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),

experimental: {
remoteFunctions: true
}
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true
},
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as path from 'node:path';
import { sveltekit } from '@sveltejs/kit/vite';

/** @type {import('vite').UserConfig} */
const config = {
build: {
minify: false
},

clearScreen: false,

logLevel: 'silent',

plugins: [sveltekit()],

server: {
fs: {
allow: [path.resolve('../../../../src')]
}
}
};

export default config;
12 changes: 12 additions & 0 deletions packages/kit/test/build-errors/prerender.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ test('entry generators should match their own route', { timeout }, () => {
`Error: The entries export from /[slug]/[notSpecific] generated entry /whatever/specific, which was matched by /[slug]/specific - see the \`handleEntryGeneratorMismatch\` option in https://svelte.dev/docs/kit/configuration#prerender for more info.${EOL}To suppress or handle this error, implement \`handleEntryGeneratorMismatch\` in https://svelte.dev/docs/kit/configuration#prerender`
);
});

test('an error in a `prerender` function should fail the build', { timeout }, () => {
assert.throws(
() =>
execSync('pnpm build', {
cwd: path.join(process.cwd(), 'apps/prerender-remote-function-error'),
stdio: 'pipe',
timeout
}),
/remote function blew up/
);
});
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading