Description
Describe the problem
Previous versions of SvelteKit supported content negotiation where a route could serve HTML to browsers or JSON data if an accept
header was used for a data request (as well as supporting data being posted to the same route).
Existing sites may already use this, it's been part of HTTP since forever. You might want to replace such a site with SvelteKit (or previously relied on SvelteKit supporting this) but changing the existing URL structure would be problematic and impactful (other systems using endpoints, pages already indexed and publicized etc...)
I think a router should be flexible enough to support existing URL structures, even if they are not the favoured default for the framework.
Describe the proposed solution
Support the accept
header in the routing and allow application/json
requests to an endpoint at the same route. Pages should only be concerned with GET requests with no accept
header (or one for html content).
Alternatives considered
Adding a load function to +page.server.ts
allows a GET request for data, but makes it difficult to control the response and would limit things to JSON (maybe you want CSV, PDF, or XML). It also appears limited in terms of what control you have over the response (http headers etc...) and still leaves you unable to use other HTTP methods.
Update: it appears that I should be treating load
as GET and the other methods are supported. However they are inconsistent in terms of signature and what response is returned.
Importance
i cannot use SvelteKit without it
Additional Information
No response