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
26 changes: 18 additions & 8 deletions bifrost/renderer/onBeforeRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@ const onBeforeRoute = (pageContext: PageContext) => {

let currentVisit = pageContext._turbolinksVisit;

if (pageContext.isHistoryNavigation) {
if (!pageContext.pageContextsAborted?.length) {
// This can be called multiple times if guards throw redirect, only notify history pop once
Turbolinks.controller.historyPoppedToLocationWithRestorationIdentifier(
pageContext.urlOriginal,
""
);
}
// Initially, currentVisit was just created by historyPoppedToLocationWithRestorationIdentifier
// If abort rendering, we'd rather Vike pass _turbolinksVisit, but it does not, so we recover from Turbolinks global
// There is risk of race condition if a history nav and a regular nav happen at the same time.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution remains imperfect. We need to open a Vike feature request. onBeforeRoute is a bit of a hack in general.

currentVisit = Turbolinks.controller.currentVisit;
}

if (pageContext.pageContextsAborted?.length && currentVisit) {
currentVisit.updateIfRedirect(pageContext.urlOriginal);
}

if (pageContext.isHistoryNavigation) {
const snapshot = Turbolinks.controller.getCachedSnapshotForLocation(
pageContext.urlOriginal
);
Turbolinks.controller.historyPoppedToLocationWithRestorationIdentifier(
pageContext.urlOriginal,
""
);

// currentVisit was just created by historyPoppedToLocationWithRestorationIdentifier
currentVisit = Turbolinks.controller.currentVisit;
if (!!snapshot) {
return {
pageContext: {
Expand All @@ -29,8 +41,6 @@ const onBeforeRoute = (pageContext: PageContext) => {
},
};
}
} else if (pageContext.pageContextsAborted && currentVisit) {
currentVisit.updateIfRedirect(pageContext.urlOriginal);
}
return { pageContext: { _turbolinksVisit: currentVisit } };
}
Expand Down
39 changes: 39 additions & 0 deletions tests/e2e/specs/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
expectNoMoreScripts,
ensureNoBrowserNavigation,
storeConsoleLog,
waitForConsoleLog,
validateDOMOnTurbolinks,
StringMatcher,
sleep,
Expand Down Expand Up @@ -83,6 +84,44 @@ test.describe("pages", () => {
).toHaveCount(1);
});

test("back button to a page proxied by render() fires each turbolinks event once", async ({
page,
}) => {
ensureAllNetworkSucceeds(page);
await page.goto("./proxy-to", { waitUntil: "networkidle" });
await waitForTurbolinksInit(page);
await expect(page).toHaveTitle("b");

await ensureNoBrowserNavigation(page, async () => {
const loaded = waitForConsoleLog(page, (m) => m.text() === T.load);
await page.getByText("vite page").click();
await loaded;
});
await expect(page).toHaveTitle("vite page");

// capture only the events from the back navigation
const logs = storeConsoleLog(page);
await ensureNoBrowserNavigation(page, async () => {
const loaded = waitForConsoleLog(page, (m) => m.text() === T.load);
await page.goBack();
await loaded;
});

await expect(page).toHaveTitle("b");
await expect(
page.locator("nav", { hasText: "Main Nav Layout" })
).toHaveCount(1);

// restoration visits skip click/before-visit, and each event fires exactly once
expect(logs.filter((s) => s.startsWith("turbolinks:"))).toEqual([
T.visit,
T.beforeRender,
T.render,
T.load,
]);
await expectNoMoreScripts(page);
});

test("it serves vite pages", async ({ page }) => {
await page.goto("./vite-page");

Expand Down
1 change: 1 addition & 0 deletions tests/vite/pages/vite-page/+Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function Page() {
<a href="/redirect-page/redirect-to">redirect page</a>
<a href="#anchor">anchor link</a>
<h2 id="anchor">anchor link test</h2>
<a href="/proxy-to">proxy-to page</a>
<div style={{ height: "1000px" }}></div>
</>
);
Expand Down