forked from frandiox/vite-ssr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.jsx
29 lines (25 loc) · 766 Bytes
/
api.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React from 'react'
function getPageProps({ baseUrl, name, path } = {}) {
// Get our page props from our custom API:
return fetch(
`${baseUrl}/api/getProps?path=${encodeURIComponent(
path
)}&name=${name}&client=${typeof window !== 'undefined'}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
).then((res) => res.json())
}
export function PropsProvider({ baseUrl, route, children: Page }) {
if (!route.meta.state || Object.keys(route.meta.state).length === 0) {
const promise = getPageProps({ baseUrl, ...route }).then((state) => {
route.meta.state = state
})
// Will be suspended until resolved
throw promise
}
return <Page {...route.meta.state} />
}