Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SSR data passing #383

Merged
merged 2 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/hello-world-ssr/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { ComponentType } from 'react'

export default function App({ Page, pageProps }: { Page: ComponentType<any>, pageProps: any }) {
return (
<main>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<Page {...pageProps} />
</main>
)
}
20 changes: 20 additions & 0 deletions examples/hello-world-ssr/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'https://esm.sh/react'
import type { SSROptions } from 'https://deno.land/x/aleph/types.d.ts'

export const ssr: SSROptions = {
props: async router => {
return {
$revalidate: 1, // revalidate props after 1 second
serverTime: Date.now()
}
},
paths: async () => {
return []
}
}

export default function Page(props) {
return (
<p>Now: {props.serverTime}</p>
)
}
8 changes: 7 additions & 1 deletion framework/react/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export default async function bootstrap(options: BootstrapOptions) {
const routing = new Routing({ routes, rewrites, basePath, defaultLocale, locales, redirect })
const [url, nestedModules] = routing.createRouter()
const components = await importPageModules(url, nestedModules)

if (renderMode === 'ssr') {
// restore ssr-data from HTML
// must be called before creating page props
loadSSRDataFromTag(url)
}

const pageRoute = createPageRoute(url, components)
const routerEl = createElement(Router, { appModule, pageRoute, routing })
const mountPoint = document.getElementById('__aleph')
Expand All @@ -27,7 +34,6 @@ export default async function bootstrap(options: BootstrapOptions) {
if (dataRoutes) {
setStaticDataRoutes(dataRoutes)
}
loadSSRDataFromTag(url)
hydrate(routerEl, mountPoint)
} else {
render(routerEl, mountPoint)
Expand Down