-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Post requests to page endpoints seem to call the get handler as well: it's either a bug or undocumented #5075
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
Comments
This is intended, as it's (generally) the more expected behavior. I believe there has been at least one issue opened previously about this, but I couldn't immediately find it. If you want your |
Thank you for the detailed repro! One thing I can say for sure is that this is intended behavior. See the issue that spawned page endpoints (called "shadow endpoints" during development): #3532 - Rich lays out this behavior in the second(?) section. Keep in mind, this is all done in one roundtrip to the server -- you're not making a networked POST call, receiving the result, and then making a networked GET call, with the bodies being merged. @Conduitry's workaround absolutely works, but from a semantic perspective, if you want a POST endpoint separate from the GET one, I would just use a dedicated endpoint (eg. Absolutely agreed that this could be documented better. |
Aah. Ok. Thanks. One question -- what's the proper way to bail? With nothing or with |
By, the way, right under that paragraph is this code snippet: <!-- src/routes/items.svelte -->
<script>
// The page always has access to props from `get`...
export let items;
// ...plus props from `post` when the page is rendered
// in response to a POST request, for example after
// submitting the form below
export let errors;
</script> |
Ok, thanks @Conduitry @tcc-sejohnson and @babichjacob (special props for pointing out that I never ever read anybody else's code comments, sorry about that. 👍 ) To expand on what @Conduitry said about bailing...
...this is how to bail... export const get: RequestHandler = async (event) => {
if (event.request.method !== 'GET') {
return {};
}
console.log('In get handler. The request method is:', event.request.method);
const kittenName = await getKittenName();
return {
status: 200,
body: {
kittenName,
testIfPropOverriddenInGet: 'set in get method',
method: event.request.method
}
}
} This keeps the response just as it was returned by the I'm fine with closing this issue, but I'll leave it up to you all. Thanks again. |
This comment was marked as off-topic.
This comment was marked as off-topic.
might be safer to export const get: RequestHandler = async (event) => {
if (event.request.method !== 'GET' && event.request.method !== 'HEAD') {
return {};
} automatic handling of HEAD requests does call the get handler and leaves out body in the end (otherwise we'd be unable to obtain the same headers as GET) |
Closing as everything is working as intended — I agree that there's probably scope for more documentation around this, but that's the sort of thing that the tutorial will cover when we get round to writing it |
Just a note about how this behavior is a subtle gotcha if you aren't aware of it and your handlers have side effects like setting cookies. In my case it was setting a CSRF cookie (i.e. a plaintext token hashed with a secret.) If the |
Describe the bug
When you have a page endpoint with both a
get
method and apost
method, aPOST
request will result in theget
method being called after thepost
method, with the response bodies from both methods being merged.This may be the intended (though confusing and potentially costly) behavior. If so, it isn't documented, as far as I can tell. I'm looking here:
There's nothing I see about
get
being calledpost
, or the order in which things are merged (although this seems ok, with the result of thepost
not being entirely overwritten.)It's unclear whether this merging occurs with the response headers as well. (I tried but failed to figure this out in my repo below.)
Ideally, for me, this wouldn't happen at all. Yeah, maybe a few lines of code can be saved by the inheritance, but it's at the cost of
get
? At that point a lot of the benefit of page endpoints goes away.)If it is the intended behavior, then it should be documented so people can work around it, probably by avoiding page endpoints altogether.
But page endpoint methods are nice, and I'd like to keep using them, if possible. The happy paradigm should be that each operates independently, being responsible for returning the full set of route component properties.
Reproduction
A minimal reproduction of the behavior is here:
https://github.com/cdcarson/sk-issue-get-handler-called-after-post-handler
git clone https://github.com/cdcarson/sk-issue-get-handler-called-after-post-handler.git cd sk-issue-get-handler-called-after-post-handler.git npm i npm run dev -- --open
Open the site, submit the form on the home page, and look at what the server logs in the console. You will see...
For completeness it also happens in prod (I'm using the node adapter)...
Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: