From 0947407b23e23ac334c0ecf4f05dd85685b5383b Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Wed, 21 Feb 2024 16:16:37 -0500 Subject: [PATCH] Fix Turbo Frame prefetching bug Closes [#1191][] Ensure Prefetching does not share cache entries for requests with differing `Turbo-Frame:` HTTP headers. [#1191]: https://github.com/hotwired/turbo/issues/1191 --- src/tests/fixtures/hover_to_prefetch.html | 7 ++-- .../link_prefetch_observer_tests.js | 33 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/tests/fixtures/hover_to_prefetch.html b/src/tests/fixtures/hover_to_prefetch.html index d47be49db..1f7808d27 100644 --- a/src/tests/fixtures/hover_to_prefetch.html +++ b/src/tests/fixtures/hover_to_prefetch.html @@ -1,9 +1,10 @@ - + Hover to Prefetch + @@ -49,11 +50,11 @@ - Hover to prefetch me + Hover to prefetch /prefetched.html frame - Hover to prefetch me + Hover to prefetch /prefetched.html page diff --git a/src/tests/functional/link_prefetch_observer_tests.js b/src/tests/functional/link_prefetch_observer_tests.js index 735efb7fb..b748b7b6b 100644 --- a/src/tests/functional/link_prefetch_observer_tests.js +++ b/src/tests/functional/link_prefetch_observer_tests.js @@ -1,6 +1,6 @@ -import { test } from "@playwright/test" +import { expect, test } from "@playwright/test" import { assert } from "chai" -import { nextBeat, sleep } from "../helpers/page" +import { nextBeat, nextEventOnTarget, sleep } from "../helpers/page" import fs from "fs" import path from "path" @@ -182,6 +182,35 @@ test("it prefetches links inside a turbo frame", async ({ page }) => { }}) }) +test("prefetching a page-wide request does not affect frame requests", async ({ page }) => { + await goTo({ page, path: "/hover_to_prefetch.html" }) + + const pageLink = page.locator("#anchor_for_prefetch_in_frame_target_top") + const frameLink = page.locator("#anchor_for_prefetch_in_frame") + + await pageLink.hover() + await sleep(25) + await frameLink.click() + + const frameRequest = await nextEventOnTarget(page, "frame_for_prefetch", "turbo:before-fetch-request") + + expect(frameRequest.fetchOptions.headers["Turbo-Frame"]).toEqual("frame_for_prefetch") +}) + +test("prefetching a frame request does not affect page-wide requests", async ({ page }) => { + await goTo({ page, path: "/hover_to_prefetch.html" }) + + const pageLink = page.locator("#anchor_for_prefetch_in_frame_target_top") + const frameLink = page.locator("#anchor_for_prefetch_in_frame") + + await frameLink.hover() + await sleep(25) + await pageLink.click() + + const pageRequest = await nextEventOnTarget(page, "html", "turbo:before-fetch-request") + + expect(pageRequest.fetchOptions.headers["Turbo-Frame"]).toEqual(undefined) +}) test("doesn't include a turbo-frame header when the link is inside a turbo frame with a target=_top", async ({ page}) => { await goTo({ page, path: "/hover_to_prefetch.html" })