Skip to content

docs: add tip when mixing universal and server load #11822

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

theetrain
Copy link
Contributor

@theetrain theetrain commented Feb 10, 2024

Relates to #11458

  • Since modifying data to include non-serializable POJOs (such as components) will throw an error, this change provides a hint for users not to do that.

I came across an issue on Discord where someone tried using dynamic import() to load a component and pass it to data in the universal load:

export async function load({ data }) {
  data.component = (await import('$lib/components/Button.svelte')).default
  return data
  //     ^ error
}

This threw an error along the lines of "cannot return non-serializable POJOs" since data was being returned by the server load in +page.server.js. Hopefully my contribution to the docs helps point out this lesser-known nuance.

I see this as a nice-to-have, so I'll understand if you don't wish to merge it.


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:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Since modifying `data` include non-serializable POJOs (such as components) will throw an error, this change provides a hint for users not to do that.
Copy link

changeset-bot bot commented Feb 10, 2024

⚠️ No Changeset found

Latest commit: 144d21f

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

@@ -216,6 +216,8 @@ export async function load({ data }) {
}
```

> It's important to not modify `data` to include any non-serializable objects since it will be processed by the **server** `load` after the **universal** `load` returns.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[...] processed by the server load [...]"

Is not quite right. It is just that the result of the server load gets serialized for hydration, and you can modify this result (the data prop) in the universal load function (which runs right after the server load function and before the serialization of the server load function's result).

Maybe this bug could also be fixed if we serialize the server load function's result before running the universal load function.

@eltigerchino eltigerchino added the documentation Improvements or additions to documentation label Feb 15, 2024
@Conduitry
Copy link
Member

@eltigerchino solicited opinions about this in the maintainers' chat, and my opinion (which I see @PatrickG also commented with above) was that we should instead serialize the server load function data with devalue before sending it on to the universal load function, so it's not tainted with any of the subsequent mutations from the universal load.

@benmccann
Copy link
Member

I just noticed #12235, which if merged would close this PR, so that one should probably be reviewed first

@eltigerchino
Copy link
Member

I just noticed #12235, which if merged would close this PR, so that one should probably be reviewed first

Unfortunately, I couldn’t figure out how to serialise the server load data before running the universal load functions without some major refactoring. A simple fix would be to clone the server data object before passing it to the universal load but that’s not ideal.

@eltigerchino
Copy link
Member

eltigerchino commented Oct 10, 2024

closing this in favour of a new issue #12236 where the solution would be as mentioned in #11822 (comment)

@svelte-docs-bot
Copy link

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.

mutating the data prop in a universal load affects the data returned from a server load
5 participants