Skip to content

Separate Load Functions for Server and Browser #10004

@jdgamble555

Description

@jdgamble555

Describe the problem

Right now there is not a way to separate them. We can run the same code in browser-only, server-only, or both, but there is no way to run SEPARATE code in both server-only, browser-only versions.

Most database fetches cannot be run in the browser. This can be fixed by using just +page.server.ts, however, this forces a new database call from the server on each route reload. What if I want DIFFERENT code depending on if it is a server or browser call? This is currently impossible. You can't use the if (browser) option, as it will load the same code and packages for the server, which you don't need or can't use in the browser.

The only work-around is to completely move your server load function code outside of your load function, and create and endpoint. That way you can do: if (!browser), fetch data from the endpoint. This is extremely problematic, as you are forced to create an endpoint for something that is already loading on the server. This enables access to your data from an endpoint, which you don't want. Plus, with endpoints, you lose type-safety like you have with PageData or $page.

Describe the proposed solution

The most logical solution which would allow backwards compatibility is to allow BOTH the +page.server.ts file AND the +page.ts files. If it is the initial load, it will run +page.server.ts, and every call to the component afterwards will run the +page.ts file.

Alternatives considered

  • Another option maybe a +page.browser.ts file.
  • You could also add a export const separate = true
  • Allow two load functions in a +page.ts file (loadBrowser, loadServer)

Importance

would make my life easier

Additional Information

I can use SvelteKit without it, but I cannot use it correctly without an extraneous endpoint.

A good example of this is a feed. When I click on the item on the feed, I should not have to refetch that item if it is already in memory. There is no way to do this without an extraneous endpoint if I can only fetch the data on the server. With Firebase, for example, this could also potentially add extraneous read costs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions