Skip to content

Commit

Permalink
fix: Cannot read properties of null (reading 'Symbol(astro.headAndCon…
Browse files Browse the repository at this point in the history
…tent)') (#11839)

Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
icaliman and bluwy authored Oct 19, 2024
1 parent a75bc5e commit ff522b9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-moose-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes error when returning a top-level `null` from an Astro file frontmatter
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/server/render/astro/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down
39 changes: 39 additions & 0 deletions packages/astro/test/units/render/components.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
);
});
});

0 comments on commit ff522b9

Please sign in to comment.