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

Commit 7781996

Browse files
author
Je
committed
refactor: improve useDeno for ssr
1 parent cb4dc60 commit 7781996

File tree

10 files changed

+191
-90
lines changed

10 files changed

+191
-90
lines changed

aleph.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { E400MissingDefaultExportAsComponent, E404Page, ErrorBoundary } from './
44
import events from './events.ts'
55
import { createPageProps, RouteModule, Routing } from './routing.ts'
66
import type { RouterURL } from './types.ts'
7-
import util, { hashShort, reModuleExt } from './util.ts'
7+
import util, { hashShort, reModuleExt, reStyleModuleExt } from './util.ts'
88

99
export function ALEPH({ initial }: {
1010
initial: {
@@ -46,11 +46,21 @@ export function ALEPH({ initial }: {
4646
if (url.pagePath !== '') {
4747
const ctree: { id: string, Component?: ComponentType<any> }[] = pageModuleTree.map(({ id }) => ({ id }))
4848
const imports = pageModuleTree.map(async mod => {
49-
const { default: C } = await import(getModuleImportUrl(baseUrl, mod, e.forceFetch))
49+
const { default: C } = await import(getModuleImportUrl(baseUrl, mod, e.forceRefetch))
5050
if (mod.asyncDeps) {
5151
// import async dependencies
52-
for (const dep of mod.asyncDeps) {
53-
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }, e.forceFetch))
52+
for (const dep of mod.asyncDeps.filter(({ url }) => reStyleModuleExt.test(url))) {
53+
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }, e.forceRefetch))
54+
}
55+
if (mod.asyncDeps.filter(({ url }) => url.startsWith('#useDeno.')).length > 0) {
56+
import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js` + (e.forceRefetch ? `?t=${Date.now()}` : '')).then(({ default: data }) => {
57+
if (util.isPlainObject(data)) {
58+
for (const key in data) {
59+
const useDenoUrl = `useDeno://${url.pathname}?${url.query.toString()}#${key}`
60+
Object.assign(window, { [useDenoUrl]: data[key] })
61+
}
62+
}
63+
})
5464
}
5565
}
5666
const pc = ctree.find(pc => pc.id === mod.id)
@@ -105,7 +115,7 @@ export function ALEPH({ initial }: {
105115
if (mod.id.startsWith('/pages/')) {
106116
const { routing } = ref.current
107117
routing.update(mod)
108-
events.emit('popstate', { type: 'popstate', forceFetch: true })
118+
events.emit('popstate', { type: 'popstate', forceRefetch: true })
109119
}
110120
break
111121
}
@@ -128,16 +138,27 @@ export function ALEPH({ initial }: {
128138
break
129139
}
130140
}
131-
const onFetchPageModule = async ({ url: pathname }: { url: string }) => {
132-
const [url, pageModuleTree] = routing.createRouter({ pathname })
141+
const onFetchPageModule = async ({ href }: { href: string }) => {
142+
const [pathname, search] = href.split('?')
143+
const [url, pageModuleTree] = routing.createRouter({ pathname, search })
133144
if (url.pagePath !== '') {
134145
const imports = pageModuleTree.map(async mod => {
135146
await import(getModuleImportUrl(baseUrl, mod))
136147
if (mod.asyncDeps) {
137148
// import async dependencies
138-
for (const dep of mod.asyncDeps) {
149+
for (const dep of mod.asyncDeps.filter(({ url }) => reStyleModuleExt.test(url))) {
139150
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }))
140151
}
152+
if (mod.asyncDeps.filter(({ url }) => url.startsWith('#useDeno.')).length > 0) {
153+
import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js`).then(({ default: data }) => {
154+
if (util.isPlainObject(data)) {
155+
for (const key in data) {
156+
const useDenoUrl = `useDeno://${url.pathname}?${url.query.toString()}#${key}`
157+
Object.assign(window, { [useDenoUrl]: data[key] })
158+
}
159+
}
160+
})
161+
}
141162
}
142163
})
143164
await Promise.all(imports)
@@ -193,6 +214,6 @@ export async function redirect(url: string, replace?: boolean) {
193214
events.emit('popstate', { type: 'popstate', scrollTo: 0 })
194215
}
195216

196-
export function getModuleImportUrl(baseUrl: string, mod: RouteModule, forceFetch = false) {
197-
return util.cleanPath(baseUrl + '/_aleph/' + util.trimSuffix(mod.id, '.js') + `.${mod.hash.slice(0, hashShort)}.js` + (forceFetch ? `?t=${Date.now()}` : ''))
217+
export function getModuleImportUrl(baseUrl: string, mod: RouteModule, forceRefetch = false) {
218+
return util.cleanPath(baseUrl + '/_aleph/' + util.trimSuffix(mod.id, '.js') + `.${mod.hash.slice(0, hashShort)}.js` + (forceRefetch ? `?t=${Date.now()}` : ''))
198219
}

bootstrap.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { ComponentType } from 'https://esm.sh/react'
22
import { hydrate, render } from 'https://esm.sh/react-dom'
33
import { ALEPH, getModuleImportUrl } from './aleph.ts'
44
import { Route, RouteModule, Routing } from './routing.ts'
5-
import { reModuleExt } from './util.ts'
5+
import { reModuleExt, reStyleModuleExt } from './util.ts'
66

77
export default async function bootstrap({
88
routes,
@@ -25,10 +25,10 @@ export default async function bootstrap({
2525
const [url, pageModuleTree] = routing.createRouter()
2626
const pageComponentTree: { id: string, Component?: ComponentType }[] = pageModuleTree.map(({ id }) => ({ id }))
2727
const imports = [...preloadModules, ...pageModuleTree].map(async mod => {
28-
const { default: C, __pageProps } = await import(getModuleImportUrl(baseUrl, mod))
28+
const { default: C } = await import(getModuleImportUrl(baseUrl, mod))
2929
if (mod.asyncDeps) {
3030
// import async dependencies
31-
for (const dep of mod.asyncDeps) {
31+
for (const dep of mod.asyncDeps.filter(({ url }) => reStyleModuleExt.test(url))) {
3232
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }))
3333
}
3434
}
@@ -49,9 +49,11 @@ export default async function bootstrap({
4949
})
5050
await Promise.all(imports)
5151

52-
const ssrData = JSON.parse(ssrDataEl.innerText)
53-
for (const key in ssrData) {
54-
Object.assign(window, { [`useDeno://${url.pathname}?${url.query.toString()}#${key}`]: ssrData[key] })
52+
if (ssrDataEl) {
53+
const ssrData = JSON.parse(ssrDataEl.innerText)
54+
for (const key in ssrData) {
55+
Object.assign(window, { [`useDeno://${url.pathname}?${url.query.toString()}#${key}`]: ssrData[key] })
56+
}
5557
}
5658

5759
const el = React.createElement(

link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function Link({
4040
}, [currentPathname, to])
4141
const prefetch = useCallback(() => {
4242
if (!util.isHttpUrl(href) && href !== currentHref && !fetchedPageModules.has(href)) {
43-
events.emit('fetch-page-module', { url: href })
43+
events.emit('fetch-page-module', { href })
4444
fetchedPageModules.add(href)
4545
}
4646
}, [href, currentHref])

0 commit comments

Comments
 (0)