Make Astro global available outside of *.astro files #190
Replies: 7 comments 4 replies
-
We had recently the same question. We currently have the "workaround" that we do the I think having some possibility to access the global |
Beta Was this translation helpful? Give feedback.
-
I will chime in with my oft-noted whine 😄 — if this could be used for non-HTML files, would allow better creation of RSS and JSON feeds — especially the latter, since the JSON Feed spec requires HTML content but, without |
Beta Was this translation helpful? Give feedback.
-
I see some new and interesting things in the docs: a note mentions that
and the RSS guide shows an example of using said const postImportResult = import.meta.glob('../posts/**/*.md', { eager: true });
const posts = Object.values(postImportResult); So I guess the same approach could be used in any file. |
Beta Was this translation helpful? Give feedback.
-
I am also running into the wish to have a Astro global object outside of .astro files for me it is .mdx files which I would like to combine with dynamic routing. However, Hence also here you currently need to create an extra .astro file as a workaround to pass the Astro.params information via props. |
Beta Was this translation helpful? Give feedback.
-
We have the same problem with |
Beta Was this translation helpful? Give feedback.
-
A use-case for Astro global in script files (and component framework files) would be sharing variables like |
Beta Was this translation helpful? Give feedback.
-
This would be great. I have a Or I suppose another solution could be if astro components could export things. |
Beta Was this translation helpful? Give feedback.
-
I just started playing with Astro, so do let me know if I'm missing something.
The docs mentions that the Astro global is available in
*.astro
files. What I didn't expect is that it's only available there. It doesn't seem possible to extract any code that uses it to a separate file.Let's take the blog sample: it would be nice to extract the logic that calls
Astro.glob()
and then sorts the posts into a separategetPosts
function, maybe in asrc/lib/posts.js
module. That doesn't work:Astro
there isundefined
, even if the function that uses it is called from an*.astro
file.Passing the
Astro
instance as an argument when callinggetPosts
function doesn't work either: this results in afn is not a function
error.The only solution I could find was to use Node.js Builtins to get the list of posts rather than using the
Astro
global, which is the opposite of what the docs recommend.Is there any way to make the Astro global available outside of
*.astro
files?Beta Was this translation helpful? Give feedback.
All reactions