Skip to content

Post requests to page endpoints seem to call the get handler as well: it's either a bug or undocumented #5075

Closed
@cdcarson

Description

@cdcarson

Describe the bug

When you have a page endpoint with both a get method and a post method, a POST request will result in the get method being called after the post 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:

These functions can, like get, return a body that will be passed to the page as props. Whereas 4xx/5xx responses from get will result in an error page rendering, similar responses to non-GET requests do not, allowing you to do things like render form validation errors[...]

There's nothing I see about get being called post, or the order in which things are merged (although this seems ok, with the result of the post 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

  • clarity and
  • making repetitive api/database calls (when I update a row in my db it returns the row -- why do I need to fetch it again in 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...

  SvelteKit v1.0.0-next.344

  local:   http://localhost:3000
  network: not exposed

  Use --host to expose server to other devices on this network


--- Start of request --
In get handler. The request method is: GET
In time consuming getKittenName api call.
--- End of request --

--- Start of request --
In post handler. The request method is: POST
In time consuming updateKittenName api call.
In get handler. The request method is: POST
In time consuming getKittenName api call.
--- End of request --

For completeness it also happens in prod (I'm using the node adapter)...

npm run build
npm run preview

Logs

No response

System Info

System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1
    Memory: 75.08 MB / 8.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
  Browsers:
    Chrome: 101.0.4951.64
    Safari: 15.5
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.48 
    @sveltejs/adapter-node: ^1.0.0-next.76 => 1.0.0-next.76 
    @sveltejs/kit: next => 1.0.0-next.344 
    svelte: ^3.44.0 => 3.48.0

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions