#Dynamic route getStaticPaths issue in SSG

5 messages · Page 1 of 1 (latest)

fiery silo
#

Hello, I'm having a bit of an issue currently and hope one of you can help me.
This is likelu not an issue on astro's end, but on mine, however I hope that I can still get the help I need.

---
import type { GetStaticPaths } from "astro";
import type { Reference } from "../../types/home";
import config from "../../config/references.yml";

const references: Reference[] = config.references;

export const getStaticPaths = (() => {
  return references.map(reference => ({
    params: {
      project: reference.name.toLowerCase()
    }
  }));
}) satisfies GetStaticPaths;

const { project } = Astro.params;
---

The component above (/pages/references/[project].astro) gives me the following error: references is not defined (references/[project].astro:9:3).

I'm kind of confused, because I made sure the import is correct, and fetching the reference array is also not an issue in other components, such as here:

import type { Reference } from "../../types/home";
import config from "../../config/references.yml";

const references: Reference[] = config.references;
// ...

The component above (/components/home/References.tsx) has no issues using the references whatsoever

#

perhaps this is some sort of incompatibility between the vite-plugin-yaml and astro?
I just created a new ts file which works if I import it:

export const staticPaths = config.references.map((reference: any) => ({
  params: {
    project: reference.name.toLowerCase()
  }
}));
sonic crow
#

Hey! So this is a classic stumbling block for using getStaticPaths!

getStaticPaths runs in it's own process and cannot reference variables outside it's own scope!

So here just simply move your const references = line into your getStaticPaths method and it should all work. (I haven't actually checked any of the other code, but give it a try and report back!)

From the docs

fiery silo
#

that worked! thank you^^

silent otter
#

If your issue is resolved, please consider doing the following:

  • From the ellipses in the top-right corner of the post (not the 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. It will just be moved down with older posts, so we can more easily focus on issues that still need to be resoled.
    Thank you.