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

Commit 2f2fdc9

Browse files
committed
Imporove ssr data fetching
1 parent b97fa96 commit 2f2fdc9

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

framework/react/bootstrap.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { hydrate, render } from 'https://esm.sh/[email protected]'
33
import { importModule } from '../core/module.ts'
44
import { Routing, RoutingOptions } from '../core/routing.ts'
55
import Router, { createPageRoute } from './components/Router.ts'
6-
import { loadSSRDataFromTag } from './pagedata.ts'
6+
import { loadSSRDataFromTag, setStaticSsrRoutes } from './pagedata.ts'
77

88
type BootstrapOptions = Required<RoutingOptions> & {
9+
ssrRoutes?: string[],
910
appModule?: string,
1011
renderMode: 'ssr' | 'spa'
1112
}
1213

1314
export default async function bootstrap(options: BootstrapOptions) {
14-
const { basePath, defaultLocale, locales, appModule: appModuleSpcifier, routes, rewrites, renderMode } = options
15+
const { basePath, defaultLocale, locales, appModule: appModuleSpcifier, routes, ssrRoutes, rewrites, renderMode } = options
1516
const { document } = window as any
1617
const appModule = appModuleSpcifier ? await importModule(basePath, appModuleSpcifier) : {}
1718
const routing = new Routing({ routes, rewrites, basePath, defaultLocale, locales })
@@ -21,23 +22,25 @@ export default async function bootstrap(options: BootstrapOptions) {
2122
const mountPoint = document.getElementById('__aleph')
2223

2324
if (renderMode === 'ssr') {
25+
if (ssrRoutes) {
26+
setStaticSsrRoutes(ssrRoutes)
27+
}
2428
loadSSRDataFromTag(url)
2529
hydrate(routerEl, mountPoint)
2630
} else {
2731
render(routerEl, mountPoint)
2832
}
2933

30-
// remove ssr head elements, set a timmer to avoid the tab title flash
31-
setTimeout(() => {
32-
Array.from(document.head.children).forEach((el: any) => {
33-
const tag = el.tagName.toLowerCase()
34-
if (
35-
el.hasAttribute('ssr') &&
36-
tag !== 'style' &&
37-
!(tag === 'link' && el.getAttribute('rel') === 'stylesheet')
38-
) {
39-
document.head.removeChild(el)
40-
}
41-
})
42-
}, 0)
34+
// remove ssr head elements
35+
await Promise.resolve()
36+
Array.from(document.head.children).forEach((el: any) => {
37+
const tag = el.tagName.toLowerCase()
38+
if (
39+
el.hasAttribute('ssr') &&
40+
tag !== 'style' &&
41+
!(tag === 'link' && el.getAttribute('rel') === 'stylesheet')
42+
) {
43+
document.head.removeChild(el)
44+
}
45+
})
4346
}

framework/react/pagedata.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import util from '../../shared/util.ts'
22
import type { RouterURL } from '../../types.ts'
33

44
const global = window as any
5+
const lazySsrRoutes: Map<string, boolean> = new Map()
6+
7+
let staticSsrRoutes: Set<string> | null = null
8+
export function setStaticSsrRoutes(routes: string[]) {
9+
staticSsrRoutes = new Set(routes)
10+
}
511

612
export function shouldLoadPageData(url: RouterURL): boolean {
713
const href = url.toString()
@@ -16,6 +22,12 @@ export function shouldLoadPageData(url: RouterURL): boolean {
1622
delete global[`${pagedataUrl}#${key}`]
1723
})
1824
}
25+
if (staticSsrRoutes) {
26+
return staticSsrRoutes.has(url.routePath)
27+
}
28+
if (lazySsrRoutes.has(url.routePath)) {
29+
return lazySsrRoutes.get(url.routePath)!
30+
}
1931
return true
2032
}
2133

@@ -29,10 +41,17 @@ export async function loadPageData(url: RouterURL): Promise<void> {
2941
const data = await resp.json()
3042
if (util.isPlainObject(data)) {
3143
storeData(href, data)
44+
if (!staticSsrRoutes) {
45+
lazySsrRoutes.set(url.routePath, true)
46+
}
47+
}
48+
} else if (resp.status === 404) {
49+
if (!staticSsrRoutes) {
50+
lazySsrRoutes.set(url.routePath, false)
3251
}
3352
}
3453
} catch (err) {
35-
console.error(err)
54+
console.error('loadPageData:', err)
3655
}
3756
}
3857

@@ -46,7 +65,9 @@ export function loadSSRDataFromTag(url: RouterURL) {
4665
storeData(href, ssrData)
4766
return
4867
}
49-
} catch (e) { }
68+
} catch (e) {
69+
console.warn('ssr-data: invalid JSON')
70+
}
5071
}
5172
}
5273

0 commit comments

Comments
 (0)