Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/neat-images-resolve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes image path resolution in content layer collections to support bare filenames. The `image()` helper now normalizes bare filenames like `"cover.jpg"` to relative paths `"./cover.jpg"` for consistent resolution behavior between markdown frontmatter and JSON content collections.
23 changes: 21 additions & 2 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,27 @@ export async function getEntryDataAndImages<
schema = schema({
image: () =>
z.string().transform((val) => {
imageImports.add(val);
return `${IMAGE_IMPORT_PREFIX}${val}`;
// Normalize bare filenames to relative paths for consistent resolution
// This ensures bare filenames like "cover.jpg" work the same way as in markdown frontmatter
// Skip normalization for:
// - Relative paths (./foo, ../foo)
// - Absolute paths (/foo)
// - URLs (http://...)
// - Aliases (~/, @/, etc.)
let normalizedPath = val;
if (
val &&
!val.startsWith('./') &&
!val.startsWith('../') &&
!val.startsWith('/') &&
!val.startsWith('~') &&
!val.startsWith('@') &&
!val.includes('://')
) {
normalizedPath = `./${val}`;
}
imageImports.add(normalizedPath);
return `${IMAGE_IMPORT_PREFIX}${normalizedPath}`;
}),
});
}
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ describe('Content Layer', () => {
assert.ok(json.images[1].data.image.startsWith('https://'));
});

it('loads images with bare filenames in JSON', async () => {
assert.ok(json.rockets[0].data.image.src.startsWith('/_astro'));
assert.equal(json.rockets[0].data.image.format, 'jpg');
});

it('loads images with relative paths in JSON', async () => {
assert.ok(json.rockets[1].data.image.src.startsWith('/_astro'));
assert.equal(json.rockets[1].data.image.format, 'jpg');
});

it('renders images from frontmatter', async () => {
assert.ok($('img[alt="Lunar Module"]').attr('src').startsWith('/_astro'));
});
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/fixtures/content-layer/src/content.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ const songs = defineCollection({
}),
});

const rockets = defineCollection({
loader: file('src/data/rockets.json'),
schema: ({ image }) =>
z.object({
id: z.string(),
name: z.string(),
manufacturer: z.string(),
image: image(),
}),
});

export const collections = {
blog,
dogs,
Expand All @@ -270,6 +281,7 @@ export const collections = {
songs,
probes,
rodents,
rockets,
notADirectory,
nothingMatches
};
Expand Down
14 changes: 14 additions & 0 deletions packages/astro/test/fixtures/content-layer/src/data/rockets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"id": "falcon-9",
"name": "Falcon 9",
"manufacturer": "SpaceX",
"image": "shuttle.jpg"
},
{
"id": "saturn-v",
"name": "Saturn V",
"manufacturer": "NASA",
"image": "./shuttle.jpg"
}
]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export async function GET() {
const nestedJsonLoader = await getCollection('birds');

const csvLoader = await getCollection('plants');


const rockets = await getCollection('rockets');

const numbers = await getCollection('numbers');

const numbersYaml = await getCollection('numbersYaml');
Expand All @@ -55,7 +57,8 @@ export async function GET() {
numbers,
numbersYaml,
numbersToml,
images,
images,
rockets,
probes,
yamlLoader,
tomlLoader,
Expand Down
Loading