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
24 changes: 24 additions & 0 deletions bifrost/lib/hardRedirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Hard redirect (full page reload) that converts same-origin URLs to relative paths.
* Turbolinks IOS requires relative URLs to open within the mobile app instead of safari.
*/
export async function hardRedirect(url: string) {
if (url.startsWith("/")) {
window.location.href = url;
await new Promise(() => {});
return;
}

const parsedUrl = new URL(url);

if (window.location.origin === parsedUrl.origin) {
// Relative redirect
window.location.href = parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
} else {
// External redirect
window.location.href = url;
}

// stop vike rendering to let navigation happen
await new Promise(() => {});
}
18 changes: 11 additions & 7 deletions bifrost/renderer/wrapped/onBeforeRender.client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "../../lib/type";
import type { PageContextClient } from "vike/types";
import { redirect } from "vike/abort";
import { hardRedirect } from "../../lib/hardRedirect";
import { getElementAttributes } from "../../lib/elementUtils";

// onBeforeRender runs before changing the browser location, so `throw redirect` works
Expand Down Expand Up @@ -55,9 +56,7 @@ export default async function wrappedOnBeforeRender(

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

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

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

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;
throw redirect(resp.url);
}
await hardRedirect(resp.url);
return;
}

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