From 74b73ccb2c3623bf6131e2bfeb1f09d5e778a886 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 23 Aug 2024 11:12:40 -0500 Subject: [PATCH] Deprecate Astro.glob --- .changeset/long-months-rule.md | 19 +++++++++++++++++++ .../astro/src/runtime/server/astro-global.ts | 4 ++++ packages/astro/src/types/public/context.ts | 1 + 3 files changed, 24 insertions(+) create mode 100644 .changeset/long-months-rule.md diff --git a/.changeset/long-months-rule.md b/.changeset/long-months-rule.md new file mode 100644 index 0000000000000..2e44f0793c470 --- /dev/null +++ b/.changeset/long-months-rule.md @@ -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 })); +``` diff --git a/packages/astro/src/runtime/server/astro-global.ts b/packages/astro/src/runtime/server/astro-global.ts index 74b32e3314219..0602302cc49da 100644 --- a/packages/astro/src/runtime/server/astro-global.ts +++ b/packages/astro/src/runtime/server/astro-global.ts @@ -5,6 +5,10 @@ import type { AstroGlobalPartial } from '../../types/public/context.js'; /** Create the Astro.glob() runtime function. */ function createAstroGlobFn() { const globHandler = (importMetaGlobResult: Record) => { + // 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, diff --git a/packages/astro/src/types/public/context.ts b/packages/astro/src/types/public/context.ts index 5f05b7a662737..89f28c70dc429 100644 --- a/packages/astro/src/types/public/context.ts +++ b/packages/astro/src/types/public/context.ts @@ -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; glob>(