Skip to content

Commit

Permalink
Deprecate Astro.glob
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Aug 23, 2024
1 parent ea71b90 commit 74b73cc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .changeset/long-months-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'astro': major
---

Deprecate Astro.glob

The `Astro.glob` function has been deprecated in favor of Content Collections and `import.meta.glob`.

- If you want to query for markdown and MDX in your project, use Content Collections.
- If you want to query source files in your project, use `import.meta.glob`(https://vitejs.dev/guide/features.html#glob-import).

Also consider using glob packages from npm, like [fast-glob](https://www.npmjs.com/package/fast-glob), especially if statically generating your site, as it is faster for most use-cases.

The easiest path is to migrate to `import.meta.glob` like so:

```diff
- const posts = Astro.glob('./posts/*.md');
+ const posts = Object.values(import.meta.glob('./posts/*.md', { eager: true }));
```
4 changes: 4 additions & 0 deletions packages/astro/src/runtime/server/astro-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type { AstroGlobalPartial } from '../../types/public/context.js';
/** Create the Astro.glob() runtime function. */
function createAstroGlobFn() {
const globHandler = (importMetaGlobResult: Record<string, any>) => {
// This is created inside of the runtime so we don't have access to the Astro logger.
console.warn(`Astro.glob is deprecated and will be removed in a future major version of Astro.
Use import.meta.glob instead: https://vitejs.dev/guide/features.html#glob-import`);

if (typeof importMetaGlobResult === 'string') {
throw new AstroError({
...AstroErrorData.AstroGlobUsedOutside,
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/types/public/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export interface AstroGlobalPartial {
* ```
*
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroglob)
* @deprecated Astro.glob is deprecated and will be removed in the next major version of Astro. Use `import.meta.glob` instead: https://vitejs.dev/guide/features.html#glob-import
*/
glob(globStr: `${any}.astro`): Promise<AstroInstance[]>;
glob<T extends Record<string, any>>(
Expand Down

0 comments on commit 74b73cc

Please sign in to comment.