astrojs

Resolving path to file in public folder with Astro


I have an astro project that has a folder structure like this:

/
  src/
    index.astro
  public/
    data.json

I can import the data.json file when running locally (via npm run dev) using the relative path src="../public/data.json". However when I build the site (npm run build), the path to the JSON file breaks.

How should I set up my project so that I can successfully reference the data.json file while running locally, and also after building the site?


Solution

  • See the astro docs.

    If you have a file in public/data.json, then its path is /data.json.

    To remember this, I always think of /favicon.ico.