Skip to content
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

account for POST bodies when serializing fetches #1385

Merged
merged 2 commits into from
May 9, 2021
Merged

Conversation

Rich-Harris
Copy link
Member

fixes #633. Figured the pragmatic choice is to ignore requests with a non-string body (as in, they don't get serialized; those fetches are repeated in the client) since other kinds are unlikely in load (presumably the main situation where this is useful is with GraphQL, which uses a JSON payload) and it would be undesirable to buffer streams, etc.

Handling other kinds of bodies is a TODO in any case:

const rendered = await respond(
{
host: request.host,
method: opts.method || 'GET',
headers,
path: resolved,
// TODO per https://developer.mozilla.org/en-US/docs/Web/API/Request/Request, this can be a
// Blob, BufferSource, FormData, URLSearchParams, USVString, or ReadableStream object
// @ts-ignore
rawBody: opts.body,
query: new URLSearchParams(search)
},

In fact, I think for now it might be best to throw an error if non-string bodies are sent in fetch in load. Better than silently failing. Though either way it should affect roughly zero people.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts

@benmccann
Copy link
Member

Additionally supporting JSON doesn't seem like it should be too hard and might be worth it if it helps add GraphQL support. Though presumably those users could just serialize the JSON to a string themselves before calling fetch

@Rich-Harris
Copy link
Member Author

JSON is a string. You don't post body: {...}, you post body: JSON.stringify({...})

@Rich-Harris Rich-Harris merged commit 2562ca0 into master May 9, 2021
@Rich-Harris Rich-Harris deleted the gh-633 branch May 9, 2021 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Post request via fetch gets overriden/replaced in load()
2 participants