#Store data globally in project

3 messages · Page 1 of 1 (latest)

ornate needle
#

Is there a way to store data and be able to use it everywhere in the project on astro ?

quiet bearBOT
#

Hi @ornate needle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can use Nano Stores to share state between components in your Astro project. Nano Stores is a lightweight, framework-agnostic library that allows you to create stores that any component can interact with. Astro recommends Nano Stores for sharing client-side storage in an islands architecture / partial hydration (https://docs.astro.build/en/concepts/islands/) setup.

You can find more information about Nano Stores and why Astro recommends it in the Sharing State documentation (https://docs.astro.build/en/core-concepts/sharing-state).

Keep in mind that Nano Stores is recommended for client-side storage. If you need to fetch data from external sources or local files, you can use fetch() with top-level await for remote data, or Astro.glob() and getCollection()/getEntryBySlug() for local data. You can find more information about data fetching in Astro in the Data Fetching guide (https://docs.astro.build/en/guides/data-fetching/).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Astro Documentation

Astro Islands (aka Component Islands) are a pattern of web architecture pioneered by Astro. “Islands architecture” was first coined by Etsy's frontend architect Katie Sylor-Miller in 2019, and expanded on by Preact creator Jason Miller.