summaryrefslogtreecommitdiff
path: root/packages/integrations/lit/server.js
diff options
context:
space:
mode:
authorGravatar Elliott Marquez <5981958+e111077@users.noreply.github.com> 2023-02-01 05:18:37 -0800
committerGravatar GitHub <noreply@github.com> 2023-02-01 08:18:37 -0500
commit0db22041531d981a813a07f4c4e00cfb7ebddd51 (patch)
treec0d51b213a6c4a90479fa509d221bf129ab05602 /packages/integrations/lit/server.js
parente193dfad1ec0d9bf857f0cce314c0127234c8bea (diff)
downloadastro-0db22041531d981a813a07f4c4e00cfb7ebddd51.tar.gz
astro-0db22041531d981a813a07f4c4e00cfb7ebddd51.tar.zst
astro-0db22041531d981a813a07f4c4e00cfb7ebddd51.zip
[Lit] Fix hydration not having the same reactive values as server (#6080)
* Fix lit hydration not having the same reactive values * add changeset * add clientEntrypoint to package exports * update tests * add changeset * only add defer-hydration when strictly necessary * remove second changest * fix test typos
Diffstat (limited to 'packages/integrations/lit/server.js')
-rw-r--r--packages/integrations/lit/server.js14
1 files changed, 11 insertions, 3 deletions
diff --git a/packages/integrations/lit/server.js b/packages/integrations/lit/server.js
index 83737c183..48a3c646f 100644
--- a/packages/integrations/lit/server.js
+++ b/packages/integrations/lit/server.js
@@ -36,10 +36,18 @@ function* render(Component, attrs, slots) {
// LitElementRenderer creates a new element instance, so copy over.
const Ctr = getCustomElementConstructor(tagName);
+ let shouldDeferHydration = false;
+
if (attrs) {
for (let [name, value] of Object.entries(attrs)) {
- // check if this is a reactive property
- if (name in Ctr.prototype) {
+ const isReactiveProperty = name in Ctr.prototype;
+ const isReflectedReactiveProperty = Ctr.elementProperties.get(name)?.reflect;
+
+ // Only defer hydration if we are setting a reactive property that cannot
+ // be reflected / serialized as a property.
+ shouldDeferHydration ||= isReactiveProperty && !isReflectedReactiveProperty;
+
+ if (isReactiveProperty) {
instance.setProperty(name, value);
} else {
instance.setAttribute(name, value);
@@ -49,7 +57,7 @@ function* render(Component, attrs, slots) {
instance.connectedCallback();
- yield `<${tagName}`;
+ yield `<${tagName}${shouldDeferHydration ? ' defer-hydration' : ''}`;
yield* instance.renderAttributes();
yield `>`;
const shadowContents = instance.renderShadow({});