sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.47k stars 1.89k forks source link

Allow content-negotiation for POST requests too #10863

Open coyotte508 opened 11 months ago

coyotte508 commented 11 months ago

Describe the problem

I want to do a +server.ts with a POST handler that downloads a file (a JSON backup).

And I want it to be accessible without client-side javascript:

<!-- backup/+page.svelte -->
<form method="POST" action="/backup">
  <input type="submit">
</form>
// backup/+server.ts

export const POST = () => {
  return new Response(JSON.stringify('{}'), {
    headers: {
      'Content-Type': 'application/json',
      'Content-Disposition': 'attachment; filename="backup.json"'
    }
  })
}

Unfortunately, there is a 405 error message with the message:

POST method not allowed. No actions exist for this page

This, despite the lack of +page.server.ts / actions.

Describe the proposed solution

When there is no actions for a page, allow the POST from the native browser form to hit +server.ts's POST handler.

This would be consistent with the GET behavior.

Alternatives considered

Currently, workarounds are using client-side javascript, or redirecting to a GET page in the action's handler.

A possible alternative feature is allow downloading files in an action handler.

Importance

nice to have

Additional Information

No response

eltigerchino commented 11 months ago

The content negotiation seems to be working as intended, but our route handling logic doesn't check if any actions exist first before skipping past our endpoints.

On a side note, this is what the accept header look like for a native form submission request:

text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7