From 8681a9515e56f21b689671b002f1cdc262ff8ebe Mon Sep 17 00:00:00 2001 From: Augustine Kim Date: Thu, 19 Oct 2023 17:32:34 -0700 Subject: [PATCH 1/3] Avoid nullish logical assignment for root part --- packages/labs/ssr-client/src/lib/hydrate-lit-html.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts b/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts index 02c6af0250..ae4d601cf0 100644 --- a/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts +++ b/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts @@ -163,7 +163,12 @@ export const hydrate = ( } // Create a new ChildPart and push it onto the stack currentChildPart = openChildPart(rootValue, marker, stack, options); - rootPart ??= currentChildPart; + // Using nullish logical assignment below can cause some minifier to move + // the `openChildPart()` call above behind the nullish check. + // See https://github.com/lit/lit/issues/4289 + if (rootPart === undefined) { + rootPart = currentChildPart; + } rootPartMarker ??= marker; } else if (markerText.startsWith('lit-node')) { // Create and hydrate attribute parts into the current ChildPart on the From ab978c58c2de0552715f26356983caa708befe59 Mon Sep 17 00:00:00 2001 From: Augustine Kim Date: Thu, 19 Oct 2023 17:36:51 -0700 Subject: [PATCH 2/3] Add changeset --- .changeset/olive-otters-vanish.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/olive-otters-vanish.md diff --git a/.changeset/olive-otters-vanish.md b/.changeset/olive-otters-vanish.md new file mode 100644 index 0000000000..cfc9be183b --- /dev/null +++ b/.changeset/olive-otters-vanish.md @@ -0,0 +1,5 @@ +--- +'@lit-labs/ssr-client': patch +--- + +Avoid nullish logical assignment in hydrate-lit-html which some minification process would not handle correctly. Fixes hydration errors in Next.js production bundles. From 0153dd04602629937d6ac0990b97621c17447c0f Mon Sep 17 00:00:00 2001 From: Augustine Kim Date: Thu, 19 Oct 2023 19:50:08 -0700 Subject: [PATCH 3/3] Update packages/labs/ssr-client/src/lib/hydrate-lit-html.ts Co-authored-by: Peter Burns --- packages/labs/ssr-client/src/lib/hydrate-lit-html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts b/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts index ae4d601cf0..5c6247f457 100644 --- a/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts +++ b/packages/labs/ssr-client/src/lib/hydrate-lit-html.ts @@ -163,7 +163,7 @@ export const hydrate = ( } // Create a new ChildPart and push it onto the stack currentChildPart = openChildPart(rootValue, marker, stack, options); - // Using nullish logical assignment below can cause some minifier to move + // Using nullish logical assignment below can cause next.js's swc to move // the `openChildPart()` call above behind the nullish check. // See https://github.com/lit/lit/issues/4289 if (rootPart === undefined) {