kit icon indicating copy to clipboard operation
kit copied to clipboard

docs: add tip when mixing universal and server load

Open theetrain opened this issue 1 year ago • 6 comments

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.

theetrain avatar Feb 10 '24 19:02 theetrain

⚠️ No Changeset found

Latest commit: 144d21f9be23b912b25f549fe3115c53a95f54fc

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

changeset-bot[bot] avatar Feb 10 '24 19:02 changeset-bot[bot]

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

Conduitry avatar May 18 '24 17:05 Conduitry

I just noticed https://github.com/sveltejs/kit/pull/12235, which if merged would close this PR, so that one should probably be reviewed first

benmccann avatar Jun 19 '24 20:06 benmccann

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.

teemingc avatar Jun 20 '24 01:06 teemingc