-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: Example of using incremental rehydration
This PR adds an example showing how to use intersection observer in conjunction with partial rehydration.
- Loading branch information
1 parent
5c278b2
commit 32c044d
Showing
6 changed files
with
49 additions
and
66 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 |
---|---|---|
@@ -1,38 +1,36 @@ | ||
import { renderComponent } from '@glimmer/core'; | ||
import RehydratableRegion from './src/RehydratableRegion'; | ||
import RehydratableCounter from './src/RehydratableCounter'; | ||
|
||
document.addEventListener( | ||
'DOMContentLoaded', | ||
() => { | ||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
entries.forEach(async (entry) => { | ||
if (entry.isIntersecting) { | ||
await renderComponent(RehydratableRegion, { | ||
element: document.querySelector('.static-component'), | ||
args: {}, | ||
rehydrate: true, | ||
}); | ||
} | ||
}); | ||
}, | ||
{ | ||
root: null, | ||
} | ||
); | ||
|
||
const rehydratables = Array.from(document.querySelectorAll('[data-hydrate]')); | ||
rehydrate({ | ||
get RehydratableCounter() { | ||
// Can load components async | ||
return Promise.resolve(RehydratableCounter); | ||
}, | ||
}); | ||
|
||
console.log(rehydratables); | ||
for (const el of rehydratables) { | ||
observer.observe(el); | ||
function rehydrate(componentMapping) { | ||
const hasHydrated = new WeakSet(); | ||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
entries.forEach(async (entry) => { | ||
if (entry.isIntersecting && !hasHydrated.has(entry.target)) { | ||
await renderComponent(await componentMapping[entry.target.dataset.hydrate], { | ||
element: entry.target.parentElement, | ||
args: JSON.parse(entry.target.querySelector('script').textContent), | ||
rehydrate: true, | ||
}); | ||
hasHydrated.add(entry.target); | ||
} | ||
}); | ||
}, | ||
{ | ||
root: null, | ||
} | ||
); | ||
|
||
// const element = document.querySelector('.static-component'); | ||
// renderComponent(RehydratingComponent, { | ||
// element: element!, | ||
// rehydrate: true, | ||
// }); | ||
}, | ||
{ once: true } | ||
); | ||
const rehydratables = Array.from(document.querySelectorAll('[data-hydrate]')); | ||
console.log(rehydratables); | ||
for (const el of rehydratables) { | ||
observer.observe(el); | ||
} | ||
} |
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
24 changes: 0 additions & 24 deletions
24
packages/example-apps/partial-rehydration/src/RehydratingComponent.ts
This file was deleted.
Oops, something went wrong.
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