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

Commit 8d2f7be

Browse files
committed
Update RouterURL types
1 parent b3e5577 commit 8d2f7be

File tree

11 files changed

+87
-87
lines changed

11 files changed

+87
-87
lines changed

framework/core/hmr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ socket.addEventListener('message', ({ data }: { data?: string }) => {
6969
type,
7070
url,
7171
updateUrl,
72-
pagePath,
72+
routePath,
7373
isIndex,
7474
useDeno,
7575
} = JSON.parse(data)
7676
switch (type) {
7777
case 'add':
7878
events.emit('add-module', {
7979
url,
80-
pagePath,
80+
routePath,
8181
isIndex,
8282
useDeno,
8383
})

framework/core/routing.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ export class Routing {
132132

133133
let locale = this._defaultLocale
134134
let pathname = decodeURI(url.pathname)
135-
let pagePath = ''
136-
let params = new URLSearchParams()
135+
let routePath = ''
136+
let params = {} as Record<string, string>
137137
let nestedModules: RouteModule[] = []
138138

139139
if (pathname !== '/' && this._locales.length > 0) {
@@ -145,35 +145,30 @@ export class Routing {
145145
}
146146
}
147147

148-
this._lookup(routePath => {
149-
const path = routePath.map(r => r.path).join('')
148+
this._lookup(route => {
149+
const path = route.map(r => r.path).join('')
150150
const [p, ok] = matchPath(path, pathname)
151151
if (ok) {
152-
nestedModules = routePath.map(r => r.module)
153-
const c = routePath[routePath.length - 1].children?.find(c => c.path === '/')
152+
nestedModules = route.map(r => r.module)
153+
const c = route[route.length - 1].children?.find(c => c.path === '/')
154154
if (c) {
155155
nestedModules.push(c.module)
156156
}
157-
pagePath = path
158-
Object.entries(p).forEach(([key, value]) => {
159-
params.append(key, value)
160-
})
157+
routePath = path
158+
params = p
161159
return false
162160
}
163161
}, true)
164-
165-
for (const name of url.searchParams.keys()) {
166-
const values = url.searchParams.getAll(name)
167-
values.forEach(value => params.append(name, value))
168-
}
162+
url.searchParams.sort()
169163

170164
return [
171165
{
172166
baseURL: this._baseURL,
173167
locale,
174168
pathname,
175-
pagePath,
169+
routePath,
176170
params,
171+
query: url.searchParams,
177172
push: (url: string) => redirect(url),
178173
replace: (url: string) => redirect(url, true),
179174
},
@@ -244,9 +239,10 @@ export function createBlankRouterURL(baseURL = '/', locale = 'en'): RouterURL {
244239
return {
245240
baseURL,
246241
locale,
247-
pagePath: '',
242+
routePath: '',
248243
pathname: '/',
249-
params: new URLSearchParams(),
244+
params: {},
245+
query: new URLSearchParams(),
250246
push: () => void 0,
251247
replace: () => void 0,
252248
}

framework/core/routing_test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,101 +83,101 @@ Deno.test(`routing`, () => {
8383
const [router, nestedModules] = routing.createRouter({ pathname: '/' })
8484
assertEquals(router.locale, 'en')
8585
assertEquals(router.pathname, '/')
86-
assertEquals(router.pagePath, '/')
86+
assertEquals(router.routePath, '/')
8787
assertEquals(nestedModules.map(m => m.url), ['/pages/index.tsx'])
8888
}
8989

9090
{
9191
const [router, nestedModules] = routing.createRouter({ pathname: '/zh-CN' })
9292
assertEquals(router.locale, 'zh-CN')
9393
assertEquals(router.pathname, '/')
94-
assertEquals(router.pagePath, '/')
94+
assertEquals(router.routePath, '/')
9595
assertEquals(nestedModules.map(m => m.url), ['/pages/index.tsx'])
9696
}
9797

9898
{
9999
const [router, nestedModules] = routing.createRouter({ pathname: '/Hello World' })
100100
assertEquals(router.locale, 'en')
101101
assertEquals(router.pathname, '/hello-world')
102-
assertEquals(router.pagePath, '/hello-world')
102+
assertEquals(router.routePath, '/hello-world')
103103
assertEquals(nestedModules.map(m => m.url), ['/pages/hello-world.tsx'])
104104
}
105105

106106
{
107107
const [router, nestedModules] = routing.createRouter({ pathname: '/你好世界' })
108108
assertEquals(router.locale, 'zh-CN')
109109
assertEquals(router.pathname, '/hello-world')
110-
assertEquals(router.pagePath, '/hello-world')
110+
assertEquals(router.routePath, '/hello-world')
111111
assertEquals(nestedModules.map(m => m.url), ['/pages/hello-world.tsx'])
112112
}
113113

114114
{
115115
const [router, nestedModules] = routing.createRouter({ pathname: '/blog' })
116116
assertEquals(router.locale, 'en')
117117
assertEquals(router.pathname, '/blog')
118-
assertEquals(router.pagePath, '/blog')
118+
assertEquals(router.routePath, '/blog')
119119
assertEquals(nestedModules.map(m => m.url), ['/pages/blog.tsx', '/pages/blog/index.tsx'])
120120
}
121121

122122
{
123123
const [router, nestedModules] = routing.createRouter({ pathname: '/zh-CN/blog' })
124124
assertEquals(router.locale, 'zh-CN')
125125
assertEquals(router.pathname, '/blog')
126-
assertEquals(router.pagePath, '/blog')
126+
assertEquals(router.routePath, '/blog')
127127
assertEquals(nestedModules.map(m => m.url), ['/pages/blog.tsx', '/pages/blog/index.tsx'])
128128
}
129129

130130
{
131131
const [router, nestedModules] = routing.createRouter({ pathname: '/blog/hello-world' })
132132
assertEquals(router.pathname, '/blog/hello-world')
133-
assertEquals(router.pagePath, '/blog/[slug]')
134-
assertEquals(router.params.get('slug'), 'hello-world')
133+
assertEquals(router.routePath, '/blog/[slug]')
134+
assertEquals(router.params['slug'], 'hello-world')
135135
assertEquals(nestedModules.map(m => m.url), ['/pages/blog.tsx', '/pages/blog/[slug].tsx'])
136136
}
137137

138138
{
139139
const [router, nestedModules] = routing.createRouter({ pathname: '/user' })
140140
assertEquals(router.pathname, '/user')
141-
assertEquals(router.pagePath, '/user')
141+
assertEquals(router.routePath, '/user')
142142
assertEquals(nestedModules.map(m => m.url), ['/pages/user.tsx', '/pages/user/index.tsx'])
143143
}
144144

145145
{
146146
const [router, nestedModules] = routing.createRouter({ pathname: '/user?name=aleph' })
147147
assertEquals(router.pathname, '/user')
148-
assertEquals(router.pagePath, '/user')
149-
assertEquals(router.params.get('name'), 'aleph')
148+
assertEquals(router.routePath, '/user')
149+
assertEquals(router.query.get('name'), 'aleph')
150150
assertEquals(nestedModules.map(m => m.url), ['/pages/user.tsx', '/pages/user/index.tsx'])
151151
}
152152

153153
{
154154
const [router, nestedModules] = routing.createRouter({ pathname: '/user/projects' })
155155
assertEquals(router.pathname, '/user/projects')
156-
assertEquals(router.pagePath, '/user/[...all]')
157-
assertEquals(router.params.get('all'), 'projects')
156+
assertEquals(router.routePath, '/user/[...all]')
157+
assertEquals(router.params['all'], 'projects')
158158
assertEquals(nestedModules.map(m => m.url), ['/pages/user.tsx', '/pages/user/[...all].tsx'])
159159
}
160160

161161
{
162162
const [router, nestedModules] = routing.createRouter({ pathname: '/user/settings/profile' })
163163
assertEquals(router.pathname, '/user/settings/profile')
164-
assertEquals(router.pagePath, '/user/[...all]')
165-
assertEquals(router.params.get('all'), 'settings/profile')
164+
assertEquals(router.routePath, '/user/[...all]')
165+
assertEquals(router.params['all'], 'settings/profile')
166166
assertEquals(nestedModules.map(m => m.url), ['/pages/user.tsx', '/pages/user/[...all].tsx'])
167167
}
168168

169169
{
170170
const [router, nestedModules] = routing.createRouter({ pathname: '/user/settings/security' })
171171
assertEquals(router.pathname, '/user/settings/security')
172-
assertEquals(router.pagePath, '/user/[...all]')
173-
assertEquals(router.params.get('all'), 'settings/security')
172+
assertEquals(router.routePath, '/user/[...all]')
173+
assertEquals(router.params['all'], 'settings/security')
174174
assertEquals(nestedModules.map(m => m.url), ['/pages/user.tsx', '/pages/user/[...all].tsx'])
175175
}
176176

177177
{
178178
const [router, nestedModules] = routing.createRouter({ pathname: '/null' })
179179
assertEquals(router.pathname, '/null')
180-
assertEquals(router.pagePath, '')
180+
assertEquals(router.routePath, '')
181181
assertEquals(nestedModules.map(m => m.url), [])
182182
}
183183
})

framework/react/components/Anchor.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function Anchor(props: AnchorProps) {
3939
const prefetching = useMemo(() => relKeys.includes('prefetch'), [relKeys])
4040
const replace = useMemo(() => relKeys.includes('replace'), [relKeys])
4141
const isNav = useMemo(() => relKeys.includes('nav'), [relKeys])
42-
const { pathname, params } = useRouter()
42+
const { pathname, params, query } = useRouter()
4343
const href = useMemo(() => {
4444
if (!util.isNEString(propHref)) {
4545
return ''
@@ -66,17 +66,13 @@ export default function Anchor(props: AnchorProps) {
6666
}
6767
if (q) {
6868
const search = new URLSearchParams(q)
69-
for (const key of search.keys()) {
70-
if (
71-
!params.has(key) ||
72-
search.getAll(key).join(',') !== params.getAll(key).join(',')
73-
) {
74-
return false
75-
}
69+
search.sort()
70+
if (search.toString() !== query.toString()) {
71+
return false
7672
}
7773
}
7874
return true
79-
}, [pathname, params, propHref])
75+
}, [pathname, params, query, propHref])
8076
const className = useMemo(() => {
8177
if (!isNav || !isCurrent) {
8278
return propClassName

framework/react/components/Router.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function Router({
4949
const onpopstate = useCallback(async (e: any) => {
5050
const { baseURL } = routing
5151
const [url, nestedModules] = routing.createRouter()
52-
if (url.pagePath !== '') {
52+
if (url.routePath !== '') {
5353
const imports = nestedModules.map(async mod => {
5454
const { default: Component } = await importModule(baseURL, mod.url, e.forceRefetch)
5555
return {
@@ -82,7 +82,7 @@ export default function Router({
8282
useEffect(() => {
8383
const isDev = !('__ALEPH' in window)
8484
const { baseURL } = routing
85-
const onAddModule = async (mod: RouteModule & { pagePath?: string, isIndex?: boolean }) => {
85+
const onAddModule = async (mod: RouteModule & { routePath?: string, isIndex?: boolean }) => {
8686
switch (mod.url) {
8787
case '/404.js': {
8888
const { default: Component } = await importModule(baseURL, mod.url, true)
@@ -103,9 +103,9 @@ export default function Router({
103103
break
104104
}
105105
default: {
106-
const { pagePath, url, ...rest } = mod
107-
if (pagePath) {
108-
routing.update(pagePath, url, rest)
106+
const { routePath, url, ...rest } = mod
107+
if (routePath) {
108+
routing.update(routePath, url, rest)
109109
events.emit('popstate', { type: 'popstate', forceRefetch: true })
110110
}
111111
break
@@ -131,7 +131,7 @@ export default function Router({
131131
const onFetchPageModule = async ({ href }: { href: string }) => {
132132
const [pathname] = href.split('?')
133133
const [url, nestedModules] = routing.createRouter({ pathname })
134-
if (url.pagePath !== '') {
134+
if (url.routePath !== '') {
135135
nestedModules.map(mod => {
136136
importModule(baseURL, mod.url)
137137
})

framework/react/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AsyncUseDenoError } from './components/ErrorBoundary.ts'
1010
*
1111
* ```tsx
1212
* export default function App() {
13-
* const { locale, pathname, pagePath, params, query } = useRouter()
13+
* const { locale, pathname, routePath, params, query } = useRouter()
1414
* return <p>{pathname}</p>
1515
* }
1616
* ```

server/api.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let gzip: ((data: Uint8Array) => Uint8Array) | null = null
99

1010
export class Request implements APIRequest {
1111
#req: ServerRequest
12-
#params: URLSearchParams
12+
#params: Record<string, string>
13+
#query: URLSearchParams
1314
#cookies: ReadonlyMap<string, string>
1415
#resp = {
1516
status: 200,
@@ -19,9 +20,10 @@ export class Request implements APIRequest {
1920
done: false
2021
}
2122

22-
constructor(req: ServerRequest, params: URLSearchParams) {
23+
constructor(req: ServerRequest, params: Record<string, string>, query: URLSearchParams) {
2324
this.#req = req
2425
this.#params = params
26+
this.#query = query
2527
const cookies = new Map()
2628
this.headers.get('cookie')?.split(';').forEach(cookie => {
2729
const p = cookie.trim().split('=')
@@ -64,10 +66,14 @@ export class Request implements APIRequest {
6466
return this.#req.respond(r)
6567
}
6668

67-
get params(): URLSearchParams {
69+
get params(): Record<string, string> {
6870
return this.#params
6971
}
7072

73+
get query(): URLSearchParams {
74+
return this.#query
75+
}
76+
7177
get cookies(): ReadonlyMap<string, string> {
7278
return this.#cookies
7379
}

0 commit comments

Comments
 (0)