🔧 fix: #1671 mount produce incorrect url

This commit is contained in:
saltyaom
2026-01-14 10:56:47 +07:00
parent c047fb410b
commit a555850247
6 changed files with 63 additions and 6 deletions
+1
View File
@@ -1,5 +1,6 @@
# 1.4.22
Bug fix:
- [#1671](https://github.com/elysiajs/elysia/issues/1671) mount() produces incorrect URL path when Elysia instance has prefix option
- ValueError with summary missing types
- Elysia not using Bun.routes
+16 -1
View File
@@ -1,3 +1,18 @@
import { Elysia } from '../src'
const app = new Elysia().get('/', () => 'a').listen(3000)
const sdkApp = new Elysia({ prefix: '/sdk' }).mount(
'/problems-domain',
(request) => {
console.log(request.url)
return Response.json({ path: new URL(request.url).pathname })
}
)
const app = new Elysia().use(sdkApp)
const response = await app
.handle(new Request('http://localhost/sdk/problems-domain/problems'))
.then((x) => x.text())
console.log(response)
+1 -1
View File
@@ -33,7 +33,7 @@ const createContext = (
const getQi =
`const u=request.url,` +
`s=u.indexOf('/',${standardHostname ? 11 : 7}),` +
`qi=u.indexOf('?', s + 1)\n`
`qi=u.indexOf('?',s+1)\n`
const needsQuery =
inference.query ||
+5 -1
View File
@@ -5613,8 +5613,12 @@ export default class Elysia<
throw new Error('Invalid handler')
})()
const length = path.length - (path.endsWith('*') ? 1 : 0)
const fullPath =
typeof path === 'string' && this.config.prefix
? this.config.prefix + path
: path
const length = fullPath.length - (path.endsWith('*') ? 1 : 0)
const handler: Handler = ({ request, path }) =>
handle(
new Request(
+7 -3
View File
@@ -28,9 +28,13 @@ import { ElysiaFile } from './universal/file'
export const hasHeaderShorthand = 'toJSON' in new Headers()
export const replaceUrlPath = (url: string, pathname: string) => {
const urlObject = new URL(url)
urlObject.pathname = pathname
return urlObject.toString()
const pathStartIndex = url.indexOf('/', 11)
const queryIndex = url.indexOf('?', pathStartIndex)
if (queryIndex === -1)
return `${url.slice(0, pathStartIndex)}${pathname.charCodeAt(0) === 47 ? '' : '/'}${pathname}`
return `${url.slice(0, pathStartIndex)}${pathname.charCodeAt(0) === 47 ? '' : '/'}${pathname}${url.slice(queryIndex)}`
}
export const isClass = (v: Object) =>
+33
View File
@@ -137,4 +137,37 @@ describe('Mount', () => {
'x-test': 'test'
})
})
it('mount without prefix - strips mount path', async () => {
const app = new Elysia().mount('/sdk/problems-domain', (request) => {
return Response.json({ path: new URL(request.url).pathname })
})
const response = await app
.handle(
new Request('http://localhost/sdk/problems-domain/problems')
)
.then((x) => x.json() as Promise<{ path: string }>)
expect(response.path).toBe('/problems')
})
it('mount with prefix - should strip both prefix and mount path', async () => {
const sdkApp = new Elysia({ prefix: '/sdk' }).mount(
'/problems-domain',
(request) => {
return Response.json({ path: new URL(request.url).pathname })
}
)
const app = new Elysia().use(sdkApp)
const response = await app
.handle(
new Request('http://localhost/sdk/problems-domain/problems')
)
.then((x) => x.json() as Promise<{ path: string }>)
expect(response.path).toBe('/problems')
})
})