vike-ripple 0.4.6 → 0.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -3,31 +3,66 @@ export { onRenderClient }
|
|
|
3
3
|
import { setPageContext } from '../hooks/usePageContext.js'
|
|
4
4
|
import { setHydrated } from '../hooks/useHydrated.js'
|
|
5
5
|
|
|
6
|
+
// tsrx_element — wraps a component fn as a Ripple TSRX element (matches ripple/internal)
|
|
7
|
+
const tsrx_element = (fn) => ({
|
|
8
|
+
render: fn,
|
|
9
|
+
[Symbol.for('ripple.element')]: true
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
/** @type {(() => void) | null} */
|
|
13
|
+
let dispose = null
|
|
14
|
+
|
|
6
15
|
const onRenderClient = async (pageContext) => {
|
|
7
|
-
|
|
8
|
-
isHydration: pageContext.isHydration,
|
|
9
|
-
isFirstRender: 'isFirstRender' in pageContext,
|
|
10
|
-
hasPage: !!pageContext.Page,
|
|
11
|
-
url: pageContext.urlOriginal,
|
|
12
|
-
location: window.location.href,
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
const { Page } = pageContext
|
|
16
|
+
const { Page, config } = pageContext
|
|
16
17
|
if (!Page) return
|
|
17
18
|
|
|
18
19
|
setPageContext(pageContext)
|
|
19
20
|
const container = document.getElementById('root')
|
|
20
|
-
if (!container)
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
if (!container) return
|
|
22
|
+
|
|
23
|
+
// ── Build same component tree as SSR ──
|
|
24
|
+
// Apply Layouts (innermost first → outermost last)
|
|
25
|
+
const Layout = config.Layout ?? config.layout
|
|
26
|
+
const Wrapper = config.Wrapper ?? config.wrapper
|
|
27
|
+
let component = Page
|
|
28
|
+
if (Layout) {
|
|
29
|
+
const layouts = Array.isArray(Layout) ? Layout : [Layout]
|
|
30
|
+
for (let i = 0; i < layouts.length; i++) {
|
|
31
|
+
const L = layouts[i]
|
|
32
|
+
const prev = component
|
|
33
|
+
component = (props) => L({ ...props, children: tsrx_element(prev) })
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (Wrapper) {
|
|
37
|
+
const wrappers = Array.isArray(Wrapper) ? Wrapper : [Wrapper]
|
|
38
|
+
for (const W of wrappers) {
|
|
39
|
+
const prev = component
|
|
40
|
+
component = (props) => W({ ...props, children: tsrx_element(prev) })
|
|
41
|
+
}
|
|
23
42
|
}
|
|
24
43
|
|
|
25
|
-
|
|
44
|
+
// ── Clean up previous root ──
|
|
45
|
+
if (dispose) {
|
|
46
|
+
dispose()
|
|
47
|
+
dispose = null
|
|
48
|
+
}
|
|
26
49
|
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
mount(
|
|
30
|
-
|
|
50
|
+
// ── Hydrate or mount ──
|
|
51
|
+
// Hydrate preserves SSR HTML; mount clears and re-renders.
|
|
52
|
+
// mount() is the stable fallback when hydration fails (e.g. during HMR reload).
|
|
53
|
+
const { mount, hydrate } = await import('ripple')
|
|
54
|
+
|
|
55
|
+
if (pageContext.isHydration && container.innerHTML !== '') {
|
|
56
|
+
try {
|
|
57
|
+
dispose = hydrate(component, { target: container, props: {} })
|
|
58
|
+
setHydrated()
|
|
59
|
+
return
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.warn('[vike-ripple] hydrate failed, falling back to mount:', err)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
31
64
|
|
|
32
|
-
|
|
65
|
+
// mount() clears container internally (target.textContent = '')
|
|
66
|
+
dispose = mount(component, { target: container, props: {} })
|
|
67
|
+
setHydrated()
|
|
33
68
|
}
|
|
@@ -26,7 +26,7 @@ const onRenderHtml = async (pageContext) => {
|
|
|
26
26
|
const Wrapper = pageContext.config.Wrapper
|
|
27
27
|
if (Layout) {
|
|
28
28
|
const layouts = Array.isArray(Layout) ? Layout : [Layout]
|
|
29
|
-
for (let i =
|
|
29
|
+
for (let i = 0; i < layouts.length; i++) {
|
|
30
30
|
const L = layouts[i]
|
|
31
31
|
const prev = wrappedPage
|
|
32
32
|
wrappedPage = (props) => L({ ...props, children: tsrx_element(prev) })
|