mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -05:00
Fix file loader for JSON object files (#11801)
* Add `filePath` to `file()` loader entries when JSON file is an object * Add changeset
This commit is contained in:
parent
6814ff07f5
commit
9f943c1344
2 changed files with 9 additions and 6 deletions
5
.changeset/four-rats-fail.md
Normal file
5
.changeset/four-rats-fail.md
Normal file
|
@ -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.
|
|
@ -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.`);
|
||||
|
|
Loading…
Add table
Reference in a new issue