From 4b89f4ed080dd0f1efed0d35311af543de1f9c60 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 1 Jul 2021 15:30:48 -0400 Subject: [PATCH] Allow Preact components to return an empty Fragment (#599) --- .../preact-component/src/components/EmptyFrag.jsx | 5 +++++ .../fixtures/preact-component/src/pages/frag.astro | 10 ++++++++++ packages/astro/test/preact-component.test.js | 8 ++++++++ packages/renderers/renderer-preact/server.js | 2 +- 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 packages/astro/test/fixtures/preact-component/src/components/EmptyFrag.jsx create mode 100644 packages/astro/test/fixtures/preact-component/src/pages/frag.astro diff --git a/packages/astro/test/fixtures/preact-component/src/components/EmptyFrag.jsx b/packages/astro/test/fixtures/preact-component/src/components/EmptyFrag.jsx new file mode 100644 index 0000000000..0a74552b5d --- /dev/null +++ b/packages/astro/test/fixtures/preact-component/src/components/EmptyFrag.jsx @@ -0,0 +1,5 @@ +import { h, Fragment } from 'preact'; + +export default function() { + return +} \ No newline at end of file diff --git a/packages/astro/test/fixtures/preact-component/src/pages/frag.astro b/packages/astro/test/fixtures/preact-component/src/pages/frag.astro new file mode 100644 index 0000000000..a451768cea --- /dev/null +++ b/packages/astro/test/fixtures/preact-component/src/pages/frag.astro @@ -0,0 +1,10 @@ +--- +import FragComponent from '../components/EmptyFrag.jsx'; +--- + + + + Preact component returns fragment + + + \ No newline at end of file diff --git a/packages/astro/test/preact-component.test.js b/packages/astro/test/preact-component.test.js index 77e17e03c6..31f206d7ee 100644 --- a/packages/astro/test/preact-component.test.js +++ b/packages/astro/test/preact-component.test.js @@ -32,4 +32,12 @@ PreactComponent('Can use hooks', async ({ runtime }) => { assert.equal($('#world').length, 1); }); +PreactComponent('Can export a Fragment', async ({ runtime }) => { + const result = await runtime.load('/frag'); + if (result.error) throw new Error(result.error); + + const $ = doc(result.contents); + assert.equal($('body').children().length, 0, 'nothing rendered but it didn\'t throw.'); +}); + PreactComponent.run(); diff --git a/packages/renderers/renderer-preact/server.js b/packages/renderers/renderer-preact/server.js index 8465dbe695..ac8661a49a 100644 --- a/packages/renderers/renderer-preact/server.js +++ b/packages/renderers/renderer-preact/server.js @@ -10,7 +10,7 @@ function check(Component, props, children) { } const { html } = renderToStaticMarkup(Component, props, children); - return Boolean(html); + return typeof html === 'string'; } function renderToStaticMarkup(Component, props, children) {