-
-
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
Changes from 5 commits
a12139a
fe77105
940b31e
938e959
c77b777
dfdcc3d
8f2d9e5
38866ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -454,8 +454,9 @@ async function updateImageReferencesInBody(html: string, fileName: string) { | |
|
|
||
| const imageObjects = new Map<string, GetImageResult>(); | ||
|
|
||
| // @ts-expect-error Virtual module resolved at runtime | ||
| const { getImage } = await import('astro:assets'); | ||
| // @ts-expect-error Internal virtual module that exports only getImage (no component | ||
| // references like Image/Picture) to avoid TDZ errors during prerender bundling (#16036). | ||
| const { getImage } = await import('virtual:astro:get-image'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We must add this module to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Desel72 can you address this comment please? |
||
|
|
||
| // First load all the images. This is done outside of the replaceAll | ||
| // function because getImage is async. | ||
|
|
||
| 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'); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { defineConfig } from 'astro/config'; | ||
|
|
||
| export default defineConfig({}); |
| 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:*" | ||
| } | ||
| } |
| 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, | ||
| }; |
| 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: | ||
|
|
||
|  |
| 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> |
| 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.
Uh oh!
There was an error while loading. Please reload this page.