From c403c251087dca248e90e8a7c8d0ae36530255d9 Mon Sep 17 00:00:00 2001 From: Mike Virata-Stone Date: Fri, 23 Aug 2024 13:01:36 -0700 Subject: [PATCH 1/3] Fix temporary elements disappearing on subsequent GET requests that redirect to a page with data-turbo-temporary elements. --- src/core/drive/visit.js | 4 +++- src/tests/fixtures/cache_observer.html | 2 ++ src/tests/fixtures/visit.html | 1 + src/tests/functional/visit_tests.js | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/core/drive/visit.js b/src/core/drive/visit.js index ec7565979..3824d2ec3 100644 --- a/src/core/drive/visit.js +++ b/src/core/drive/visit.js @@ -419,7 +419,9 @@ export class Visit { async render(callback) { this.cancelRender() - this.frame = await nextRepaint() + await new Promise((resolve) => { + this.frame = requestAnimationFrame(() => resolve()) + }) await callback() delete this.frame } diff --git a/src/tests/fixtures/cache_observer.html b/src/tests/fixtures/cache_observer.html index 5ad72b190..ad1b391f7 100644 --- a/src/tests/fixtures/cache_observer.html +++ b/src/tests/fixtures/cache_observer.html @@ -12,5 +12,7 @@

Cache Observer

data-turbo-temporary
data-turbo-cache=false

rendering

+

Redirection link back to here

+ diff --git a/src/tests/fixtures/visit.html b/src/tests/fixtures/visit.html index 19e40e5bb..7d246ffa3 100644 --- a/src/tests/fixtures/visit.html +++ b/src/tests/fixtures/visit.html @@ -21,6 +21,7 @@

Visit

one.html


Stream link with ?key=value

+

Link to cache observer

diff --git a/src/tests/functional/visit_tests.js b/src/tests/functional/visit_tests.js index dd0b97968..c53a2600c 100644 --- a/src/tests/functional/visit_tests.js +++ b/src/tests/functional/visit_tests.js @@ -175,6 +175,22 @@ test("cache does not override response after redirect", async ({ page }) => { assert.equal(await page.locator("some-cached-element").count(), 0) }) +test("cache does not hide temporary elements on the second visit after redirect", async ({ page }) => { + await page.click("#cache-observer-link") + await nextBeat() + await page.click("#redirect-here-link") + await nextBeat() // 301 redirect response + await nextBeat() // 200 response + + assert.equal(await page.locator("#temporary").count(), 1) + + await page.click("#redirect-here-link") + await nextBeat() // 301 redirect response + await nextBeat() // 200 response + + assert.equal(await page.locator("#temporary").count(), 1) +}) + function cancelNextVisit(page) { return cancelNextEvent(page, "turbo:before-visit") } From 519623fe2543776bca31c09e9c31880b4f20642e Mon Sep 17 00:00:00 2001 From: mikepayready Date: Sun, 25 Aug 2024 19:56:55 -0700 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Bruno Prieto --- src/core/drive/visit.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/drive/visit.js b/src/core/drive/visit.js index 3824d2ec3..d91083ebb 100644 --- a/src/core/drive/visit.js +++ b/src/core/drive/visit.js @@ -420,7 +420,8 @@ export class Visit { async render(callback) { this.cancelRender() await new Promise((resolve) => { - this.frame = requestAnimationFrame(() => resolve()) + this.frame = + document.visibilityState === "hidden" ? setTimeout(() => resolve(), 0) : requestAnimationFrame(() => resolve()) }) await callback() delete this.frame From e10512bf1c32ad43f53fce6f505fdf8bfe3f34ee Mon Sep 17 00:00:00 2001 From: Mike Virata-Stone Date: Sun, 25 Aug 2024 20:47:49 -0700 Subject: [PATCH 3/3] Fix lint: nextRepaint no longer needed --- src/core/drive/visit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/drive/visit.js b/src/core/drive/visit.js index d91083ebb..97aef6300 100644 --- a/src/core/drive/visit.js +++ b/src/core/drive/visit.js @@ -1,7 +1,7 @@ import { FetchMethod, FetchRequest } from "../../http/fetch_request" import { getAnchor } from "../url" import { PageSnapshot } from "./page_snapshot" -import { getHistoryMethodForAction, uuid, nextRepaint } from "../../util" +import { getHistoryMethodForAction, uuid } from "../../util" import { StreamMessage } from "../streams/stream_message" import { ViewTransitioner } from "./view_transitioner"