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

Using nanostores in astro components #7936

Closed
lstephensca opened this issue Apr 18, 2024 · 4 comments · Fixed by #8223
Closed

Using nanostores in astro components #7936

lstephensca opened this issue Apr 18, 2024 · 4 comments · Fixed by #8223
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)

Comments

@lstephensca
Copy link
Contributor

lstephensca commented Apr 18, 2024

📚 Subject area/topic

Nanostores

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/recipes/sharing-state-islands/
https://docs.astro.build/en/recipes/sharing-state/

📋 Description of content that is out-of-date or incorrect

Currently, the docs pertaining to sharing-state-islands seem to advise against using nanostores in .astro components in the FAQ section. However, the docs for sharing-state demonstrates setting up nanostores for use inside .astro components with setting state/subscribing to changes. There appears to be a contradiction between the two.

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

@lstephensca lstephensca added the improve documentation Enhance existing documentation (e.g. add an example, improve description) label Apr 18, 2024
@TheOtterlord TheOtterlord added help - confirm behaviour Walk through the example/issue and confirm this is a general behaviour, or a correct update to make. help wanted Issues looking for someone to run with them! labels Apr 18, 2024
@Fryuni
Copy link
Member

Fryuni commented Apr 18, 2024

I think that would benefit from some rewording, but they are not exactly contradicting.

Explaining:

Can I use Nano Stores in .astro files or other server-side components?

Nano Stores can be imported, written to, and read from in server-side components, but we don’t recommend it! This is due to a few restrictions:

  • Writing to a store from a .astro file or non-hydrated component will not affect the value received by client-side components.
  • You cannot pass a Nano Store as a “prop” to client-side components.
  • You cannot subscribe to store changes from a .astro file, since Astro components do not re-render.

That is referring to server-side components, on Astro components that means the frontmatter:

---
// This runs on the server, not recommended
import {flag} from './stores.js';

const flagValue = flag.get();
---

A <script> block in an Astro component is a plain client-side script generated by the Astro component, it is never executed on the server-side even when pre-rendering. Using nanostores there is not a problem.

---
---
<script>
// This runs on the client, perfectly fine
import {flag} from './stores.js';

const flagValue = flag.get();
</script>

@lstephensca
Copy link
Contributor Author

Good point! I should have clarified a bit more in my issue regarding the differences between using nanostores in frontmatter and script blocks and not have been in a rush.

@Fryuni Fryuni removed the help - confirm behaviour Walk through the example/issue and confirm this is a general behaviour, or a correct update to make. label Apr 18, 2024
@sarah11918
Copy link
Member

Thanks for the conversation here! I wonder if something like this would address the issue?

Existing:

Nano Stores can be imported, written to, and read from in server-side components, but we don’t recommend it! This is due to a few restrictions:

Change to something like...

Nano Stores can be used in <script> tags to share state between .astro components. However, Using Nano Stores in the frontmatter of server-side components is not recommended because of the following restrictions:

Would something like that help? If so, would someone like to make a PR to update with clearer guidance?

@Fryuni
Copy link
Member

Fryuni commented May 5, 2024

I think that is a great improvement!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants