Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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
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
Loading