Replies: 1 comment 14 replies
-
@yidongw let me see if I am understanding correctly: You have data fetching logic defined in your store, and you want to prefetch that data during build time so that it is available during runtime (without the FE needing to refetch it); and you want to do this without duplicating the data fetching logic in order to use it from node. First of all, you cannot access a store instance that you've created in You can, however, use the same store logic in By doing this, you can create a store instance in node and run the data fetching api. You will then be creating a new store instance inside of You could:
Probably one of the best ways is to push the data into the GraphQL layer because using a static query means your data will be available during SSR and it will also be split into its own data chunk for consumption in the browser, reducing your initial bundle size. This can lead to more maintenance though due to the nature of GraphQL, so I understand if you want to avoid it. IIRC, there's a way to do the webpack alias method that will let you load a json chunk synchronously during SSR but also load it in its own chunk (asynchronously) during runtime, much like the static query process does. It's been a while since I've tried to do that though, so I'd have to try to put together a POC to even remember how. If any of these methods sounds appealing or if I've misunderstood what you're trying to accomplish, let me know. If you want to move forward with one of these, maybe you can share your project and we can make it work. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using mobx-state-tree for storing data, and it contains some product data that doesn't change very often, so I want to get those data ready in the build time.
I implemented
wrapRootElement
in gatsby-ssr.js and gatsby-browser.js.wrapRootElement
is the same function that creates a store and wrapping the element with context providerI also need to
createPage
in gatsby-node.js according to using-gatsby-without-graphql, but all the data fetching is currently done in the store, so in order to fetch the data, I need to somehow access the store that was created in the gatsby-ssr.js or gatsby-browser.js.How am I supposed to do that? Or I should use a different approach?
Beta Was this translation helpful? Give feedback.
All reactions