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

Allow server-only load functions to return more than JSON #6318

Merged
merged 25 commits into from
Aug 29, 2022
Merged

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Aug 26, 2022

closes #6008
fixes #6357

This allows load functions to return things like Date objects, regexes, Map and Set, BigInt and so on. It also allows repeated and cyclical references, for the times when they're useful.

TODO:

  • docs
  • finish updating tests

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. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Aug 26, 2022

🦋 Changeset detected

Latest commit: 5eafe2c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@sveltejs/adapter-netlify Patch
@sveltejs/adapter-vercel Patch
@sveltejs/kit Patch
@sveltejs/adapter-auto Patch

Not sure what this means? Click here to learn what changesets are.

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

@Conduitry
Copy link
Member

Did you have an opinion about a statically analyzable config for making a given page use JSON instead of Devalue? Do you think the performance impact is not big enough to warrant that?

@Rich-Harris
Copy link
Member Author

I think it's probably fine — for all but the most complex objects, we're talking fractions of milliseconds. One thing devalue could do to reduce the overhead is, after it completes its initial walk of the object, return JSON.stringify(data) if it didn't encounter any non-JSON-able values (or repeated references of objects or arrays). Though the resulting data will be larger as a result, so it's probably a wash.

@Rich-Harris
Copy link
Member Author

No, I'm afraid that's probably a non-starter. The code generated by devalue needs to know how to reference that class in the client. It would involve jumping through some absurd hoops. Just take care of it on the server

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

This looks very nice! Looking at the code I think it may even be possible to make this configurable to some extend. The window method stays but what kind of format is written to it could be configured by the user (JSON, superjson, whatever). Not for now but thought for later.

packages/kit/src/runtime/client/client.js Show resolved Hide resolved
packages/kit/src/runtime/server/page/render.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/server/page/index.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
@dummdidumm dummdidumm merged commit c530d33 into master Aug 29, 2022
@dummdidumm dummdidumm deleted the gh-6008 branch August 29, 2022 15:59
@0gust1
Copy link
Contributor

0gust1 commented Aug 29, 2022

Oh. Yeah. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants