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

docs(load): clarify universal and server load #11458

Merged
merged 3 commits into from
Jan 9, 2024

Conversation

theetrain
Copy link
Contributor

(no linkable ticket)

  • Provides details and examples on how universal and server load runs, in what order, and what data gets returned.

Please don't delete this checklist! 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 and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Provide details and examples on how universal and server `load` runs, in what order, and what data gets returned.
Copy link

changeset-bot bot commented Dec 24, 2023

⚠️ No Changeset found

Latest commit: a0c8783

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Rich-Harris
Copy link
Member

I feel like this is already covered:

Universal `load` functions are called with a `LoadEvent`, which has a `data` property. If you have `load` functions in both `+page.js` and `+page.server.js` (or `+layout.js` and `+layout.server.js`), the return value of the server `load` function is the `data` property of the universal `load` function's argument.

In rare cases, you might need to use both together — for example, you might need to return an instance of a custom class that was initialised with data from your server.

I'm open to the idea that it could be improved but we should avoid being duplicative, or spending a significant amount of real estate on a somewhat rare use case

@theetrain
Copy link
Contributor Author

theetrain commented Dec 26, 2023

It is covered, but comes with unanswered questions:

  1. Are universal and server load methods called in parallel?
  2. Which load method's return value will be passed to export let data?
  3. Does the returned data between both loaders merge automatically the same way layout data does?
  4. Is there a similar await server() method like await parent()?

All of which are answered in this PR.

I also considered opening a feature request for await server() (akin to await parent()) but I realize it's better to keep existing behaviour—with both load methods being called one after the other—that way the universal load can obtain all data from the server load. And if a user really wants to parallelize server and universal load methods, they can appoint all async calls to the server load.

@dummdidumm
Copy link
Member

I'm ok with the text enhancements but I also don't know if the additional code snippet is worth it

@theetrain
Copy link
Contributor Author

@dummdidumm I suppose this line adequately explains it:

You can combine server and universal load returned data by spreading the universal load's data property

Though since this concept isn't covered elsewhere (such as the tutorial), I feel the code snippet is warranted. I often feel generous when it comes to showing examples of concepts, and the Kit docs are comprehensive enough that we get to decide whether deeper explanations should belong in the tutorial, guide, or API sections.

@Rich-Harris Rich-Harris merged commit b440214 into sveltejs:main Jan 9, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants