-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
deferred-comp
to demo deferred hydration.
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 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,8 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>`deferred-comp` tests</title> | ||
<meta charset="utf8"> | ||
</head> | ||
<body></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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { parseHtml } from 'hydroactive/testing.js'; | ||
import { DeferredComp } from './deferred-comp.js'; | ||
|
||
describe('deferred-comp', () => { | ||
afterEach(() => { | ||
for (const node of document.body.childNodes) node.remove(); | ||
}); | ||
|
||
function render(): InstanceType<typeof DeferredComp> { | ||
return parseHtml(DeferredComp, ` | ||
<deferred-comp defer-hydration> | ||
<div>Hello, <span>World</span>!</div> | ||
</deferred-comp> | ||
`); | ||
} | ||
|
||
describe('DeferredComp', () => { | ||
it('does not hydrate on connection', () => { | ||
const el = render(); | ||
document.body.appendChild(el); | ||
|
||
// Component upgrades immediately. | ||
expect(el).toBeInstanceOf(DeferredComp); | ||
|
||
// Hydration should be delayed. | ||
expect(el.querySelector('span')!.textContent).toBe('World'); | ||
}); | ||
|
||
it('hydrates when `defer-hydration` is removed', async () => { | ||
const el = render(); | ||
document.body.appendChild(el); | ||
|
||
el.removeAttribute('defer-hydration'); | ||
|
||
// Hydration is synchronous, but rendering is not. | ||
await el.stable(); | ||
|
||
expect(el.querySelector('span')!.textContent).toBe('HydroActive'); | ||
}); | ||
}); | ||
}); |
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,13 @@ | ||
import { defineComponent } from 'hydroactive'; | ||
|
||
/** Says hello to HydroActive on hydration. */ | ||
export const DeferredComp = defineComponent('deferred-comp', (comp) => { | ||
const name = comp.live('span', String); | ||
name.set('HydroActive'); | ||
}); | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'deferred-comp': InstanceType<typeof DeferredComp>; | ||
} | ||
} |
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,32 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Hydration Demo</title> | ||
<meta charset="utf8"> | ||
|
||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"hydroactive": "/hydroactive/index.js", | ||
"hydroactive/": "/hydroactive/" | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1>Hydration Demo</h1> | ||
|
||
<nav><a href="/">Home</a></nav> | ||
|
||
<deferred-comp defer-hydration> | ||
<h2>Deferred Component</h2> | ||
|
||
<div>Hello, <span>World</span>!</div> | ||
|
||
<script src="./deferred-comp.js" type="module"></script> | ||
</deferred-comp> | ||
<button onclick="javascript:this.previousElementSibling.removeAttribute('defer-hydration'); this.disabled = true;"> | ||
Hydrate | ||
</button> | ||
</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