Placement of getStaticPaths matters
Astro Info
Astro v5.9.3
Node v24.3.0
System macOS (arm64)
Package Manager npm
Output static
Adapter none
Integrations none
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
I've noticed a weird behaviour with dynamic routing and Astro properties.
I was following the i18n documentation and have this code in my index.astro
export const getStaticPaths = async () => [
{ params: { locale: "en" } },
{ params: { locale: "de" } },
];
I decided to move the paths to another file so that I do not need to touch x files whenever I add another language:
import { i18nPaths } from "../../i18n/utils";
export const getStaticPaths = async () => i18nPaths;
Now I have the following problem: I was trying to access Astro.url and Astro.currentLocale. But depending on where I put the getStaticPaths function they would be undefined or not:
Works:
console.log(Astro.url, Astro.currentLocale) // URL, en
export const getStaticPaths = async () => i18nPaths;
Does not work:
export const getStaticPaths = async () => i18nPaths;
console.log(Astro.url, Astro.currentLocale) // undefined, undefined
This behaviour does not occur when I inline the paths again:
Works:
export const getStaticPaths = async () => [
{ params: { locale: "en" } },
{ params: { locale: "de" } },
];
console.log(Astro.url, Astro.currentLocale); // URL, en
What's the expected result?
In my opinion this is valid too:
import { i18nPaths } from "../../i18n/utils";
export const getStaticPaths = async () => i18nPaths;
console.log(Astro.url, Astro.currentLocale) // this should give URL, en
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-znzuqhyl
Participation
- [x] I am willing to submit a pull request for this issue.
In your reproduction, this is expected for Astro.currentLocale to be undefined because you need to configure i18n in your Astro configuration file. Also, getStaticPaths should be used with dynamic routes.
But you're right, there is a bug because of getStaticPaths format which causes Astro.url to be undefined.
// This doesn't work.
export const getStaticPaths = () => i18nPaths;
// This doesn't work.
export const getStaticPaths = () => [...i18nPaths];
// This works.
export const getStaticPaths = () => {
return i18nPaths;
};
Here's an updated reproduction where I use Astro.params to make the issue more obvious: an error is thrown with the first two formats, while the third works just fine!
I think this is related to the compiler... I thought we had a similar issue somewhere, but I can't find it! Maybe I'm just remembering this from a Discord issue...
Thank you for the explanation :)
Sorry about not being clear about Astro.currentLocale in my setup - I do have it defined :)
Even though it is defined in the setup it shill shows up as undefined in the 'not working' cases though.
Yeah it's interesting :) If I find the time I can also try and look into the issue more. Would be nice to contribute something to Astro - I love it so far!
Glad to hear that!
Even though it is defined in the setup it shill shows up as undefined in the 'not working' cases though.
Oh, okay! I didn't noticed that while testing on Stackblitz but I tried a few things so I might have missed that. In any cases, it's probably for the same reason that Astro.url is undefined. Since getStaticPaths is not properly parsed, some things are not properly resolved... Using the following both are correctly resolved in Stackblitz:
export const getStaticPaths = () => {
return i18nPaths;
};
Did it work in your project?
I found the issue I was referring to (https://github.com/withastro/astro/issues/13790) but it seems to have been fixed in https://github.com/withastro/compiler/pull/1075 so this is probably another edge case not handled by the compiler! And given that, I think it's safe to move it to the compiler repo!
If you are familiar with Go, feel free to submit a PR, you can probably take inspiration of https://github.com/withastro/compiler/pull/1075 if needed! 🙌🏽
Hey thanks for the write up!
Yeah it worked - before posting this issue I already had it fixed in my code, I just wanted to make the Astro team aware of this issue :D But thanks!
I will give it a try soon - but compiler stuff and go are not necessarily my strength haha Might be a fun challenge though!