diff --git a/.changeset/twelve-moose-cough.md b/.changeset/twelve-moose-cough.md new file mode 100644 index 0000000000..c3c470c595 --- /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 eb32c3148e..e0b5668823 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 2df412e81b..5db0e76b44 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 68df865181..90d57fe018 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 991f0f1052..948adf8e3b 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); + }, + ); + }); });