create-vike 0.0.336 → 0.0.338
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/README.md +11 -0
- package/boilerplate-react/package.json +1 -1
- package/boilerplate-react/renderer/+onRenderClient.jsx +3 -3
- package/boilerplate-react/renderer/+onRenderHtml.jsx +3 -3
- package/boilerplate-react/renderer/{PageShell.jsx → PageLayout.jsx} +4 -4
- package/boilerplate-react-ts/package.json +1 -1
- package/boilerplate-react-ts/renderer/+onRenderClient.tsx +3 -3
- package/boilerplate-react-ts/renderer/+onRenderHtml.tsx +3 -3
- package/boilerplate-react-ts/renderer/{PageShell.tsx → PageLayout.tsx} +3 -3
- package/boilerplate-vue/package.json +1 -1
- package/boilerplate-vue/renderer/app.js +20 -11
- package/boilerplate-vue/renderer/useData.js +10 -3
- package/boilerplate-vue/renderer/utils.js +3 -0
- package/boilerplate-vue-ts/package.json +1 -1
- package/boilerplate-vue-ts/renderer/app.ts +19 -17
- package/boilerplate-vue-ts/renderer/useData.ts +13 -5
- package/boilerplate-vue-ts/renderer/usePageContext.ts +2 -2
- package/boilerplate-vue-ts/renderer/utils.ts +11 -0
- package/package.json +1 -1
- /package/boilerplate-react/renderer/{PageShell.css → PageLayout.css} +0 -0
- /package/boilerplate-react-ts/renderer/{PageShell.css → PageLayout.css} +0 -0
- /package/boilerplate-vue/renderer/{PageShell.vue → PageLayout.vue} +0 -0
- /package/boilerplate-vue-ts/renderer/{PageShell.vue → PageLayout.vue} +0 -0
package/README.md
ADDED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export { onRenderClient }
|
|
3
3
|
|
|
4
4
|
import ReactDOM from 'react-dom/client'
|
|
5
|
-
import {
|
|
5
|
+
import { PageLayout } from './PageLayout'
|
|
6
6
|
import { getPageTitle } from './getPageTitle'
|
|
7
7
|
|
|
8
8
|
let root
|
|
@@ -17,9 +17,9 @@ function onRenderClient(pageContext) {
|
|
|
17
17
|
if (!container) throw new Error('DOM element #react-root not found')
|
|
18
18
|
|
|
19
19
|
const page = (
|
|
20
|
-
<
|
|
20
|
+
<PageLayout pageContext={pageContext}>
|
|
21
21
|
<Page />
|
|
22
|
-
</
|
|
22
|
+
</PageLayout>
|
|
23
23
|
)
|
|
24
24
|
if (pageContext.isHydration) {
|
|
25
25
|
root = ReactDOM.hydrateRoot(container, page)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export { onRenderHtml }
|
|
3
3
|
|
|
4
4
|
import ReactDOMServer from 'react-dom/server'
|
|
5
|
-
import {
|
|
5
|
+
import { PageLayout } from './PageLayout'
|
|
6
6
|
import { escapeInject, dangerouslySkipEscape } from 'vike/server'
|
|
7
7
|
import logoUrl from './logo.svg'
|
|
8
8
|
import { getPageTitle } from './getPageTitle'
|
|
@@ -16,9 +16,9 @@ function onRenderHtml(pageContext) {
|
|
|
16
16
|
|
|
17
17
|
// Alternativly, we can use an HTML stream, see https://vike.dev/streaming
|
|
18
18
|
const pageHtml = ReactDOMServer.renderToString(
|
|
19
|
-
<
|
|
19
|
+
<PageLayout pageContext={pageContext}>
|
|
20
20
|
<Page />
|
|
21
|
-
</
|
|
21
|
+
</PageLayout>
|
|
22
22
|
)
|
|
23
23
|
|
|
24
24
|
// See https://vike.dev/head
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { PageLayout }
|
|
2
2
|
|
|
3
3
|
import React from 'react'
|
|
4
4
|
import PropTypes from 'prop-types'
|
|
@@ -7,13 +7,13 @@ import logoUrl from './logo.svg'
|
|
|
7
7
|
import { PageContextProvider } from './usePageContext'
|
|
8
8
|
import { Link } from './Link'
|
|
9
9
|
import './css/index.css'
|
|
10
|
-
import './
|
|
10
|
+
import './PageLayout.css'
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
PageLayout.propTypes = {
|
|
13
13
|
pageContext: PropTypes.any,
|
|
14
14
|
children: childrenPropType
|
|
15
15
|
}
|
|
16
|
-
function
|
|
16
|
+
function PageLayout({ pageContext, children }) {
|
|
17
17
|
return (
|
|
18
18
|
<React.StrictMode>
|
|
19
19
|
<PageContextProvider pageContext={pageContext}>
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export { onRenderClient }
|
|
3
3
|
|
|
4
4
|
import ReactDOM from 'react-dom/client'
|
|
5
|
-
import {
|
|
5
|
+
import { PageLayout } from './PageLayout'
|
|
6
6
|
import { getPageTitle } from './getPageTitle'
|
|
7
7
|
import type { OnRenderClientAsync } from 'vike/types'
|
|
8
8
|
|
|
@@ -18,9 +18,9 @@ const onRenderClient: OnRenderClientAsync = async (pageContext): ReturnType<OnRe
|
|
|
18
18
|
if (!container) throw new Error('DOM element #react-root not found')
|
|
19
19
|
|
|
20
20
|
const page = (
|
|
21
|
-
<
|
|
21
|
+
<PageLayout pageContext={pageContext}>
|
|
22
22
|
<Page />
|
|
23
|
-
</
|
|
23
|
+
</PageLayout>
|
|
24
24
|
)
|
|
25
25
|
if (pageContext.isHydration) {
|
|
26
26
|
root = ReactDOM.hydrateRoot(container, page)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export { onRenderHtml }
|
|
3
3
|
|
|
4
4
|
import ReactDOMServer from 'react-dom/server'
|
|
5
|
-
import {
|
|
5
|
+
import { PageLayout } from './PageLayout'
|
|
6
6
|
import { escapeInject, dangerouslySkipEscape } from 'vike/server'
|
|
7
7
|
import logoUrl from './logo.svg'
|
|
8
8
|
import type { OnRenderHtmlAsync } from 'vike/types'
|
|
@@ -17,9 +17,9 @@ const onRenderHtml: OnRenderHtmlAsync = async (pageContext): ReturnType<OnRender
|
|
|
17
17
|
|
|
18
18
|
// Alternativly, we can use an HTML stream, see https://vike.dev/streaming
|
|
19
19
|
const pageHtml = ReactDOMServer.renderToString(
|
|
20
|
-
<
|
|
20
|
+
<PageLayout pageContext={pageContext}>
|
|
21
21
|
<Page />
|
|
22
|
-
</
|
|
22
|
+
</PageLayout>
|
|
23
23
|
)
|
|
24
24
|
|
|
25
25
|
// See https://vike.dev/head
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { PageLayout }
|
|
2
2
|
|
|
3
3
|
import React from 'react'
|
|
4
4
|
import logoUrl from './logo.svg'
|
|
@@ -6,9 +6,9 @@ import { PageContextProvider } from './usePageContext'
|
|
|
6
6
|
import { Link } from './Link'
|
|
7
7
|
import type { PageContext } from 'vike/types'
|
|
8
8
|
import './css/index.css'
|
|
9
|
-
import './
|
|
9
|
+
import './PageLayout.css'
|
|
10
10
|
|
|
11
|
-
function
|
|
11
|
+
function PageLayout({ children, pageContext }: { children: React.ReactNode; pageContext: PageContext }) {
|
|
12
12
|
return (
|
|
13
13
|
<React.StrictMode>
|
|
14
14
|
<PageContextProvider pageContext={pageContext}>
|
|
@@ -1,36 +1,45 @@
|
|
|
1
1
|
export { createApp }
|
|
2
2
|
|
|
3
|
-
import { createSSRApp,
|
|
4
|
-
import
|
|
3
|
+
import { createSSRApp, h, markRaw, reactive, ref } from 'vue'
|
|
4
|
+
import PageLayout from './PageLayout.vue'
|
|
5
5
|
import { setPageContext } from './usePageContext'
|
|
6
|
+
import { setData } from './useData'
|
|
7
|
+
import { isObject } from './utils'
|
|
6
8
|
|
|
7
9
|
function createApp(pageContext) {
|
|
8
10
|
const { Page } = pageContext
|
|
9
11
|
|
|
10
12
|
const pageRef = ref(markRaw(Page))
|
|
11
13
|
|
|
12
|
-
const
|
|
14
|
+
const PageWithLayout = {
|
|
13
15
|
render() {
|
|
14
|
-
return h(
|
|
16
|
+
return h(PageLayout, {}, { default: () => h(pageRef.value) })
|
|
15
17
|
}
|
|
16
|
-
}
|
|
18
|
+
}
|
|
17
19
|
|
|
18
|
-
const app = createSSRApp(
|
|
20
|
+
const app = createSSRApp(PageWithLayout)
|
|
19
21
|
|
|
20
|
-
//
|
|
22
|
+
// app.changePage() is called upon navigation, see +onRenderClient.ts
|
|
21
23
|
Object.assign(app, {
|
|
22
24
|
changePage: (pageContext) => {
|
|
25
|
+
const data = pageContext.data ?? {}
|
|
26
|
+
assertDataIsObject(data)
|
|
27
|
+
Object.assign(dataReactive, data)
|
|
23
28
|
Object.assign(pageContextReactive, pageContext)
|
|
24
29
|
pageRef.value = markRaw(pageContext.Page)
|
|
25
30
|
}
|
|
26
31
|
})
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
33
|
+
const data = pageContext.data ?? {}
|
|
34
|
+
assertDataIsObject(data)
|
|
35
|
+
const dataReactive = reactive(data)
|
|
30
36
|
const pageContextReactive = reactive(pageContext)
|
|
31
|
-
|
|
32
|
-
// Make pageContext available from any Vue component
|
|
33
37
|
setPageContext(app, pageContextReactive)
|
|
38
|
+
setData(app, dataReactive)
|
|
34
39
|
|
|
35
40
|
return app
|
|
36
41
|
}
|
|
42
|
+
|
|
43
|
+
function assertDataIsObject(data) {
|
|
44
|
+
if (!isObject(data)) throw new Error('Return value of data() hook should be an object, undefined, or null')
|
|
45
|
+
}
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
// https://vike.dev/useData
|
|
2
2
|
export { useData }
|
|
3
|
+
export { setData }
|
|
3
4
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
5
|
+
import { inject } from 'vue'
|
|
6
|
+
|
|
7
|
+
const key = Symbol()
|
|
6
8
|
|
|
7
9
|
/** https://vike.dev/useData */
|
|
8
10
|
function useData() {
|
|
9
|
-
const data =
|
|
11
|
+
const data = inject(key)
|
|
12
|
+
if (!data) throw new Error('setData() not called')
|
|
10
13
|
return data
|
|
11
14
|
}
|
|
15
|
+
|
|
16
|
+
function setData(app, data) {
|
|
17
|
+
app.provide(key, data)
|
|
18
|
+
}
|
|
@@ -1,44 +1,46 @@
|
|
|
1
1
|
export { createApp }
|
|
2
2
|
|
|
3
|
-
import { createSSRApp,
|
|
4
|
-
import
|
|
3
|
+
import { createSSRApp, h, markRaw, reactive, ref } from 'vue'
|
|
4
|
+
import PageLayout from './PageLayout.vue'
|
|
5
5
|
import { setPageContext } from './usePageContext'
|
|
6
6
|
import type { PageContext } from 'vike/types'
|
|
7
|
+
import { setData } from './useData'
|
|
8
|
+
import { isObject, objectAssign } from './utils'
|
|
7
9
|
|
|
8
10
|
function createApp(pageContext: PageContext) {
|
|
9
11
|
const { Page } = pageContext
|
|
10
12
|
|
|
11
13
|
const pageRef = ref(markRaw(Page))
|
|
12
|
-
|
|
14
|
+
|
|
15
|
+
const PageWithLayout = {
|
|
13
16
|
render() {
|
|
14
|
-
return h(
|
|
17
|
+
return h(PageLayout, {}, { default: () => h(pageRef.value) })
|
|
15
18
|
}
|
|
16
|
-
}
|
|
19
|
+
}
|
|
17
20
|
|
|
18
|
-
const app = createSSRApp(
|
|
21
|
+
const app = createSSRApp(PageWithLayout)
|
|
19
22
|
|
|
20
|
-
//
|
|
23
|
+
// app.changePage() is called upon navigation, see +onRenderClient.ts
|
|
21
24
|
objectAssign(app, {
|
|
22
25
|
changePage: (pageContext: PageContext) => {
|
|
26
|
+
const data = pageContext.data ?? {}
|
|
27
|
+
assertDataIsObject(data)
|
|
28
|
+
Object.assign(dataReactive, data)
|
|
23
29
|
Object.assign(pageContextReactive, pageContext)
|
|
24
30
|
pageRef.value = markRaw(pageContext.Page)
|
|
25
31
|
}
|
|
26
32
|
})
|
|
27
33
|
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
const data = pageContext.data ?? {}
|
|
35
|
+
assertDataIsObject(data)
|
|
36
|
+
const dataReactive = reactive(data)
|
|
30
37
|
const pageContextReactive = reactive(pageContext)
|
|
31
|
-
|
|
32
|
-
// Make pageContext available from any Vue component
|
|
33
38
|
setPageContext(app, pageContextReactive)
|
|
39
|
+
setData(app, dataReactive)
|
|
34
40
|
|
|
35
41
|
return app
|
|
36
42
|
}
|
|
37
43
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
obj: Obj,
|
|
41
|
-
objAddendum: ObjAddendum
|
|
42
|
-
): asserts obj is Obj & ObjAddendum {
|
|
43
|
-
Object.assign(obj, objAddendum)
|
|
44
|
+
function assertDataIsObject(data: unknown): asserts data is Record<string, unknown> {
|
|
45
|
+
if (!isObject(data)) throw new Error('Return value of data() hook should be an object, undefined, or null')
|
|
44
46
|
}
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
// https://vike.dev/useData
|
|
2
2
|
export { useData }
|
|
3
|
+
export { setData }
|
|
3
4
|
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
5
|
+
import { inject } from 'vue'
|
|
6
|
+
import type { App } from 'vue'
|
|
7
|
+
|
|
8
|
+
const key = Symbol()
|
|
6
9
|
|
|
7
10
|
/** https://vike.dev/useData */
|
|
8
|
-
function useData<Data>():
|
|
9
|
-
const data =
|
|
10
|
-
|
|
11
|
+
function useData<Data>(): Data {
|
|
12
|
+
const data = inject(key)
|
|
13
|
+
if (!data) throw new Error('setData() not called')
|
|
14
|
+
return data as any
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function setData(app: App, data: unknown): void {
|
|
18
|
+
app.provide(key, data)
|
|
11
19
|
}
|
|
@@ -9,12 +9,12 @@ import type { PageContext } from 'vike/types'
|
|
|
9
9
|
const key: InjectionKey<PageContext> = Symbol()
|
|
10
10
|
|
|
11
11
|
/** https://vike.dev/usePageContext */
|
|
12
|
-
function usePageContext() {
|
|
12
|
+
function usePageContext(): PageContext {
|
|
13
13
|
const pageContext = inject(key)
|
|
14
14
|
if (!pageContext) throw new Error('setPageContext() not called in parent')
|
|
15
15
|
return pageContext
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
function setPageContext(app: App, pageContext: PageContext) {
|
|
18
|
+
function setPageContext(app: App, pageContext: PageContext): void {
|
|
19
19
|
app.provide(key, pageContext)
|
|
20
20
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Same as Object.assign() but with type inference
|
|
2
|
+
export function objectAssign<Obj extends object, ObjAddendum>(
|
|
3
|
+
obj: Obj,
|
|
4
|
+
objAddendum: ObjAddendum
|
|
5
|
+
): asserts obj is Obj & ObjAddendum {
|
|
6
|
+
Object.assign(obj, objAddendum)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isObject(value: unknown): value is Record<string, unknown> {
|
|
10
|
+
return typeof value === 'object' && value !== null
|
|
11
|
+
}
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|