Method overrides
See original GitHub issueI could have sworn we’d already talked about this at some point, but I guess not.
Is your feature request related to a problem? Please describe.
Bewilderingly, the only valid <form>
methods are GET and POST — in other words <form method="put">
is invalid, and will result in form.method === 'get'
.
A popular workaround is to use a method override query string parameter, like so:
<form method="post" action="/todos/{id}?_method=put">
<!-- form elements -->
</form>
(As evidence for the validity of this approach, there’s an official Express middleware for it: https://www.npmjs.com/package/method-override).
It would be nice if such a thing existed in SvelteKit as well, since we want to encourage people to build apps that work with JavaScript disabled, and that’s only really possible if you can use PUT and DELETE alongside GET and POST without having to resort to fetch
.
Describe the solution you’d like
Since the ?_method=put
cowpath already exists, I propose that we bake it into SvelteKit, while making it configurable:
// svelte.config.cjs
module.exports = {
kit: {
methodOverride: {
key: '_method',
allowed: ['post', 'put', 'delete']
}
}
};
Then, when any request that comes in where query.has(override)
, we change the request method before passing it to handle
.
How important is this feature to you? It would be possible to do in userland…
export function handle({ request, render }) {
return render({
...request,
method: request.query.get('_method') || request.method
});
}
…but I think this is the sort of thing that belongs in the framework.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:17
- Comments:20 (15 by maintainers)
Top GitHub Comments
Huh. I find it much grosser to update or delete a resource with a POST request. If HTTP methods mean anything at all, then
POST /items/xyz/update
means ‘create a new resource of the type/items/xyz/update
’, which is incoherent.Gross 😅
Better to scaffold a server-side
POST /items/:id/update
endpoint imo. It can even be an alias to the same handler forPUT
route.Methods and status codes are fundamental building blocks. Paths can be reworked if need be. And there’s a much deeper cow path for shuttling everything thru POSTs if needed.