I'm struggling to find a good solution to this - I have an astro app with a couple of pages and some react islands. I want to fetch some initial data on the server, pass it to these islands for the initial server-side render, but then also store this data in a store (lets say nanostores) so that client side the data can be mutated locally.
#nanostores client side hydration
59 messages · Page 1 of 1 (latest)
Inox Tools
Make your Nanostores concurrent safe and shared from server to client
Sorry for the long wait
@fallen stratus
no worries, thank you
hi @livid compass it works fine as expected but i don't know why i get this error when i try to import shared function Cannot find module '@it-astro:request-nanostores'
anyway it's fine, i just disturbed about this error
Did you install the package with your package manager?
Can you run astro info and share the results?
yes
Just figured I would double check 😅
@upper siren so?
No need for the extra ping, they're obviously aware of the thread 😛
maybe. anyway even it works but the build is failed cuz of this error 🫤
It should be working, it really only needs to be installed to define the module. Might be related to a problem we've been debugging related to virtual modules defined by integrations.
Can you share you astro.config.mjs?
This is a problem we've seen some people have but so far none of us trying to investigate it manage to reproduce it
here is it
What about the other integrations? Does it fail even with just the request-nanostore integration?
no the only error it's from the store just this module '@it-astro:request-nanostores'
I tried with that exact config and same versions you are using and it is working fine.
It is such a weird error we can't replicate to debug
out of curiosity what does your store look like?
here is my store and my config with all integrations
Do you get the error if it's a .ts file? no real need for it to be tsx
Wait
Is the error on the IDE or when you run the build?
Does it break when running astro build?
That output is not from astro build it is from astro check
You are probably running npm run build and it has astro check && astro build
yes i run npm run build
If you created from a template there should be an astro script. Can you try running npm run astro build so it building without typechecking?
If that works then it is an entirely separate problem from what we are debugging. It's just a type thing.
Rename your astro.config.mjs to astro.config.mts. Your tsconfig is not loading the types brought in by your Astro config
i just run npm run astro build so i didn't get any error
i rename it and i still get the same error!!
Now that is weird. It should work. Can you share your tsconfig?
I have an idea on how to fix this so people don't have to deal with that
Got it
Your type checking module resolution is not matching what happens for the build. Astro bundles your code so you need "moduleResolution": "bundler"
This is set on the base config you are extending from, so you can just remove that line as well
The node resolution won't load the virtual modules because they are incompatible with Node, without Vite they wouldn't exist
@upper siren Did I understand correctly that with this integration I can access my store even in server-side .astro components? If yes, could you please provide a minimal example? I need to get data from the store and display it on a page
#showcase message
I need to get data from the store and display it on a page
Be aware that the data only goes one way. Whatever you put in your store on the server will be available on the client, but if you change it on the client it won't be available on following requests to the server.
For that you'll need to use some other mechanism, like cookies or a session database, to keep the state between multiple requests.
I have an example on the repo doing just that: https://github.com/Fryuni/inox-tools/tree/main/examples/request-nanostores
Here is how it works:
- There is Astro Action that receives a cart item and set's it into a cookie
- When you open the page, the server sets the nanostore to be the value of the cookie
- The component that adds an item to the cart calls a function that creates a nanostore task. That task will update the nanostore locally for client reactivity and call the action to update the state for the server.
my task is to create just a global store where I will send data in the form of json, which I receive in a .astro file in the script tag. But for some reason when I view the store on other pages, it is empty
I don't need to change the store on the client in any way, I just want to write the different data that I receive from the components there once
But for some reason when I view the store on other pages, it is empty
The state sharing is per page. On each page you read what was set from the server. If the next page doesn't set the state, it won't be set
is there any simple method where I could write my data and then read and take it from anywhere?
You have two options:
- You can use Nano Stores Persistent which will persist the data you set on it to
localStorage, so you'll be sharing the information on the client-side. But if the user navigates directly to one of the pages that use the state and never to the page that sets the state things might break. - You can set the state of a shared atom from a middleware so it is set from the server on every page
thanks
If your issue is resolved, please help by doing the following two steps:
- From the ellipses (3-dot menu) in the top-right corner of the post (not the first message), edit the tags to include the Solved tag.
- From the same ellipses, select Close Post.
Your post will still be available to search and can be re-opened simply by replying in it. Closing a post moves it down with older posts, so we can more easily focus on issues that still need to be resovled.
Thank you for your help!
The issue was already solved