Skip to content

Commit

Permalink
Mark comment prop as unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Jun 9, 2023
1 parent e20cb5d commit 8e0a6e3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 1 addition & 3 deletions .changeset/shy-hotels-impress.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
'preact-render-to-string': minor
---

Add ability to render comments via `<Fragment comment="my-comment" />`. When the `comment` prop is present all children of that `Fragment` will be ignored. This PR only supports that in server environments as it's useful to mark islands and other things. It's not supported in the browser.

We picked a `Fragment` for this as it's the least common component and therefore the branch in our code with the least impact on perf by adding another if-check.
Add experimental ability to render HTML comments via `<Fragment UNSTABLE_comment="my-comment" />`. When the `UNSTABLE_comment` prop is present all children of that `Fragment` will be ignored and a HTML comment will be rendered instead. This feature is added to allow framework authors to experiment with marking DOM for hydration in the client. Note that it's marked as unstable and might change in the future.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
if (type === Fragment) {
// Fragments are the least used components of core that's why
// branching here for comments has the least effect on perf.
if (props.comment) {
return '<!--' + encodeEntities(props.comment || '') + '-->';
if (props.UNSTABLE_comment) {
return '<!--' + encodeEntities(props.UNSTABLE_comment || '') + '-->';
}

rendered = props.children;
Expand Down
6 changes: 4 additions & 2 deletions test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1311,13 +1311,15 @@ describe('render', () => {

describe('HTML Comments', () => {
it('should render HTML comments via Fragments', () => {
expect(render(<Fragment comment="foo" />)).to.equal('<!--foo-->');
expect(render(<Fragment UNSTABLE_comment="foo" />)).to.equal(
'<!--foo-->'
);
});

it('should ignore children with comment prop', () => {
expect(
render(
<Fragment comment="foo">
<Fragment UNSTABLE_comment="foo">
<p>foo</p>
</Fragment>
)
Expand Down

0 comments on commit 8e0a6e3

Please sign in to comment.