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

Commit fc551c4

Browse files
author
Je
committed
refactor: cleanup
1 parent 06ddf90 commit fc551c4

File tree

12 files changed

+154
-144
lines changed

12 files changed

+154
-144
lines changed

app.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import React, { ComponentType, createContext, useCallback, useEffect, useState } from 'https://esm.sh/react'
2-
import { DataContext } from './data.ts'
1+
import React, { ComponentType, useCallback, useEffect, useState } from 'https://esm.sh/react'
2+
import { AppManifestContext, DataContext, RouterContext } from './context.ts'
33
import { E404Page, E501App, E501Page } from './error.ts'
44
import events from './events.ts'
5-
import route from './route.ts'
6-
import { RouterContext } from './router.ts'
5+
import { createRouter } from './router.ts'
76
import type { AppManifest, Module, RouterURL } from './types.ts'
87
import util, { hashShort } from './util.ts'
98

10-
export const AppManifestContext = createContext<AppManifest>({
11-
baseUrl: '/',
12-
defaultLocale: 'en',
13-
locales: {},
14-
})
15-
AppManifestContext.displayName = 'AppManifestContext'
16-
179
export function ALEPH({ initial }: {
1810
initial: {
1911
manifest: AppManifest
@@ -48,7 +40,7 @@ export function ALEPH({ initial }: {
4840
})
4941
const onpopstate = useCallback(async () => {
5042
const { baseUrl, defaultLocale, locales } = manifest
51-
const url = route(
43+
const url = createRouter(
5244
baseUrl,
5345
Object.keys(pageModules),
5446
{
@@ -167,6 +159,38 @@ export function ALEPH({ initial }: {
167159
)
168160
}
169161

162+
export async function redirect(url: string, replace: boolean) {
163+
const { location, document, history } = window as any
164+
165+
if (util.isHttpUrl(url)) {
166+
location.href = url
167+
return
168+
}
169+
170+
url = util.cleanPath(url)
171+
if (location.protocol === 'file:') {
172+
const dataEl = document.getElementById('ssr-data')
173+
if (dataEl) {
174+
const ssrData = JSON.parse(dataEl.innerHTML)
175+
if (ssrData && 'url' in ssrData) {
176+
const { url: { pagePath: initialPagePath } } = ssrData
177+
location.href = location.href.replace(
178+
`/${util.trimPrefix(initialPagePath, '/') || 'index'}.html`,
179+
`/${util.trimPrefix(url, '/') || 'index'}.html`
180+
)
181+
}
182+
}
183+
return
184+
}
185+
186+
if (replace) {
187+
history.replaceState(null, '', url)
188+
} else {
189+
history.pushState(null, '', url)
190+
}
191+
events.emit('popstate', { type: 'popstate' })
192+
}
193+
170194
export function getModuleImportUrl(baseUrl: string, { moduleId, hash }: Module) {
171195
return util.cleanPath(baseUrl + '/_aleph/' + moduleId.replace(/\.js$/, `.${hash.slice(0, hashShort)}.js`))
172196
}

bootstrap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'https://esm.sh/react'
22
import { hydrate, render } from 'https://esm.sh/react-dom'
33
import { ALEPH, getModuleImportUrl } from './app.ts'
44
import { ErrorBoundary } from './error.ts'
5-
import route from './route.ts'
5+
import { createRouter } from './router.ts'
66
import type { AppManifest, Module, RouterURL } from './types.ts'
77
import util from './util.ts'
88

@@ -29,7 +29,7 @@ export default async function bootstrap({
2929
throw new Error("invalid ssr-data")
3030
}
3131
} else {
32-
url = route(
32+
url = createRouter(
3333
baseUrl,
3434
Object.keys(pageModules),
3535
{

context.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createContext } from 'https://esm.sh/react'
2+
import type { AppManifest, RouterURL } from './types.ts'
3+
4+
export const AppManifestContext = createNamedContext<AppManifest>(
5+
'AppManifestContext',
6+
{
7+
baseUrl: '/',
8+
defaultLocale: 'en',
9+
locales: {},
10+
}
11+
)
12+
13+
export const DataContext = createNamedContext<Record<string, any>>(
14+
'DataContext',
15+
{}
16+
)
17+
18+
export const RouterContext = createNamedContext<RouterURL>(
19+
'RouterContext',
20+
{
21+
locale: 'en',
22+
pagePath: '/',
23+
pathname: '/',
24+
params: {},
25+
query: new URLSearchParams(),
26+
}
27+
)
28+
29+
function createNamedContext<T>(name: string, defaultValue: T) {
30+
const c = createContext(defaultValue)
31+
c.displayName = name
32+
return c
33+
}

data.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

hooks.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { ComponentType, useContext } from 'https://esm.sh/react'
2+
import { DataContext, RouterContext } from './context.ts'
3+
import type { RouterURL } from './types.ts'
4+
5+
export function useData(key: string) {
6+
const data = useContext(DataContext)
7+
return data[key]
8+
}
9+
10+
export function useRouter() {
11+
return useContext(RouterContext)
12+
}
13+
14+
export function withRouter(Component: ComponentType<{ url: RouterURL }>) {
15+
function WithRouter(props: any) {
16+
const url = useRouter()
17+
return React.createElement(Component, Object.assign({}, props, { url }))
18+
}
19+
return WithRouter
20+
}

link.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Children, cloneElement, CSSProperties, isValidElement, MouseEvent, PropsWithChildren, useCallback, useEffect, useMemo, useRef } from 'https://esm.sh/react'
2-
import { redirect, useRouter } from './router.ts'
2+
import { redirect } from './app.ts'
3+
import { useRouter } from './hooks.ts'
34
import util from './util.ts'
45

56
interface LinkProps {

mod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export { AppManifestContext } from './app.ts'
2-
export * from './data.ts'
1+
export { redirect } from './app.ts'
2+
export * from './context.ts'
33
export { ErrorPage } from './error.ts'
44
export { default as Head, SEO, Viewport } from './head.ts'
5+
export * from './hooks.ts'
56
export { default as Link } from './link.ts'
6-
export * from './router.ts'
77

project.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { minify } from 'https://esm.sh/terser'
22
import { EventEmitter } from './events.ts'
33
import { createHtml } from './html.ts'
44
import log from './log.ts'
5-
import route from './route.ts'
5+
import { createRouter } from './router.ts'
66
import { colors, ensureDir, path, Sha1, walk } from './std.ts'
77
import { compile } from './tsc/compile.ts'
88
import type { APIHandle, Config, Location, RouterURL } from './types.ts'
@@ -140,7 +140,7 @@ export default class Project {
140140
modId = './data/index.js'
141141
}
142142
if (!this.#modules.has(modId)) {
143-
console.warn(`can't get the module by path '${pathname}(${modId})'`)
143+
log.warn(`can't get the module by path '${pathname}(${modId})'`)
144144
}
145145
return this.getModule(modId)
146146
}
@@ -176,7 +176,7 @@ export default class Project {
176176

177177
async getPageHtml(location: Location): Promise<[number, string]> {
178178
const { baseUrl, defaultLocale } = this.config
179-
const url = route(
179+
const url = createRouter(
180180
baseUrl,
181181
Array.from(this.#pageModules.keys()),
182182
{
@@ -524,7 +524,6 @@ export default class Project {
524524
this._clearPageRenderCache(moduleId)
525525
} else {
526526
const pagePath = util.trimPrefix(moduleId, './pages').replace(reModuleExt, '').replace(/\s+/g, '-').replace(/\/index$/i, '') || '/'
527-
console.log(">", pagePath)
528527
this.#pageModules.set(pagePath, { moduleId, rendered: new Map() })
529528
}
530529
}
@@ -556,7 +555,7 @@ export default class Project {
556555
}
557556
})
558557
}).catch(err => {
559-
log.error("compile", './' + path, err.message)
558+
log.error(`compile(./${path}):`, err.message)
560559
})
561560
} else if (this.#modules.has(moduleId)) {
562561
this.#modules.delete(moduleId)

renderer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { ComponentType } from 'https://esm.sh/react'
22
import { renderToString } from 'https://esm.sh/react-dom/server'
3-
import { DataContext } from './data.ts'
3+
import { DataContext, RouterContext } from './context.ts'
44
import { E501App, E501Page, ErrorBoundary } from './error.ts'
5-
import { RouterContext } from './router.ts'
65
import type { RouterURL } from './types.ts'
76
import util from './util.ts'
87
export { renderHead } from './head.ts'

route.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)