October 22, 2024
Chicago 12, Melborne City, USA
javascript

Astro.js: Accessing the Astro global object in MDX pages


I have a TypeScript library that, under given conditions, should redirect to an error page.
One way would be using the internal Astro global object, like with Astro.redirect('/404'). Unfortunately, Astro seems not accessible when rendering MDX pages.

Here is a MWE, where, for simplicity, I am using Astro.url.

foo.mdx:

---
layout: ./foolayout.astro
---

hello

foolayout.astro:

---
import lib from "./foolib.ts";
---
{lib.url}
<slot />

foolib.ts:

export const url = Astro.url; 

This gives the error: Astro is not defined.

Note that, if I use the Astro global object in the layout file, I have no errors. That is:
foolayout.astro:

---
---
{Astro.url}
<slot />

However, I need to use it from the TypeScript library, when a given condition is met.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video