Prerendering with +server.js blocks cURL requests
Describe the bug
When attempting to cURL a preview build of SvelteKit that uses prerender within a +server.js file a 405 Method not allowed is returned. However when providing an extra "Accept" header 200 OK is returned, and when using +page.server.js instead a 200 OK is returned.
Example repository linked below.
Reproduction
https://github.com/dextermb/svelte-kit-prerender-405-server-js
Logs
No response
System Info
npx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"
Need to install the following packages:
[email protected]
Ok to proceed? (y) y
System:
OS: macOS 13.4
CPU: (12) arm64 Apple M2 Max
Memory: 79.95 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
Browsers:
Chrome: 113.0.5672.126
Firefox: 110.0.1
Safari: 16.5
Severity
annoyance
Additional Information
No response
I think you may have forgotten a commit in your repro -- there's no +server file that I could find.
I think you may have forgotten a commit in your repro -- there's no
+serverfile that I could find.
Hey, it's part of the set up steps [see the README.md]. Just simply run:
echo 'export const prerender = true' > 'src/routes/+server.js'
Ahhh, okay, yeah -- I don't believe this is a bug. There are two things at play here:
- You can't prerender
+serverfiles with mutative methods (POST, for example) -- this doesn't apply here, but does apply to the above PR- @gtm-nayan In light of this, I'm not sure if your PR is necessary -- it certainly shouldn't be for prerendering, though if it fixes some undiscovered bug for runtime code, we might still want to merge it
- You can't prerender a route with both a
+serverfile and a+pagefile. This is because content negotiation is impossible for prerendered files -- there's no server logic to disambiguate a request to/foousing theAcceptheader- I think you're seeing mixed success here right now because the preview server is still performing this content negotiation for you when it shouldn't be?
This PR will throw an error when you try to do this: https://github.com/sveltejs/kit/pull/9994/files
This makes sense now I read it. I wonder if there's a way we can better document / make this obvious. I feel like adding actual documentation around it might cause confusion when this is very much a niche issue.
It's unfortunate that preview isn't working as-per vercel, but I'm not sure what we can do about this - Vercel does some magic hosting pre-rendered files directly on the edge, whereas I assume the node adapter would not, so perhaps we have to put this down to adapter specifics and accept that preview will always behave a bit more like the node adapter than anything else.
Closed via #11256, having conflicting route definitions / both a +page.js and +server.js when prerendering is an error in SvelteKit 2.0