From df343e0eaffecbef7cfbc7cd060401ac1916d037 Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Wed, 16 Feb 2022 13:15:18 +0100 Subject: [PATCH] Improve readability of buildUrl function --- packages/web-runtime/src/router/index.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/web-runtime/src/router/index.js b/packages/web-runtime/src/router/index.js index 3cd3b539371..ac969f0722b 100644 --- a/packages/web-runtime/src/router/index.js +++ b/packages/web-runtime/src/router/index.js @@ -108,20 +108,29 @@ export const router = patchRouter( ) export const buildUrl = (pathname) => { + const isHistoryMode = !!base 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('/') + if (isHistoryMode) { + // in history mode we can't determine the base path, it must be provided by the document + baseUrl.pathname = new URL(base.href).pathname + } else { + // in hash mode, auto-determine the base path by removing `/index.html` + 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