kit
kit copied to clipboard
Having a +server.js file caused 405
Describe the bug
I had an on-going issue with meta-tags not rendering on social platforms, as well as possible indexing problems.
One of my pages was returning 405- even though there was nothing unusual about it.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>
<body><p>GET method not allowed</p></body>
</html>
For example:
https://colorschemer.com/schemes returns 405 https://colorschemer.com/schemes/red worked fine
I was using Facebook debugger to test changes
I ended up trying different deployment solutions, going in circles with configurations, and playing with rendering options. In the end, the problem was just the presence of the +server.js file
Directory structure was:
- /schemes
- [slug]
- +server.js
- layout, page, etc
The file had
// export const prerender = true;
// export const ssr = true;
I would try uncommenting one at at time, but ultimately, just removing it fixed the problem
Reproduction
Put an empty +server.js file, deploy and test with Facebook debugger
Logs
No response
System Info
Netlify build
Severity
annoyance
Additional Information
No response
I'd argue that the current behavior is correct, if perhaps not useful. If you have a +server
file, it's expected to export methods for all of the HTTP verbs you want to support - and if you don't export any methods, the endpoint doesn't support any verbs. I'm marking this as something to possibly consider for SvelteKit 3.0.
We could add a warning, something like "you didn't add any verbs, are you sure this is correct?" - not sure how you would be able to silence it though (or would you even want to? In which situation would this be the wanted behavior?)
If there is no export, maybe it should ignore it, otherwise, an empty export can do it?
It did confuse me for days as other pages worked fine. I had assumed that the settings for server.js would apply to sub-directories but it does not look to be the case
I was trapped is this for days.
When having an +server.js with only a POST method in it. The +page.svelte work just fine if the request has an accept header. If not it return 405 method not allowed.