diff --git a/packages/signalizejs/src/modules/spa.js b/packages/signalizejs/src/modules/spa.js index 8056a3c..2c9f352 100755 --- a/packages/signalizejs/src/modules/spa.js +++ b/packages/signalizejs/src/modules/spa.js @@ -20,6 +20,7 @@ export default async ({ params, resolve, root }, config) => { const spaCacheHeader = config?.cacheHeader ?? `${spaHeaderPrefix}Cache-Control`; const spaAppVersionHeader = config?.appVersionHeader ?? `${spaHeaderPrefix}App-Version`; const spaTransitionsHeader = config?.appVersionHeader ?? `${spaHeaderPrefix}Transitions`; + const spaReloadHeader = config?.reloadHeader ?? `${spaHeaderPrefix}Reload`; const defaultStateAction = 'push'; /** @type {import('../../types/modules/spa').HistoryState|undefined} */ @@ -146,15 +147,26 @@ export default async ({ params, resolve, root }, config) => { navigationRequestIsRunning = false; abortNavigationRequestController = undefined; - const requestIsWithoutErroor = navigationResponse.error === null; - - if (requestIsWithoutErroor) { - if (navigationResponse.response.redirected) { - urlString = navigationResponse.response.url; - } + const requestIsWithoutError = navigationResponse.error === null; + if (requestIsWithoutError) { try { - responseData = navigationResponse.response === null ? '' : await navigationResponse.response.text(); + const { response } = navigationResponse; + const responseIsNull = response === null; + responseData = responseIsNull ? '' : await response.text(); + + if (!responseIsNull && response.redirected) { + const responseUrl = new URL(response.url); + if (window.location.protocol !== responseUrl.protocol + || window.location.hostname !== responseUrl.hostname + || spaReloadHeader in response.headers + ) { + window.location = response.url; + } else { + urlString = responseUrl.toString(); + } + } + } catch (error) { dispatchEventData.error = error; console.error(error); @@ -251,7 +263,6 @@ export default async ({ params, resolve, root }, config) => { if (!navigationScrollStopped) { if (urlHash !== null && urlHash.trim().length > 2) { - console.log() scrollElementIntoView(url.hash); } else { queueMicrotask(() => { diff --git a/packages/signalizejs/types/modules/spa.d.ts b/packages/signalizejs/types/modules/spa.d.ts index 35decdd..067ae05 100644 --- a/packages/signalizejs/types/modules/spa.d.ts +++ b/packages/signalizejs/types/modules/spa.d.ts @@ -35,6 +35,8 @@ export interface SpaConfig { cacheHeader?: string; /** The app version header name. Used to dispatch event, that SPA version has changed. */ appVersionHeader?: string; + /** The reload header name. Used to trigger full page reload. */ + reloadHeader?: string; } export interface HistoryState {