-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(assets): resolve Picture TDZ error when combined with content render() #16171
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
Merged
alexanderniebuhr
merged 8 commits into
withastro:main
from
Daedalus-Icarus:fix/issue-16036
Apr 8, 2026
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a12139a
fix(assets): resolve Picture TDZ error when combined with content ren…
Daedalus-Icarus fe77105
chore: add changeset for Picture TDZ fix
Daedalus-Icarus 940b31e
fix: rename virtual module to virtual:astro:get-image
Daedalus-Icarus 938e959
Merge branch 'main' into fix/issue-16036
matthewp c77b777
Merge branch 'main' into fix/issue-16036
matthewp dfdcc3d
Merge branch 'main' of https://github.com/Desel72/astro into fix/issu…
8f2d9e5
fix: add virtual:astro:get-image to dev-only.d.ts and remove ts-expec…
38866ae
Apply suggestion from @alexanderniebuhr
alexanderniebuhr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Fixes a build error (`Cannot access '$$Picture' before initialization`) that occurred when a prerendered page used the `<Picture>` component and another page called `render()` on content collection entries | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/astro/test/content-collection-picture-render.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { before, describe, it } from 'node:test'; | ||
| import * as cheerio from 'cheerio'; | ||
| import { loadFixture } from './test-utils.js'; | ||
|
|
||
| // Regression test for https://github.com/withastro/astro/issues/16036 | ||
| // Using the <Picture> component on a prerendered page combined with render() | ||
| // on content collection entries caused a TDZ error during build: | ||
| // "ReferenceError: Cannot access '$$Picture' before initialization" | ||
| describe('Content collection with Picture component and render()', () => { | ||
| /** @type {import("./test-utils.js").Fixture} */ | ||
| let fixture; | ||
|
|
||
| before(async () => { | ||
| fixture = await loadFixture({ root: './fixtures/content-collection-picture-render/' }); | ||
| }); | ||
|
|
||
| describe('Build', () => { | ||
| before(async () => { | ||
| await fixture.build(); | ||
| }); | ||
|
|
||
| it('successfully builds pages using the Picture component', async () => { | ||
| const html = await fixture.readFile('/index.html'); | ||
| assert.ok(html, 'Expected index page to be generated'); | ||
|
|
||
| const $ = cheerio.load(html); | ||
| const $picture = $('picture'); | ||
| assert.ok($picture.length, 'Expected <picture> element to be rendered'); | ||
| }); | ||
|
|
||
| it('successfully builds content collection pages with render()', async () => { | ||
| const html = await fixture.readFile('/blog/post-1/index.html'); | ||
| assert.ok(html, 'Expected blog page to be generated'); | ||
|
|
||
| const $ = cheerio.load(html); | ||
| assert.equal($('.title').text(), 'Post One'); | ||
| }); | ||
|
|
||
| it('resolves cover image in content collection entry', async () => { | ||
| const html = await fixture.readFile('/blog/post-1/index.html'); | ||
| const $ = cheerio.load(html); | ||
|
|
||
| const $img = $('.cover'); | ||
| assert.ok($img.attr('src'), 'Expected cover image to have a src'); | ||
| }); | ||
|
|
||
| it('renders content body from content collection entry', async () => { | ||
| const html = await fixture.readFile('/blog/post-1/index.html'); | ||
| const $ = cheerio.load(html); | ||
|
|
||
| const $content = $('.content'); | ||
| assert.ok($content.length, 'Expected content div to be present'); | ||
| assert.ok($content.text().includes('Hello world'), 'Expected rendered markdown content'); | ||
| }); | ||
| }); | ||
| }); |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/content-collection-picture-render/astro.config.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { defineConfig } from 'astro/config'; | ||
|
|
||
| export default defineConfig({}); |
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/content-collection-picture-render/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "name": "@test/content-collection-picture-render", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "astro": "workspace:*" | ||
| } | ||
| } |
Binary file added
BIN
+70 Bytes
...astro/test/fixtures/content-collection-picture-render/src/assets/test-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions
16
packages/astro/test/fixtures/content-collection-picture-render/src/content.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { defineCollection } from 'astro:content'; | ||
| import { z } from 'astro/zod'; | ||
| import { glob } from 'astro/loaders'; | ||
|
|
||
| const blog = defineCollection({ | ||
| loader: glob({ pattern: '**/*.md', base: './src/content/blog' }), | ||
| schema: ({ image }) => | ||
| z.object({ | ||
| title: z.string(), | ||
| cover: image(), | ||
| }), | ||
| }); | ||
|
|
||
| export const collections = { | ||
| blog, | ||
| }; |
8 changes: 8 additions & 0 deletions
8
...stro/test/fixtures/content-collection-picture-render/src/content/blog/post-1.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| title: Post One | ||
| cover: ../../assets/test-image.png | ||
| --- | ||
|
|
||
| Hello world! Here is an image: | ||
|
|
||
|  |
26 changes: 26 additions & 0 deletions
26
...ages/astro/test/fixtures/content-collection-picture-render/src/pages/blog/[...slug].astro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| import { getCollection, render } from 'astro:content'; | ||
|
|
||
| export async function getStaticPaths() { | ||
| const posts = await getCollection('blog'); | ||
| return posts.map((post) => ({ | ||
| params: { slug: post.id }, | ||
| props: { post }, | ||
| })); | ||
| } | ||
|
|
||
| const { post } = Astro.props; | ||
| const { Content } = await render(post); | ||
| --- | ||
| <html> | ||
| <head> | ||
| <title>{post.data.title}</title> | ||
| </head> | ||
| <body> | ||
| <h1 class="title">{post.data.title}</h1> | ||
| <img class="cover" src={post.data.cover.src} width={post.data.cover.width} height={post.data.cover.height} alt="cover" /> | ||
| <div class="content"> | ||
| <Content /> | ||
| </div> | ||
| </body> | ||
| </html> |
12 changes: 12 additions & 0 deletions
12
packages/astro/test/fixtures/content-collection-picture-render/src/pages/index.astro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| import { Picture } from 'astro:assets'; | ||
| import testImage from '../assets/test-image.png'; | ||
| --- | ||
| <html> | ||
| <head> | ||
| <title>Picture Page</title> | ||
| </head> | ||
| <body> | ||
| <Picture class="hero" src={testImage} alt="Test image" formats={['webp']} /> | ||
| </body> | ||
| </html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.