From a2a7b9ada571b3cb47886e23fd3e482f23b250d0 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 7 Sep 2022 00:12:24 +0200 Subject: [PATCH 1/5] correctly unmount vnodes --- .changeset/hungry-rules-itch.md | 5 +++++ src/index.js | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 .changeset/hungry-rules-itch.md diff --git a/.changeset/hungry-rules-itch.md b/.changeset/hungry-rules-itch.md new file mode 100644 index 00000000..61f84440 --- /dev/null +++ b/.changeset/hungry-rules-itch.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': major +--- + +Correctly unmount vnodes diff --git a/src/index.js b/src/index.js index e9982df1..66537247 100644 --- a/src/index.js +++ b/src/index.js @@ -48,6 +48,7 @@ renderToString.render = renderToString; let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW); const EMPTY_ARR = []; +const ALL_VNODES = []; function renderToString(vnode, context, opts) { context = context || {}; @@ -80,6 +81,15 @@ function renderToString(vnode, context, opts) { if (options[COMMIT]) options[COMMIT](vnode, EMPTY_ARR); options[SKIP_EFFECTS] = previousSkipEffects; EMPTY_ARR.length = 0; + + if (options.unmount) { + let c; + while ((c = ALL_VNODES.pop())) { + options.unmount(c); + } + } + ALL_VNODES.length = 0; + return res; } @@ -204,6 +214,8 @@ function _renderToString(vnode, context, isSvgMode, selectValue) { if (options[DIFF]) options[DIFF](vnode); + ALL_VNODES.push(vnode); + let type = vnode.type, props = vnode.props; From b1256f47284f783a055af792994631dc86088b2c Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 7 Sep 2022 00:34:49 +0200 Subject: [PATCH 2/5] options.unmount after diffed --- src/index.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 66537247..a135e826 100644 --- a/src/index.js +++ b/src/index.js @@ -48,7 +48,6 @@ renderToString.render = renderToString; let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW); const EMPTY_ARR = []; -const ALL_VNODES = []; function renderToString(vnode, context, opts) { context = context || {}; @@ -82,14 +81,6 @@ function renderToString(vnode, context, opts) { options[SKIP_EFFECTS] = previousSkipEffects; EMPTY_ARR.length = 0; - if (options.unmount) { - let c; - while ((c = ALL_VNODES.pop())) { - options.unmount(c); - } - } - ALL_VNODES.length = 0; - return res; } @@ -214,8 +205,6 @@ function _renderToString(vnode, context, isSvgMode, selectValue) { if (options[DIFF]) options[DIFF](vnode); - ALL_VNODES.push(vnode); - let type = vnode.type, props = vnode.props; @@ -223,12 +212,15 @@ function _renderToString(vnode, context, isSvgMode, selectValue) { const isComponent = typeof type === 'function'; if (isComponent) { if (type === Fragment) { - return _renderToString( + const result = _renderToString( vnode.props.children, context, isSvgMode, selectValue ); + if (options.unmount) options.unmount(vnode); + + return result; } let rendered; @@ -246,6 +238,8 @@ function _renderToString(vnode, context, isSvgMode, selectValue) { // Recurse into children before invoking the after-diff hook const str = _renderToString(rendered, context, isSvgMode, selectValue); if (options[DIFFED]) options[DIFFED](vnode); + + if (options.unmount) options.unmount(vnode); return str; } @@ -354,6 +348,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue) { } if (options[DIFFED]) options[DIFFED](vnode); + if (options.unmount) options.unmount(vnode); if (hasChildren) { s = s + pieces; From 418f13c701f213d02493dd021adb81d6997416c4 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Thu, 8 Sep 2022 22:39:48 +0200 Subject: [PATCH 3/5] remove change as we are merging to a tagged branch --- .changeset/hungry-rules-itch.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .changeset/hungry-rules-itch.md diff --git a/.changeset/hungry-rules-itch.md b/.changeset/hungry-rules-itch.md deleted file mode 100644 index 61f84440..00000000 --- a/.changeset/hungry-rules-itch.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'preact-render-to-string': major ---- - -Correctly unmount vnodes From 277dc200503ff2b82ed72ad873cc9a9fd87a7156 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sat, 10 Sep 2022 21:19:46 +0200 Subject: [PATCH 4/5] Create breezy-avocados-call.md --- .changeset/breezy-avocados-call.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/breezy-avocados-call.md diff --git a/.changeset/breezy-avocados-call.md b/.changeset/breezy-avocados-call.md new file mode 100644 index 00000000..54c84287 --- /dev/null +++ b/.changeset/breezy-avocados-call.md @@ -0,0 +1,5 @@ +--- +"preact-render-to-string": patch +--- + +correctly unmount vnodes From 12498f79f1e5ec47ffd13d06152334f92d0f3fb0 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sat, 10 Sep 2022 21:21:37 +0200 Subject: [PATCH 5/5] Update index.js --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index 644fde79..9892d218 100644 --- a/src/index.js +++ b/src/index.js @@ -219,6 +219,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) { // Invoke rendering on Components const isComponent = typeof type === 'function'; if (isComponent) { + let rendered; if (type === Fragment) { rendered = props.children; } else {