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

Commit 014c0a0

Browse files
committed
Fix ssr data loading
1 parent 2b024a3 commit 014c0a0

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

framework/core/routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type Route = {
1212

1313
export type RouteModule = {
1414
readonly url: string
15-
readonly useDeno?: boolean
15+
readonly withData?: boolean
1616
}
1717

1818
export type RoutingOptions = {
@@ -67,7 +67,7 @@ export class Routing {
6767
})
6868
}
6969

70-
update(path: string, moduleUrl: string, options: { isIndex?: boolean, useDeno?: boolean } = {}) {
70+
update(path: string, moduleUrl: string, options: { isIndex?: boolean, withData?: boolean } = {}) {
7171
const { isIndex, ...rest } = options
7272
const newRoute: Route = {
7373
path: path === '/' ? path : util.trimSuffix(path, '/') + (options.isIndex ? '/' : ''),

framework/react/bootstrap.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ type BootstrapOptions = Required<RoutingOptions> & {
1414
export default async function bootstrap(options: BootstrapOptions) {
1515
const { basePath, defaultLocale, locales, routes, rewrites, sharedModules, renderMode } = options
1616
const { document } = window as any
17-
const customComponents: Record<string, { C: ComponentType, useDeno?: boolean }> = {}
18-
await Promise.all(sharedModules.map(async ({ url, useDeno }) => {
17+
const customComponents: Record<string, { C: ComponentType, withData?: boolean }> = {}
18+
await Promise.all(sharedModules.map(async ({ url, withData }) => {
1919
const { default: C } = await importModule(basePath, url)
2020
switch (trimModuleExt(url)) {
2121
case '/404':
22-
customComponents['E404'] = { C, useDeno }
22+
customComponents['E404'] = { C, withData }
2323
break
2424
case '/app':
25-
customComponents['App'] = { C, useDeno }
25+
customComponents['App'] = { C, withData }
2626
break
2727
}
2828
}))
@@ -35,7 +35,7 @@ export default async function bootstrap(options: BootstrapOptions) {
3535
Component
3636
}
3737
})
38-
if (!!customComponents.App?.useDeno || nestedModules.findIndex(mod => !!mod.useDeno) > -1) {
38+
if (!!customComponents.App?.withData || nestedModules.findIndex(mod => !!mod.withData) > -1) {
3939
await loadPageDataFromTag(url)
4040
}
4141
const pageRoute: PageRoute = { ...createPageProps(await Promise.all(imports)), url }

framework/react/components/Router.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export default function Router({
2020
pageRoute,
2121
routing,
2222
}: {
23-
customComponents: Record<'E404' | 'App', { C: ComponentType, useDeno?: boolean }>
23+
customComponents: Record<'E404' | 'App', { C: ComponentType, withData?: boolean }>
2424
pageRoute: PageRoute,
2525
routing: Routing
2626
}) {
27-
const appUseDeno = !!customComponents.App?.useDeno
27+
const appWithData = !!customComponents.App?.withData
2828
const [e404, setE404] = useState<{ Component: ComponentType<any>, props?: Record<string, any> }>(() => {
2929
const { E404 } = customComponents
3030
if (E404) {
@@ -57,7 +57,7 @@ export default function Router({
5757
Component
5858
}
5959
})
60-
if (appUseDeno || nestedModules.findIndex(mod => !!mod.useDeno) > -1) {
60+
if (appWithData || nestedModules.findIndex(mod => !!mod.withData) > -1) {
6161
await loadPageData(url)
6262
}
6363
setRoute({ ...createPageProps(await Promise.all(imports)), url })
@@ -135,7 +135,7 @@ export default function Router({
135135
nestedModules.map(mod => {
136136
importModule(basePath, mod.url)
137137
})
138-
if (appUseDeno || nestedModules.findIndex(mod => !!mod.useDeno) > -1) {
138+
if (appWithData || nestedModules.findIndex(mod => !!mod.withData) > -1) {
139139
loadPageData(url)
140140
}
141141
}

server/app.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -316,30 +316,27 @@ export class Application implements ServerApplication {
316316
}
317317
if (hmrable) {
318318
let routePath: string | undefined = undefined
319-
let useDeno: boolean | undefined = undefined
319+
let withData: boolean | undefined = undefined
320320
let isIndex: boolean | undefined = undefined
321321
if (module.url.startsWith('/pages/')) {
322322
const [path, _, options] = this.createRouteUpdate(module.url)
323323
routePath = path
324-
useDeno = options.useDeno
324+
withData = options.withData
325325
isIndex = options.isIndex
326326
} else {
327327
if (['/app', '/404'].includes(trimModuleExt(module.url))) {
328-
this.lookupDeps(module.url, dep => {
329-
if (this.getModule(dep.url)?.useDenoHook) {
330-
useDeno = true
331-
return false
332-
}
333-
})
328+
if (this.hasSSRData(module.url)) {
329+
withData = true
330+
}
334331
}
335332
}
336333
if (type === 'add') {
337334
this.#fsWatchListeners.forEach(e => {
338-
e.emit('add', { url: module.url, routePath, isIndex, useDeno })
335+
e.emit('add', { url: module.url, routePath, isIndex, withData })
339336
})
340337
} else {
341338
this.#fsWatchListeners.forEach(e => {
342-
e.emit('modify-' + module.url, { useDeno })
339+
e.emit('modify-' + module.url, { withData })
343340
})
344341
}
345342
}
@@ -603,16 +600,13 @@ export class Application implements ServerApplication {
603600
sharedModules: Array.from(this.#modules.values()).filter(({ url }) => {
604601
return ['/app', '/404'].includes(trimModuleExt(url))
605602
}).map(({ url }) => {
606-
let useDeno: boolean | undefined = undefined
603+
let withData: boolean | undefined = undefined
607604
if (this.config.ssr !== false) {
608-
this.lookupDeps(url, dep => {
609-
if (this.getModule(dep.url)?.useDenoHook) {
610-
useDeno = true
611-
return false
612-
}
613-
})
605+
if (this.hasSSRData(url)) {
606+
withData = true
607+
}
614608
}
615-
return { url, useDeno }
609+
return { url, withData }
616610
}),
617611
renderMode: this.config.ssr ? 'ssr' : 'spa'
618612
}
@@ -786,19 +780,16 @@ export class Application implements ServerApplication {
786780
log.info(`Done in ${Math.round(performance.now() - start)}ms`)
787781
}
788782

789-
private createRouteUpdate(url: string): [string, string, { isIndex?: boolean, useDeno?: boolean }] {
783+
private createRouteUpdate(url: string): [string, string, { isIndex?: boolean, withData?: boolean }] {
790784
const isBuiltinModule = moduleExts.some(ext => url.endsWith('.' + ext))
791785
let routePath = isBuiltinModule ? toPagePath(url) : util.trimSuffix(url, '/pages')
792-
let useDeno: boolean | undefined = undefined
786+
let withData: boolean | undefined = undefined
793787
let isIndex: boolean | undefined = undefined
794788

795-
if (this.config.ssr !== false) {
796-
this.lookupDeps(url, dep => {
797-
if (this.getModule(dep.url)?.useDenoHook) {
798-
useDeno = true
799-
return false
800-
}
801-
})
789+
if (this.config.ssr !== false && !url.startsWith('/api/')) {
790+
if (this.hasSSRData(url)) {
791+
withData = true
792+
}
802793
}
803794

804795
if (!isBuiltinModule) {
@@ -823,7 +814,7 @@ export class Application implements ServerApplication {
823814
}
824815
}
825816

826-
return [routePath, url, { isIndex, useDeno }]
817+
return [routePath, url, { isIndex, withData }]
827818
}
828819

829820
/** fetch resource by the url. */
@@ -1403,6 +1394,21 @@ export class Application implements ServerApplication {
14031394
return ssr
14041395
}
14051396

1397+
private hasSSRData(url: string) {
1398+
let hasData = false
1399+
if (this.getModule(url)?.useDenoHook) {
1400+
hasData = true
1401+
} else {
1402+
this.lookupDeps(url, dep => {
1403+
if (this.getModule(dep.url)?.useDenoHook) {
1404+
hasData = true
1405+
return false
1406+
}
1407+
})
1408+
}
1409+
return hasData
1410+
}
1411+
14061412
/** lookup app deps recurively. */
14071413
lookupDeps(
14081414
url: string,

0 commit comments

Comments
 (0)