I was trying to import some markdown files in Astro in order to make a "dynamic" blog. I used Astro.glob()
. I already had it functioning perfectly. However, since a recent update it stopped working.
The code:
---
let posts: any[];
try {
posts = await Astro.glob("./blog/*.md");
console.log(posts.length);
posts = shuffleArray(posts);
posts = posts.slice(0, 15);
} catch (error) {
posts = [];
console.log(error);
}
---
The file structure is: project_root/pages/blog/*.md
This is the layout of the blog posts:
import shuffleArray from "../helpers/shuffleArray";
import MenuLayout from "./MenuLayout.astro";
const { frontmatter } = Astro.props;
let posts;
try {
posts = await Astro.glob("*.md");
posts = shuffleArray(posts);
let i;
for(i = 0; i < posts.length - 2; i++) {
if (posts[i].url === Astro.url.pathname
|| posts[i + 1].url === Astro.url.pathname
|| posts[i + 2].url === Astro.url.pathname) {
continue;
}
break;
}
posts = posts.slice(i, i + 2);
} catch (error) {
posts = "";
}
<MenuLayout title={frontmatter.title}>
<main>
<article id="central">
<slot />
</article>
</main>
</MenyLayout>
This is the markdown header example:
---
layout: ../../layouts/BlogPostLayout.astro
title: Something
author: something
img_url: /obiwan.gif
date: 2024-05-29
---
I tried changing the routes, the directory name or the files extension. This only happens to me when it's the .md extension. I tried with .astro and it works just right.
The problem really is that is just want to import it access them and pass the data of the URL to a component to just generate the routes.
EDIT: error message
Error: Invalid glob: "*.md" (resolved: "*.md"). It must start with '/' or './'
at toAbsoluteGlob (file:///home/kinire98/Documents/portfolio/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:39014:9)
at async Promise.all (index 0)
at async file:///home/kinire98/Documents/portfolio/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:38843:27
at async Promise.all (index 0)
at async parseImportGlob (file:///home/kinire98/Documents/portfolio/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:38857:11)
at async transformGlobImport (file:///home/kinire98/Documents/portfolio/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:38888:19)
at async TransformPluginContext.transform (file:///home/kinire98/Documents/portfolio/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:38662:22)
at async PluginContainer.transform (file:///home/kinire98/Documents/portfolio/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:49033:18)
at async loadAndTransform (file:///home/kinire98/Documents/portfolio/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:51866:27)
The problem was that the glob was invalid. Not the one I thought, but other in the codebase.
Search for other globs in your code base.
Answering for anyone out there that needs this.
Astro.glob()
itself isn't broken, which is what I thought previously