diff options
author | 2024-10-19 06:02:50 +0300 | |
---|---|---|
committer | 2024-10-19 11:02:50 +0800 | |
commit | ff522b96a01391a29b44f820dfcc2a2176d871e7 (patch) | |
tree | 33c4c2363c6ec0628569bf126995f8160d75eecc | |
parent | a75bc5e3068ed80366a03efbec78b3b0f8837516 (diff) | |
download | astro-ff522b96a01391a29b44f820dfcc2a2176d871e7.tar.gz astro-ff522b96a01391a29b44f820dfcc2a2176d871e7.tar.zst astro-ff522b96a01391a29b44f820dfcc2a2176d871e7.zip |
fix: Cannot read properties of null (reading 'Symbol(astro.headAndContent)') (#11839)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
5 files changed, 47 insertions, 3 deletions
diff --git a/.changeset/twelve-moose-cough.md b/.changeset/twelve-moose-cough.md new file mode 100644 index 000000000..c3c470c59 --- /dev/null +++ b/.changeset/twelve-moose-cough.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes error when returning a top-level `null` from an Astro file frontmatter diff --git a/packages/astro/src/runtime/server/render/astro/head-and-content.ts b/packages/astro/src/runtime/server/render/astro/head-and-content.ts index eb32c3148..e0b566882 100644 --- a/packages/astro/src/runtime/server/render/astro/head-and-content.ts +++ b/packages/astro/src/runtime/server/render/astro/head-and-content.ts @@ -9,7 +9,7 @@ export type HeadAndContent = { }; export function isHeadAndContent(obj: unknown): obj is HeadAndContent { - return typeof obj === 'object' && !!(obj as any)[headAndContentSym]; + return typeof obj === 'object' && obj !== null && !!(obj as any)[headAndContentSym]; } export function createHeadAndContent(head: string, content: RenderTemplateResult): HeadAndContent { diff --git a/packages/astro/src/runtime/server/render/astro/instance.ts b/packages/astro/src/runtime/server/render/astro/instance.ts index 2df412e81..5db0e76b4 100644 --- a/packages/astro/src/runtime/server/render/astro/instance.ts +++ b/packages/astro/src/runtime/server/render/astro/instance.ts @@ -101,5 +101,5 @@ export function createAstroComponentInstance( } export function isAstroComponentInstance(obj: unknown): obj is AstroComponentInstance { - return typeof obj === 'object' && !!(obj as any)[astroComponentInstanceSym]; + return typeof obj === 'object' && obj !== null && !!(obj as any)[astroComponentInstanceSym]; } diff --git a/packages/astro/src/runtime/server/render/astro/render-template.ts b/packages/astro/src/runtime/server/render/astro/render-template.ts index 68df86518..90d57fe01 100644 --- a/packages/astro/src/runtime/server/render/astro/render-template.ts +++ b/packages/astro/src/runtime/server/render/astro/render-template.ts @@ -57,7 +57,7 @@ export class RenderTemplateResult { // Determines if a component is an .astro component export function isRenderTemplateResult(obj: unknown): obj is RenderTemplateResult { - return typeof obj === 'object' && !!(obj as any)[renderTemplateResultSym]; + return typeof obj === 'object' && obj !== null && !!(obj as any)[renderTemplateResultSym]; } export function renderTemplate(htmlParts: TemplateStringsArray, ...expressions: any[]) { diff --git a/packages/astro/test/units/render/components.test.js b/packages/astro/test/units/render/components.test.js index 991f0f105..948adf8e3 100644 --- a/packages/astro/test/units/render/components.test.js +++ b/packages/astro/test/units/render/components.test.js @@ -108,4 +108,43 @@ describe('core/render components', () => { }, ); }); + + it('should render component with `null` response', async () => { + const fixture = await createFixture({ + '/src/pages/index.astro': ` + --- + import NullComponent from '../components/NullComponent.astro'; + --- + <NullComponent /> + `, + '/src/components/NullComponent.astro': ` + --- + return null; + --- + `, + }); + + await runInContainer( + { + inlineConfig: { + root: fixture.path, + logLevel: 'silent', + }, + }, + async (container) => { + const { req, res, done, text } = createRequestAndResponse({ + method: 'GET', + url: '/', + }); + container.handle(req, res); + + await done; + const html = await text(); + const $ = cheerio.load(html); + + assert.equal($('body').text(), ''); + assert.equal(res.statusCode, 200); + }, + ); + }); }); |