-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lit] Fix hydration not having the same reactive values as server #6080
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
34478bf
Fix lit hydration not having the same reactive values
e111077 8db563c
add changeset
e111077 70e8f45
add clientEntrypoint to package exports
e111077 8a3f2e7
update tests
e111077 2843a67
add changeset
e111077 fc71fc6
only add defer-hydration when strictly necessary
e111077 1735d56
remove second changest
e111077 f3530b2
fix test typos
e111077 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/lit': patch | ||
--- | ||
|
||
Fixes Lit hydration not having the same reactive values as server (losing state upon hydration) |
35 changes: 35 additions & 0 deletions
35
packages/astro/e2e/fixtures/lit-component/src/components/NonDeferredCounter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { LitElement, html } from 'lit'; | ||
|
||
export default class NonDeferredCounter extends LitElement { | ||
static get properties() { | ||
return { | ||
count: { | ||
type: Number, | ||
// All set properties are reflected to attributes so its hydration is | ||
// not deferred. | ||
reflect: true, | ||
}, | ||
}; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
this.count = 0; | ||
} | ||
|
||
increment() { | ||
this.count++; | ||
} | ||
|
||
render() { | ||
return html` | ||
<div> | ||
<p>Count: ${this.count}</p> | ||
|
||
<button type="button" @click=${this.increment}>Increment</button> | ||
</div> | ||
`; | ||
} | ||
} | ||
|
||
customElements.define('non-deferred-counter', NonDeferredCounter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import MyCounter from '../components/Counter.js'; | ||
|
||
const someProps = { | ||
count: 0, | ||
count: 10, | ||
}; | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import MyCounter from '../components/Counter.js'; | ||
|
||
const someProps = { | ||
count: 0, | ||
count: 10, | ||
}; | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/astro/test/fixtures/lit-element/src/components/non-deferred-element.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { LitElement, html } from 'lit'; | ||
import { property, customElement } from 'lit/decorators.js'; | ||
|
||
@customElement('non-deferred-counter') | ||
export class NonDeferredCounter extends LitElement { | ||
// All set properties are reflected to attributes so its hydration is not | ||
// hydration-deferred should always be set. | ||
@property({ type: Number, reflect: true }) count = 0; | ||
|
||
increment() { | ||
this.count++; | ||
} | ||
|
||
render() { | ||
return html` | ||
<div> | ||
<p>Count: ${this.count}</p> | ||
|
||
<button type="button" @click=${this.increment}>Increment</button> | ||
</div> | ||
`; | ||
} | ||
} |
23 changes: 15 additions & 8 deletions
23
packages/astro/test/fixtures/lit-element/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
--- | ||
import {MyElement} from '../components/my-element.js'; | ||
import {NonDeferredCounter} from '../components/non-deferred-element.js'; | ||
--- | ||
|
||
<html> | ||
<head> | ||
<title>LitElements</title> | ||
<title>LitElements</title> | ||
</head> | ||
<body> | ||
<MyElement | ||
foo="bar" | ||
str-attr={'initialized'} | ||
bool={false} | ||
obj={{data: 1}} | ||
reflectedStrProp={'initialized reflected'}> | ||
</MyElement> | ||
<MyElement | ||
id="default" | ||
foo="bar" | ||
str-attr={'initialized'} | ||
bool={false} | ||
obj={{data: 1}} | ||
reflectedStrProp={'initialized reflected'}> | ||
</MyElement> | ||
<NonDeferredCounter | ||
id="non-deferred" | ||
count={10} | ||
foo="bar"> | ||
</NonDeferredCounter> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you might be the first to add logical or assignments to our codebase. |
||
|
||
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({}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export default (element: HTMLElement) => | ||
async ( | ||
Component: any, | ||
props: Record<string, any>, | ||
) => { | ||
// Get the LitElement element instance (may or may not be upgraded). | ||
const component = element.children[0] as HTMLElement; | ||
|
||
// If there is no deferral of hydration, then all reactive properties are | ||
// already serialzied as reflected attributes, or no reactive props were set | ||
if (!component || !component.hasAttribute('defer-hydration')) { | ||
return; | ||
} | ||
|
||
// Set properties on the LitElement instance for resuming hydration. | ||
for (let [name, value] of Object.entries(props)) { | ||
// Check if reactive property or class property. | ||
if (name in Component.prototype) { | ||
(component as any)[name] = value; | ||
} | ||
} | ||
|
||
// Tell LitElement to resume hydration. | ||
component.removeAttribute('defer-hydration'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love these comments