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: add $app/state module #13140

Merged
merged 38 commits into from
Dec 16, 2024
Merged

feat: add $app/state module #13140

merged 38 commits into from
Dec 16, 2024

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Dec 10, 2024

Closes #10761

This adds a new $app/state module, which is basically the same as the existing $app/stores module but using Svelte 5 state. (Needless to say, it only works with Svelte 5.)

This brings many benefits:

  • no more weird $ prefix! you basically don't need to learn stores any more, except for dealing with legacy code
  • no more weird timing stuff. Because store subscribe callbacks are synchronous, it's possible to get into a weird state where e.g. $page.url updates before navigation occurs. We jump through weird hoops (the notifiable_store stuff) to prevent this from causing bugs in people's apps
  • as a corollary to the above, once we can get rid of $app/stores (which I'd like to do in SvelteKit 3), we'll no longer need to awkwardly pass stuff into the root component
  • properties are fine-grained. an update to page.data won't cause something that only cares about an unrelated property like page.status to re-run

Related PRs:

TODO:

  • figure out how to make it work for Svelte 4 apps
  • do the same thing for navigating and updated
  • create a sv migrate task to automate the migration
  • use said migration to update all the tests in this repo
  • deprecate everything in $app/stores (the sooner we do this, the less disruptive it'll be when we remove them)
  • update the tutorial
  • update the docs

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.

Copy link

changeset-bot bot commented Dec 10, 2024

🦋 Changeset detected

Latest commit: cf41f7e

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

@Rich-Harris
Copy link
Member Author

preview: https://svelte-dev-git-preview-kit-13140-svelte.vercel.app/

this is an automated message

@dummdidumm
Copy link
Member

Some thoughts on the migration:

  • either we do this very simple string-based. This will likely work for 95% or more of all use cases, because of the unique constraints that work in our favor here:
    • check if there's a page import from $app/store
    • make sure we're in runes mode by searching for clear indicators like $props() or $state( or $derived( or $effect(
    • make sure that page as-is is not used
    • if not, then replace all $page with page
  • or we do it in Svelte core as part of the migrate function
    • add a new parameter migration to specify which migration specifically you want (default to "do all of them")
    • if migration: 'page store' then do the migration for the page store specifically
    • we can make use of more elaborate static analysis here; since we now support TypeScript it should run without issues for basically everyone

dummdidumm added a commit to sveltejs/cli that referenced this pull request Dec 12, 2024
A simple, string-based migration for sveltejs/kit#13140 (tested on that repo, zero false-positives, less than five false negatives)
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
documentation/docs/20-core-concepts/10-routing.md Outdated Show resolved Hide resolved
documentation/docs/20-core-concepts/20-load.md Outdated Show resolved Hide resolved
documentation/docs/20-core-concepts/30-form-actions.md Outdated Show resolved Hide resolved
documentation/docs/20-core-concepts/50-state-management.md Outdated Show resolved Hide resolved
documentation/docs/30-advanced/25-errors.md Outdated Show resolved Hide resolved
documentation/docs/30-advanced/67-shallow-routing.md Outdated Show resolved Hide resolved
documentation/docs/30-advanced/67-shallow-routing.md Outdated Show resolved Hide resolved
packages/kit/src/exports/public.d.ts Outdated Show resolved Hide resolved
@Rich-Harris Rich-Harris merged commit f2393eb into main Dec 16, 2024
14 checks passed
@Rich-Harris Rich-Harris deleted the app-state branch December 16, 2024 15:00
@github-actions github-actions bot mentioned this pull request Dec 16, 2024
Rich-Harris added a commit to sveltejs/cli that referenced this pull request Dec 16, 2024
* feat: add page-state migration

A simple, string-based migration for sveltejs/kit#13140 (tested on that repo, zero false-positives, less than five false negatives)

* handle aliases

* add migration to docs

* update link

* lint

* rename to app-state

* rename

* remove .current from navigating references

* hopefully this will get it to shut up

* add migration task

---------

Co-authored-by: Manuel Serret <mserret99@gmail.com>
Co-authored-by: Rich Harris <rich.harris@vercel.com>
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.

Expanded support for Svelte 5
4 participants