Skip to content

Commit

Permalink
Spa redirect fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Machy8 committed Nov 30, 2024
1 parent c841e8b commit b2363ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/signalizejs/src/modules/spa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/signalizejs/types/modules/spa.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b2363ff

Please sign in to comment.