diff options
author | 2024-08-21 13:17:52 +0200 | |
---|---|---|
committer | 2024-08-21 12:17:52 +0100 | |
commit | 9f943c1344671b569a0d1ddba683b3cca0068adc (patch) | |
tree | adea982834def5ff38b9540410fb0f4898c4faca | |
parent | 6814ff07f56426dfce5b14355c07bed28fc28032 (diff) | |
download | astro-9f943c1344671b569a0d1ddba683b3cca0068adc.tar.gz astro-9f943c1344671b569a0d1ddba683b3cca0068adc.tar.zst astro-9f943c1344671b569a0d1ddba683b3cca0068adc.zip |
Fix file loader for JSON object files (#11801)
* Add `filePath` to `file()` loader entries when JSON file is an object
* Add changeset
-rw-r--r-- | .changeset/four-rats-fail.md | 5 | ||||
-rw-r--r-- | packages/astro/src/content/loaders/file.ts | 10 |
2 files changed, 9 insertions, 6 deletions
diff --git a/.changeset/four-rats-fail.md b/.changeset/four-rats-fail.md new file mode 100644 index 000000000..aa0fcbdb7 --- /dev/null +++ b/.changeset/four-rats-fail.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug where the `filePath` property was not available on content collection entries when using the content layer `file()` loader with a JSON file that contained an object instead of an array. This was breaking use of the `image()` schema utility among other things. diff --git a/packages/astro/src/content/loaders/file.ts b/packages/astro/src/content/loaders/file.ts index cbc684a99..75e5e214d 100644 --- a/packages/astro/src/content/loaders/file.ts +++ b/packages/astro/src/content/loaders/file.ts @@ -26,6 +26,8 @@ export function file(fileName: string): Loader { return; } + const normalizedFilePath = posixRelative(fileURLToPath(settings.config.root), filePath); + if (Array.isArray(json)) { if (json.length === 0) { logger.warn(`No items found in ${fileName}`); @@ -39,11 +41,7 @@ export function file(fileName: string): Loader { continue; } const data = await parseData({ id, data: rawItem, filePath }); - store.set({ - id, - data, - filePath: posixRelative(fileURLToPath(settings.config.root), filePath), - }); + store.set({ id, data, filePath: normalizedFilePath }); } } else if (typeof json === 'object') { const entries = Object.entries<Record<string, unknown>>(json); @@ -51,7 +49,7 @@ export function file(fileName: string): Loader { store.clear(); for (const [id, rawItem] of entries) { const data = await parseData({ id, data: rawItem, filePath }); - store.set({ id, data }); + store.set({ id, data, filePath: normalizedFilePath }); } } else { logger.error(`Invalid data in ${fileName}. Must be an array or object.`); |