diff --git a/packages/web-runtime/src/router/index.js b/packages/web-runtime/src/router/index.js index 1c6dc4c9f8e..942e13953a4 100644 --- a/packages/web-runtime/src/router/index.js +++ b/packages/web-runtime/src/router/index.js @@ -114,9 +114,12 @@ export const buildUrl = (pathname) => { } if (/\.(html?)$/i.test(pathname)) { - baseUrl.pathname = base - ? pathname - : [...baseUrl.pathname.split('/'), ...pathname.split('/')].filter(Boolean).join('/') + baseUrl.pathname = [ + ...(base ? [...new URL(base.href).pathname.split('/')] : [...baseUrl.pathname.split('/')]), + ...pathname.split('/') + ] + .filter(Boolean) + .join('/') } else { baseUrl[base ? 'pathname' : 'hash'] = router.resolve(pathname).href } diff --git a/packages/web-runtime/tests/unit/router/index.spec.ts b/packages/web-runtime/tests/unit/router/index.spec.ts index 8b55eb6d126..ee2275a7ee0 100644 --- a/packages/web-runtime/tests/unit/router/index.spec.ts +++ b/packages/web-runtime/tests/unit/router/index.spec.ts @@ -1,14 +1,17 @@ describe('buildUrl', () => { it.each` - location | base | path | expected - ${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login'} | ${'https://localhost:8080/index.php/apps/web#/login'} - ${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:8080/index.php/apps/web#/login/foo'} - ${'https://localhost:8080/index.php/apps/web/#/login'} | ${''} | ${'/bar.html'} | ${'https://localhost:8080/index.php/apps/web/bar.html'} - ${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:9200/#/login/foo'} - ${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'} - ${'https://localhost:9200/files/list/all'} | ${'/'} | ${'/login/foo'} | ${'https://localhost:9200/login/foo'} - ${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'} - ${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/bar.htm'} | ${'https://localhost:9200/bar.htm'} + location | base | path | expected + ${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login'} | ${'https://localhost:8080/index.php/apps/web#/login'} + ${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:8080/index.php/apps/web#/login/foo'} + ${'https://localhost:8080/index.php/apps/web/#/login'} | ${''} | ${'/bar.html'} | ${'https://localhost:8080/index.php/apps/web/bar.html'} + ${'https://localhost:8080/index.php/apps/web/#/login'} | ${'/index.php/apps/web/foo'} | ${'/bar'} | ${'https://localhost:8080/index.php/apps/web/foo/bar'} + ${'https://localhost:8080/index.php/apps/web/#/login'} | ${'/index.php/apps/web/foo'} | ${'/bar.html'} | ${'https://localhost:8080/index.php/apps/web/foo/bar.html'} + ${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:9200/#/login/foo'} + ${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'} + ${'https://localhost:9200/files/list/all'} | ${'/'} | ${'/login/foo'} | ${'https://localhost:9200/login/foo'} + ${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/login/foo'} | ${'https://localhost:9200/foo/login/foo'} + ${'https://localhost:9200/files/list/all'} | ${'/'} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'} + ${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/bar.html'} | ${'https://localhost:9200/foo/bar.html'} `('$path -> $expected', async ({ location, base, path, expected }) => { delete window.location window.location = new URL(location) as any