Skip to content

feat: Fail prerendering for pages with mutative endpoints - fixes #3410 and #4252 #4812

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

Merged
5 changes: 5 additions & 0 deletions .changeset/brave-jobs-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

feat: Pages marked for prerendering fail during ssr at runtime
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function load_node({
/** @type {import('types').SSRPage} */ (route),
event,
options,
!!state.prerender
node.module.prerender ?? state?.prerender?.default ?? false
)
: {};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script context="module">
export const load = ({ error, status }) => {
return {
props: {
title: `${status}: ${error.message}`
}
};
};
</script>

<script>
export let title = 'Unknown error!';
</script>

<h1>{title}</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script context="module">
export const prerender = true;
</script>

<h1>I have a mutative endpoint!</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const post = () => {
return {};
};
9 changes: 9 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,15 @@ test.describe.parallel('Errors', () => {
expect(response.status()).toBe(500);
expect(await response.text()).toMatch('thisvariableisnotdefined is not defined');
});

test('prerendering a page with a mutative page endpoint results in a catchable error', async ({
page
}) => {
await page.goto('/prerendering/mutative-endpoint');
expect(await page.textContent('h1')).toBe(
'500: Cannot prerender pages that have endpoints with mutative methods'
);
});
});

test.describe.parallel('ETags', () => {
Expand Down