Skip to content

Bring back support for content negotiation (allow +page.svelte & +server.ts to co-exist) #5896

Closed
@CaptainCodeman

Description

@CaptainCodeman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions