Commit 7debb96
Fix async-loader for Preact X (#1309)
* Fix async-loader for Preact X
I finally (FINALLY) found a solution for #1293, #1308, etc etc. This doesn't use DOM hacks or anything - it just crawls up the tree of VNodes and performs a Depth-First-Search backwards from the position of AsyncComponent in order to find the most recently hydrated DOM element. Essentially, the first populated `vnode._dom` reference it finds is guaranteed to be the previous sibling of the parent element AsyncComponent is about to get rendered into. As long as AsyncComponent isn't being used load a component that renders a Fragment, the nextSibling of that discovered element is the exact node Preact will try to hydrate.
In the case of an AsyncComponent wrapping a Fragment, only the first Fragment child will be hydrated. This means the remaining children will have the flicker effect, but that's pretty reasonable:
```js
// app.js
import Foo from 'async!./foo';
export default () => <div><Foo /></div>
// foo.js
export default () => <Fragment>
<h1>Hello</h1>
<p>world!</p> // will be culled+reinserted during hydration
</Fragment>
```
* Stop searching if we hit an element parent
Co-authored-by: Anup <[email protected]>1 parent 9f3282b commit 7debb96
1 file changed
+24
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
5 | 26 | | |
6 | 27 | | |
| 28 | + | |
7 | 29 | | |
8 | 30 | | |
9 | 31 | | |
| |||
23 | 45 | | |
24 | 46 | | |
25 | 47 | | |
26 | | - | |
| 48 | + | |
| 49 | + | |
27 | 50 | | |
28 | 51 | | |
29 | 52 | | |
| |||
0 commit comments