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

docs: state management #8547

Merged
merged 26 commits into from
Feb 28, 2023
Merged

docs: state management #8547

merged 26 commits into from
Feb 28, 2023

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Jan 16, 2023

  • docs about state management
  • more helpful error message when trying to run a SvelteKit store on the server

closes #8524
closes #8302
related to #8614

The docs are WIP, I'd like to add more stuff to it.

  • maybe move the explanation about cookies from Loading Data in there?
  • touch on invalidation as a method to handle state
  • in general a bit more indepth about the global gotchas, maybe more code examples?
  • other thoughts?

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

- docs about state management
- more helpful error message when trying to run a SvelteKit store on the server

closes #8524
@changeset-bot
Copy link

changeset-bot bot commented Jan 16, 2023

⚠️ No Changeset found

Latest commit: 978ac41

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


- if you need to access the user state only inside `load` functions, use `locals` in server `load` functions
- if you need to persist the user state across reloads, but only need to access it inside `load` functions, use `cookies` in server `load` functions
- if you need to access and update the state inside components, use Svelte's [context feature](https://svelte.dev/docs#run-time-svelte-setcontext). That way, the state is scoped to components, which means they are not shared across different requests on the server. The drawback is that you can only set and subscribe to the store at component initialization. SvelteKit's stores from `$app/stores` for example are setup like this (which is why you may have encountered a related error message)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could it be useful to have an example of this here ?
I am not sure myself what is meant with this, but are you referring to returning data from load, placing it in a store and then use setContext in the page to make it available ?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes this would be the way - and I agree this probably needs a code example

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

maybe move the explanation about cookies from Loading Data in there?

I think the cookies docs make sense in Loading Data where they are now, but we should certainly link to them

@Rich-Harris
Copy link
Member

Agree that we should clarify why you can't subscribe to app stores outside component initialization (though the improved error messages are the real win here), but I do think we should be careful about how far into the weeds we go. For example the 'types of state' section feels a bit intimidating ("I need to understand all these different kinds of things? I just wanted to load some data") and yet is arguably incomplete (it doesn't mention stores, or how API/backend relates to page data).

There's a cost to adding documentation — it makes it harder for a newcomer to RTFM — and so we need to be sure that we're solving a real problem. #8524 is painfully vague about our docs' shortcomings, to the point that I'm not sure any PR could claim to close it.

@benmccann
Copy link
Member

I agree the docs in this PR are too broad. It seems like the thing that people trip over a lot is how to use stores with SSR. I think we could cover that much more concisely. Perhaps we could document the pattern Rich recommended in #7105 (comment) in the load docs or something

@dummdidumm
Copy link
Member Author

dummdidumm commented Jan 20, 2023

I'm good with removing the broader section, it was intended as a start to a more full-fledged docs page - imagine about twice the content there is now in the end. But I don't have a good vision of how it all could look like.

We already have a short section on stores and SSR in the load docs, which noone reads because noone thinks that it's hidden in there. I think a general "State Management" page makes sense since it's a widely used term so people are far more likely to click on that to see how things are done "the SvelteKit way".

#8614 is a prime example that we need a dedicated section for this.

@stephane-vanraes
Copy link
Contributor

Perhaps the docs should simple focus on the "API" instead.
And in addition you make a guides segment that goes a bit deeper in certain parts like state management.
NextJS has for example a more in detail section on how to make forms https://nextjs.org/docs/guides/building-forms
Remix also has a "Guides" section in their docs that cover things like data loading in more detail.

@dummdidumm
Copy link
Member Author

I update the doc to remove that "types of state" prelude and added a section on form interaction instead. It has reminiscents of a trouble shooting or "do it this way instead" which I think is a good addition.

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

The forms stuff feels out-of-place in the state section to me. It seems like it belongs much better on the form actions page.

We've really got to get the PR doc deploys fixed 😄

@dummdidumm
Copy link
Member Author

I was debating this myself, there's duplication to the form actions section, but if you browse the docs just from looking at the titles, you may end up here searching for the best way to do this. It's a little like the error docs which also contain references to various other places in the docs.

Comment on lines 69 to 71
## Managing forms

When coming from a pure Svelte or JavaScript background, you might be used to handling all form interactions through JavaScript. This works well when JavaScript is available but results in unresponsive UI when it isn't (which may be [more often than you think](https://kryogenix.org/code/browser/everyonehasjs.html)). If this is a concern to you, leverage SvelteKit's [form actions](form-actions) instead.
Copy link
Member

Choose a reason for hiding this comment

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

Does this belong here? It's well covered by the form section, and I don't think of mutations as part of state management

Suggested change
## Managing forms
When coming from a pure Svelte or JavaScript background, you might be used to handling all form interactions through JavaScript. This works well when JavaScript is available but results in unresponsive UI when it isn't (which may be [more often than you think](https://kryogenix.org/code/browser/everyonehasjs.html)). If this is a concern to you, leverage SvelteKit's [form actions](form-actions) instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Mutating state is an integral part of managing state to me. If all state was just readonly, managing it would be a lot easier 😄

@Rich-Harris
Copy link
Member

#9239

* updates

* fix broken link

* fix file annotations

* tweak

* Update documentation/docs/20-core-concepts/50-state-management.md

* Update documentation/docs/20-core-concepts/50-state-management.md

Co-authored-by: Rich Harris <[email protected]>

* Update documentation/docs/20-core-concepts/50-state-management.md

---------

Co-authored-by: Simon H <[email protected]>
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.

Sveltekit documentation is not beginner friendly at all. load functions runs on both server and client
6 participants