Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions bifrost/lib/hardNavigate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Hard navigate to a URL using `history.pushState` and `window.location.reload` instead of
* `window.location.href`. This prevents the mobile apps from opening the URL in a browser tab.
*/
export async function hardNavigate(url: string): Promise<never> {
history.pushState(null, "", url);
window.Turbolinks.controller.viewInvalidated();
// stop vike rendering to let navigation happen
await new Promise(() => {});
}
24 changes: 0 additions & 24 deletions bifrost/lib/hardRedirect.ts

This file was deleted.

19 changes: 8 additions & 11 deletions bifrost/renderer/wrapped/onBeforeRender.client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "../../lib/type";
import type { PageContextClient } from "vike/types";
import { redirect } from "vike/abort";
import { hardRedirect } from "../../lib/hardRedirect";
import { getElementAttributes } from "../../lib/elementUtils";
import { hardNavigate } from "../../lib/hardNavigate";

// onBeforeRender runs before changing the browser location, so `throw redirect` works
// we wait for onBeforeRenderClient to call mergeHead, which runs after browser location change
Expand Down Expand Up @@ -56,7 +56,9 @@ export default async function wrappedOnBeforeRender(

if (!resp) {
// hard reload. can happen on cors errors when redirected to external page
await hardRedirect(pageContext.urlParsed.href);
window.location.href = pageContext.urlParsed.href;
// stop vike rendering to let navigation happen
await new Promise(() => {});
return;
}

Expand All @@ -70,26 +72,21 @@ export default async function wrappedOnBeforeRender(
throw redirect(parsedUrl.pathname + parsedUrl.search + parsedUrl.hash);
} else {
// external redirect
await hardRedirect(resp.url);
return;
throw redirect(resp.url);
}
}

if (!resp.ok) {
await hardRedirect(resp.url);
return;
await hardNavigate(resp.url);
}

const html = await resp.text();
const layoutInfo = pageContext.config.getLayout!(
Object.fromEntries(resp.headers.entries())
);
if (!layoutInfo) {
// Fallback to full reload if layout not found
// window.location.href = resp.url;
await hardRedirect(resp.url);
await hardNavigate(resp.url);
return;
}
}

const parsed = document.createElement("html");
parsed.innerHTML = html;
Expand Down