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

Prerendered page alongside non-GET request handler should be an error #4252

Closed
Rich-Harris opened this issue Mar 6, 2022 · 4 comments · Fixed by #4812
Closed

Prerendered page alongside non-GET request handler should be an error #4252

Rich-Harris opened this issue Mar 6, 2022 · 4 comments · Fixed by #4812
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Milestone

Comments

@Rich-Harris
Copy link
Member

Describe the bug

Related to #3294. I don't think there's a situation in which it would make sense to have a non-GET request handler alongside a prerendered page.

In the same vein as #4093 (comment), SvelteKit should throw an error if a prerenderable page (either export const prerender = true, or config.kit.prerender.default = true without a export const prerenderable = false) has an endpoint with non-GET methods.

Reproduction

Add a non-GET method to any page endpoint and mark the page as prerenderable: https://stackblitz.com/edit/sveltejs-kit-template-default-8la3qs?file=src%2Froutes%2Fabout.js&terminal=dev

Logs

No response

System Info

System:
    OS: macOS 12.0.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 169.98 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.1/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  Browsers:
    Chrome: 98.0.4758.109
    Firefox: 97.0.1
    Safari: 15.1
  npmPackages:
    svelte: ^3.43.0 => 3.44.2

Severity

annoyance

Additional Information

No response

@Rich-Harris Rich-Harris added the bug Something isn't working label Mar 6, 2022
@Rich-Harris Rich-Harris added this to the 1.0 milestone Mar 6, 2022
@benmccann benmccann added the p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. label Mar 17, 2022
@benmccann benmccann added help wanted PRs welcomed. The implementation details are unlikely to cause debate and removed help wanted labels Apr 4, 2022
@tadmccorkle
Copy link

@benmccann I was looking into this one - I might be misunderstanding the issue, but I think this is already implemented when building. The following can be observed with the StackBlitz link above by running npm run build.

Currently, building with either:

  • a page with export const prerender = true; with a non-get endpoint
  • config.kit.prerender.default = true and a page without export const prerender = false; with a non-get endpoint

results in the following error after Vite's build output:

Error: Cannot prerender pages that have endpoints with mutative methods
    at load_shadow_data (file:///[OMITTED]/kit-sandbox/.svelte-kit/output/server/index.js:1038:13)
    at async load_node (file:///[OMITTED]/kit-sandbox/.svelte-kit/output/server/index.js:828:28)
    at async respond$1 (file:///[OMITTED]/kit-sandbox/.svelte-kit/output/server/index.js:1241:22)
    at async resolve (file:///[OMITTED]/kit-sandbox/.svelte-kit/output/server/index.js:1584:105)
    at async Object.handle (file:///[OMITTED]/kit-sandbox/.svelte-kit/output/server/chunks/hooks-5b953b47.js:6:20)
    at async respond (file:///[OMITTED]/kit-sandbox/.svelte-kit/output/server/index.js:1540:22)
    at async visit (file:///[OMITTED]/kit/packages/kit/src/core/build/prerender/prerender.js:145:20)
> 500 /about
    at file:///[OMITTED]/kit/packages/kit/src/core/build/prerender/prerender.js:31:11
    at save (file:///[OMITTED]/kit/packages/kit/src/core/build/prerender/prerender.js:266:4)
    at visit (file:///[OMITTED]/kit/packages/kit/src/core/build/prerender/prerender.js:155:3)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

That error is thrown here: https://github.com/sveltejs/kit/blob/master/packages/kit/src/runtime/server/page/load_node.js#L389

So am I misunderstanding the issue and/or not seeing a flaw or gap in the current implementation? Or is there some other desired outcome from this issue, like a new test for this case or for the error to be thrown in a context other than when building?

@mrkishi
Copy link
Member

mrkishi commented Apr 13, 2022

@tadmccorkle I believe this is about displaying an error page during dev as well, as to minimize dev/build disparity.

@pto1026
Copy link

pto1026 commented Apr 21, 2022

I would like to tackle this issue, can you please assign me

@tadmccorkle
Copy link

tadmccorkle commented Apr 22, 2022

@pto1026 I tried looking into this one, so I wanted to let you know what didn't work for me - I probably just don't know the correct place to implement the fix.

I wanted to make the fix on the dev side of things, and the most appropriate place felt like the plugin.js file where the vite sveltekit plugin is defined. Adding a prerender property here (https://github.com/sveltejs/kit/blob/master/packages/kit/src/core/dev/plugin.js#L342-L348) got the error page to show if you refresh the browser on an offending page when the svelte config file specifies prerender by default, but it won't show upon navigation.

I tried adding logic to the server's respond function (https://github.com/sveltejs/kit/blob/master/packages/kit/src/runtime/server/index.js#L16), but had similar results - any changes I made only produced the error page on browser refresh, not a navigation.

I'm pretty new to the code base, so, again, I'm probably not looking in the right places. Either that, or the fix will touch more code than I was expecting.

Rich-Harris added a commit that referenced this issue May 23, 2022
… and #4252 (#4812)

* feat: Failing test

* feat: Prerendered pages with mutative endpoints fail at runtime

* feat: Changeset

* fix: clarify with variable

* determine prerenderability based on config, not state

* rename state.prerender to state.prerendering, remove state.prerendering.default

* fix type

Co-authored-by: Rich Harris <hello@rich-harris.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants