Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate glob #11826

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading