-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
13 changed files
with
181 additions
and
35 deletions.
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
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