diff --git a/packages/web-runtime/src/router/index.js b/packages/web-runtime/src/router/index.js index 3cd3b539371..3557be04182 100644 --- a/packages/web-runtime/src/router/index.js +++ b/packages/web-runtime/src/router/index.js @@ -108,20 +108,30 @@ export const router = patchRouter( ) export const buildUrl = (pathname) => { - const baseUrl = new URL(window.location.href.split('#')[0]) - if (baseUrl.pathname.endsWith('/index.html')) { - baseUrl.pathname = baseUrl.pathname.split('/').slice(0, -1).filter(Boolean).join('/') + const isHistoryMode = !!base + let baseUrl + if (isHistoryMode) { + // in history mode we can't determine the baseUrl, it must be provided by the document + baseUrl = new URL(base.href) + } else { + // in hash mode, determine baseUrl by removing `/index.html` and following parts + baseUrl = new URL(window.location.href.split('#')[0]) + if (baseUrl.pathname.endsWith('/index.html')) { + baseUrl.pathname = baseUrl.pathname.split('/').slice(0, -1).filter(Boolean).join('/') + } } + /** + * build full url by either + * - concatenating baseUrl and pathname (for unknown/non-router urls, e.g. `oidc-callback.html`) or + * - resolving via router (for known routes) + */ if (/\.(html?)$/i.test(pathname)) { - baseUrl.pathname = [ - ...(base ? new URL(base.href) : baseUrl).pathname.split('/'), - ...pathname.split('/') - ] + baseUrl.pathname = [...baseUrl.pathname.split('/'), ...pathname.split('/')] .filter(Boolean) .join('/') } else { - baseUrl[base ? 'pathname' : 'hash'] = router.resolve(pathname).href + baseUrl[isHistoryMode ? 'pathname' : 'hash'] = router.resolve(pathname).href } return baseUrl.href