From ff522b96a01391a29b44f820dfcc2a2176d871e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20C=C4=83liman?= Date: Sat, 19 Oct 2024 06:02:50 +0300 Subject: [PATCH] fix: Cannot read properties of null (reading 'Symbol(astro.headAndContent)') (#11839) Co-authored-by: Bjorn Lu --- .changeset/twelve-moose-cough.md | 5 +++ .../server/render/astro/head-and-content.ts | 2 +- .../runtime/server/render/astro/instance.ts | 2 +- .../server/render/astro/render-template.ts | 2 +- .../test/units/render/components.test.js | 39 +++++++++++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 .changeset/twelve-moose-cough.md diff --git a/.changeset/twelve-moose-cough.md b/.changeset/twelve-moose-cough.md new file mode 100644 index 000000000000..c3c470c595ca --- /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 eb32c3148e40..e0b566882304 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 2df412e81b36..5db0e76b44c6 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 68df865181fb..90d57fe01806 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 991f0f105238..948adf8e3bb1 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'; + --- + + `, + '/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); + }, + ); + }); });