Skip to content

Commit

Permalink
Fix temporary elements disappearing on subsequent GET requests that r…
Browse files Browse the repository at this point in the history
…edirect to a page with data-turbo-temporary elements. (#1302)

* Fix temporary elements disappearing on subsequent GET requests that redirect to a page with data-turbo-temporary elements.

* Apply suggestions from code review

Co-authored-by: Bruno Prieto <[email protected]>

* Fix lint: nextRepaint no longer needed

---------

Co-authored-by: Bruno Prieto <[email protected]>
  • Loading branch information
mikepayready and brunoprietog authored Aug 26, 2024
1 parent f48b0de commit 2f7abac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/core/drive/visit.js
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -419,7 +419,10 @@ export class Visit {

async render(callback) {
this.cancelRender()
this.frame = await nextRepaint()
await new Promise((resolve) => {
this.frame =
document.visibilityState === "hidden" ? setTimeout(() => resolve(), 0) : requestAnimationFrame(() => resolve())
})
await callback()
delete this.frame
}
Expand Down
2 changes: 2 additions & 0 deletions src/tests/fixtures/cache_observer.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ <h1>Cache Observer</h1>
<div id="temporary" data-turbo-temporary>data-turbo-temporary</div>
<div id="temporary-with-deprecated-selector" data-turbo-cache="false">data-turbo-cache=false</div>
<p><a id="link" href="/src/tests/fixtures/rendering.html">rendering</a></p>
<p><a id="redirect-here-link" href="/__turbo/redirect?path=/src/tests/fixtures/cache_observer.html">Redirection link back to here</a></p>
</section>
</body>
</html>
1 change: 1 addition & 0 deletions src/tests/fixtures/visit.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h1>Visit</h1>
<p><a id="below-the-fold-link" href="/src/tests/fixtures/one.html">one.html</a></p>
<hr class="push-below-fold">
<p><a id="stream-link" href="/__turbo/stream-response?content=link&type=stream&key=value" data-turbo-stream>Stream link with ?key=value</a></p>
<p><a id="cache-observer-link" href="/src/tests/fixtures/cache_observer.html">Link to cache observer</a></p>
</section>

<div id="messages"></div>
Expand Down
16 changes: 16 additions & 0 deletions src/tests/functional/visit_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 2f7abac

Please sign in to comment.