Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 2d5f897

Browse files
authored
Fix SSR data passing (#383)
* fix(framework/react): load ssr-data before creating page props * feat: add hello-world-ssr
1 parent 3a8678f commit 2d5f897

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

examples/hello-world-ssr/app.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React, { ComponentType } from 'react'
2+
3+
export default function App({ Page, pageProps }: { Page: ComponentType<any>, pageProps: any }) {
4+
return (
5+
<main>
6+
<head>
7+
<meta name="viewport" content="width=device-width" />
8+
</head>
9+
<Page {...pageProps} />
10+
</main>
11+
)
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'https://esm.sh/react'
2+
import type { SSROptions } from 'https://deno.land/x/aleph/types.d.ts'
3+
4+
export const ssr: SSROptions = {
5+
props: async router => {
6+
return {
7+
$revalidate: 1, // revalidate props after 1 second
8+
serverTime: Date.now()
9+
}
10+
},
11+
paths: async () => {
12+
return []
13+
}
14+
}
15+
16+
export default function Page(props) {
17+
return (
18+
<p>Now: {props.serverTime}</p>
19+
)
20+
}

framework/react/bootstrap.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export default async function bootstrap(options: BootstrapOptions) {
1919
const routing = new Routing({ routes, rewrites, basePath, defaultLocale, locales, redirect })
2020
const [url, nestedModules] = routing.createRouter()
2121
const components = await importPageModules(url, nestedModules)
22+
23+
if (renderMode === 'ssr') {
24+
// restore ssr-data from HTML
25+
// must be called before creating page props
26+
loadSSRDataFromTag(url)
27+
}
28+
2229
const pageRoute = createPageRoute(url, components)
2330
const routerEl = createElement(Router, { appModule, pageRoute, routing })
2431
const mountPoint = document.getElementById('__aleph')
@@ -27,7 +34,6 @@ export default async function bootstrap(options: BootstrapOptions) {
2734
if (dataRoutes) {
2835
setStaticDataRoutes(dataRoutes)
2936
}
30-
loadSSRDataFromTag(url)
3137
hydrate(routerEl, mountPoint)
3238
} else {
3339
render(routerEl, mountPoint)

0 commit comments

Comments
 (0)