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

Commit 8107bb3

Browse files
committed
Improve routing
1 parent 7ca08eb commit 8107bb3

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

framework/core/routing.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import util from '../../shared/util.ts'
22
import type { RouterURL } from '../../types.d.ts'
3-
import { redirect } from './redirect.ts'
43

54
const ghostRoute: Route = { path: '', module: '' }
65

@@ -16,6 +15,7 @@ export type RoutingOptions = {
1615
locales?: string[]
1716
routes?: Route[]
1817
rewrites?: Record<string, string>
18+
redirect?: (url: string, replace?: boolean) => void
1919
}
2020

2121
export class Routing {
@@ -24,19 +24,22 @@ export class Routing {
2424
private _locales: string[]
2525
private _routes: Route[]
2626
private _rewrites?: Record<string, string>
27+
private _redirect?: (url: string, replace?: boolean) => void
2728

2829
constructor({
2930
basePath = '/',
3031
defaultLocale = 'en',
3132
locales = [],
3233
routes = [],
33-
rewrites
34+
rewrites,
35+
redirect
3436
}: RoutingOptions = {}) {
3537
this._basePath = basePath
3638
this._defaultLocale = defaultLocale
3739
this._locales = locales
3840
this._routes = routes
3941
this._rewrites = rewrites
42+
this._redirect = redirect
4043
}
4144

4245
get basePath() {
@@ -179,8 +182,8 @@ export class Routing {
179182
const qs = this.query.toString()
180183
return [this.pathname, qs].filter(Boolean).join('?')
181184
},
182-
push: (url: string) => redirect(url),
183-
replace: (url: string) => redirect(url, true),
185+
push: (url: string) => this._redirect && this._redirect(url),
186+
replace: (url: string) => this._redirect && this._redirect(url, true),
184187
},
185188
nestedModules
186189
]

framework/react/bootstrap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createElement } from 'https://esm.sh/[email protected]'
22
import { hydrate, render } from 'https://esm.sh/[email protected]'
33
import { importModule } from '../core/module.ts'
4+
import { redirect } from '../core/redirect.ts'
45
import { Routing, RoutingOptions } from '../core/routing.ts'
56
import Router, { createPageRoute, importPageModules } from './components/Router.ts'
67
import { loadSSRDataFromTag, setStaticDataRoutes } from './pagedata.ts'
@@ -15,7 +16,7 @@ export default async function bootstrap(options: BootstrapOptions) {
1516
const { basePath, defaultLocale, locales, appModule: appModuleSpcifier, routes, dataRoutes, rewrites, renderMode } = options
1617
const { document } = window as any
1718
const appModule = appModuleSpcifier ? await importModule(basePath, appModuleSpcifier) : {}
18-
const routing = new Routing({ routes, rewrites, basePath, defaultLocale, locales })
19+
const routing = new Routing({ routes, rewrites, basePath, defaultLocale, locales, redirect })
1920
const [url, nestedModules] = routing.createRouter()
2021
const components = await importPageModules(url, nestedModules)
2122
const pageRoute = createPageRoute(url, components)

0 commit comments

Comments
 (0)