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

Commit e5505ba

Browse files
committed
fix: fix api routing (#218)
1 parent 4e62dc3 commit e5505ba

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

framework/core/routing.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,12 @@ export function rewriteURL(reqUrl: string, baseURL: string, rewrites: Record<str
260260
const to = rewrites[path]
261261
const [params, ok] = matchPath(path, decodeURI(url.pathname))
262262
if (ok) {
263-
const { searchParams } = url
264-
url.href = 'http://localhost' + util.cleanPath(to.replace(/:(.+)(\/|&|$)/g, (s, k, e) => {
263+
url.pathname = util.cleanPath(to.replace(/:(.+)(\/|&|$)/g, (s, k, e) => {
265264
if (k in params) {
266265
return params[k] + e
267266
}
268267
return s
269268
}))
270-
for (const [key, value] of url.searchParams.entries()) {
271-
searchParams.append(key, value)
272-
}
273-
url.search = searchParams.toString()
274269
break
275270
}
276271
}

framework/react/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ export default function Router({
129129
}
130130
}
131131
const onFetchPageModule = async ({ href }: { href: string }) => {
132-
const [pathname, search] = href.split('?')
133-
const [url, nestedModules] = routing.createRouter({ pathname, search })
132+
const [pathname] = href.split('?')
133+
const [url, nestedModules] = routing.createRouter({ pathname })
134134
if (url.pagePath !== '') {
135135
nestedModules.map(mod => {
136136
importModule(baseURL, mod.url)

server/app.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ import log from '../shared/log.ts'
3131
import util from '../shared/util.ts'
3232
import type {
3333
Config,
34-
LoaderPlugin,
35-
LoaderTransformOutput,
3634
RouterURL,
3735
ServerApplication,
38-
TransformFn
3936
} from '../types.ts'
4037
import { VERSION } from '../version.ts'
4138
import { Bundler, bundlerRuntimeCode } from './bundler.ts'
@@ -68,6 +65,8 @@ export type DependencyDescriptor = {
6865
isDynamic?: boolean
6966
}
7067

68+
type TransformFn = (url: string, code: string) => string
69+
7170
/** The application class for aleph server. */
7271
export class Application implements ServerApplication {
7372
readonly workingDir: string
@@ -345,10 +344,12 @@ export class Application implements ServerApplication {
345344
if (trimModuleExt(url) === '/app') {
346345
this.#renderer.clearCache()
347346
} else if (url.startsWith('/pages/')) {
348-
this.#renderer.clearCache(toPagePath(url))
349-
this.#pageRouting.removeRoute(toPagePath(url))
347+
const [pagePath] = this.createRouteUpdate(url)
348+
this.#renderer.clearCache(pagePath)
349+
this.#pageRouting.removeRoute(pagePath)
350350
} else if (url.startsWith('/api/')) {
351-
this.#apiRouting.removeRoute(toPagePath(url))
351+
const [pagePath] = this.createRouteUpdate(url)
352+
this.#apiRouting.removeRoute(pagePath)
352353
}
353354
this.#modules.delete(url)
354355
if (this.isHMRable(url)) {
@@ -435,7 +436,9 @@ export class Application implements ServerApplication {
435436
const [url, nestedModules] = router
436437
if (url.pagePath !== '') {
437438
const { url: moduleUrl } = nestedModules.pop()!
438-
return [url, this.#modules.get(moduleUrl)!]
439+
if (this.#modules.has(moduleUrl)) {
440+
return [url, this.#modules.get(moduleUrl)!]
441+
}
439442
}
440443
}
441444
return null
@@ -473,8 +476,8 @@ export class Application implements ServerApplication {
473476
return null
474477
}
475478

476-
const cacheKey = loc.pathname + (loc.search || '')
477-
const [_, data] = await this.#renderer.useCache(pagePath, cacheKey, async () => {
479+
const path = loc.pathname + (loc.search || '')
480+
const [_, data] = await this.#renderer.useCache(pagePath, path, async () => {
478481
return await this.#renderer.renderPage(router, nestedModules)
479482
})
480483
return data

server/server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ export class Server {
149149

150150
// serve APIs
151151
if (pathname.startsWith('/api/')) {
152-
const route = app.getAPIRoute({ pathname, search: url.searchParams.toString() })
152+
const route = app.getAPIRoute({
153+
pathname,
154+
search: Array.from(url.searchParams.keys()).length > 0 ? '?' + url.searchParams.toString() : ''
155+
})
153156
if (route !== null) {
154157
try {
155158
const [{ params }, { jsFile, hash }] = route
@@ -170,7 +173,10 @@ export class Server {
170173
}
171174

172175
// ssr
173-
const [status, html] = await app.getPageHTML({ pathname, search: url.searchParams.toString() })
176+
const [status, html] = await app.getPageHTML({
177+
pathname,
178+
search: Array.from(url.searchParams.keys()).length > 0 ? '?' + url.searchParams.toString() : ''
179+
})
174180
req.status(status).send(html, 'text/html; charset=utf-8')
175181
} catch (err) {
176182
req.status(500).send(

0 commit comments

Comments
 (0)