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

feat: make client router treeshakeable #11340

Merged
merged 18 commits into from Jan 8, 2024
Merged

feat: make client router treeshakeable #11340

merged 18 commits into from Jan 8, 2024

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Dec 15, 2023

This removes the create_client method in client.js in favor of having everything as exported functions at the top level. Slight rearrangements to make some things lazy or put it behind a BROWSER ? condition are necessary, but otherwise the code is almost completely untouched. This makes it even less likely we can ever unit test the client router, but I think that ship has sailed a long time ago and e2e tests are a much more robust and an outright better way to test this, so it's a non-issue.

A hello world SvelteKit app shrinks by 2 kilobytes (uncompressed) from doing this.

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

This removes the `create_client` method in `client.js` in favor of having everything as exported functions at the top level. Slight rearrangements to make some things lazy or put it behind a `BROWSER ?` condition are necessary, but otherwise the code is almost completely untouched. This makes it even less likely we can ever unit test the client router, but I think that ship has sailed a long time ago and e2e tests are a much more robust and an outright better way to test this, so it's a non-issue.
Copy link

changeset-bot bot commented Dec 15, 2023

🦋 Changeset detected

Latest commit: 706db8a

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

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

@dummdidumm dummdidumm marked this pull request as draft December 15, 2023 15:17
@dummdidumm
Copy link
Member Author

dummdidumm commented Dec 15, 2023

Mhm the test failure is legit, since there are two apps loaded embedded-style which share the same global stuff that's overriding each other. It was added due to #9576 - although I'm wondering if the test that was added really reflects what happens in the real world. In the NYT case these are probably separate SvelteKit apps. I don't see how they wouldn't clobber each other as soon as you use beforeNavigate etc because those are definitely not encapsulated (I tested this and indeed beforeNavigate would be called in both). Also, things like states etc are shared across instances in the prior version, too.

Therefore, I'm leaning towards going ahead with the change people who inline multiple SvelteKit apps into their site will most likely create separate bundles. Are there other ways to enforce separate instances?

@dummdidumm
Copy link
Member Author

POC for potential solution: force deduplication of modules by appending a query string when detecting embedded mode. Ideally each embedding has a unique but stable ID - no idea how that could look like.

@dummdidumm
Copy link
Member Author

Linking to #9808 just to double check if that wouldn't happen again (I don't think so)

use an increasing id -> things are still cached, but dynamically, and Vite plays ball, too
@dummdidumm
Copy link
Member Author

dummdidumm commented Jan 5, 2024

This is ready for review, with #11521 being another optimization which saves another 1kb compressed / 3kb uncompressed because of the no longer needed devalue functions. Total savings of both PRs combined would be 2kb compressed and 5 kb uncompressed, with #11521 being the more impactful one.

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
@benmccann
Copy link
Member

@dummdidumm heads up that there's a merge conflict on this now

* chore: apply state to history.state directly

The serialization capabilities of history.state are basically identical to those of devalue.stringify . As such, the indirection of saving the data in the session storage isn't necessary, which also saves 1kb compressed / 3kb uncompressed because of the no longer needed devalue functions

* Apply suggestions from code review

---------

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
* reduce indirection

* fix

* tweak error messages

* sigh

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
* remove all the embedded hackery

* ugh

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
@Rich-Harris
Copy link
Member

Fantastic work. I think this'll probably unlock some broader improvements in how the code is organised but we can look into those later

@Rich-Harris Rich-Harris merged commit aabe33c into main Jan 8, 2024
13 checks passed
@Rich-Harris Rich-Harris deleted the treeshakeable-client branch January 8, 2024 23:57
@github-actions github-actions bot mentioned this pull request Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants