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

Stop showing stale cache snapshot on same-page link click #895

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/core/drive/visit.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { Adapter } from "../native/adapter"
import { FetchMethod, FetchRequest, FetchRequestDelegate } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { History } from "./history"
import { getAnchor } from "../url"
import { getAnchor, toCacheKey } from "../url"
import { Snapshot } from "../snapshot"
import { PageSnapshot } from "./page_snapshot"
import { Action } from "../types"
@@ -293,11 +293,12 @@ export class Visit implements FetchRequestDelegate {

loadCachedSnapshot() {
const snapshot = this.getCachedSnapshot()
const isSameCacheKey = toCacheKey(this.location) === toCacheKey(this.view.lastRenderedLocation)
if (snapshot) {
const isPreview = this.shouldIssueRequest()
this.render(async () => {
this.cacheSnapshot()
if (this.isSamePage) {
if (isSameCacheKey) {
this.adapter.visitRendered(this)
} else {
if (this.view.renderPromise) await this.view.renderPromise
12 changes: 12 additions & 0 deletions src/tests/fixtures/timestamp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html id="html">
<head>
<meta charset="utf-8">
<title>Turbo</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
</head>
<body>
<p>Hello <span id="timestamp">$TIMESTAMP</span></p>
<p><a id="refresh" href="/__turbo/timestamp">Refresh</a></p>
</body>
</html>
25 changes: 25 additions & 0 deletions src/tests/functional/visit_tests.ts
Original file line number Diff line number Diff line change
@@ -220,6 +220,31 @@ test("test Visit with network error", async ({ page }) => {
await nextEventNamed(page, "turbo:fetch-request-error")
})

test("test clicking link to current page does not preview a stale cache snapshot", async ({ page }) => {
const timestamps: number[] = []
const fetchTimestamp = async () => timestamps.push(parseFloat(await page.locator("#timestamp").innerText()))
const assertTimestampNotDecreasing = () => {
const n = timestamps.length
if (n < 2) return
assert(timestamps[n - 1] >= timestamps[n - 2], "timestamps are non-decreasing")
}

await visitLocation(page, "/__turbo/timestamp")
await nextEventNamed(page, "turbo:load")
await fetchTimestamp()

for (let i = 0; i < 5; i++) {
await page.click("a#refresh")
await nextEventNamed(page, "turbo:render")
await fetchTimestamp()
assertTimestampNotDecreasing()

await nextEventNamed(page, "turbo:load")
await fetchTimestamp()
assertTimestampNotDecreasing()
}
})

async function visitLocation(page: Page, location: string) {
return page.evaluate((location) => window.Turbo.visit(location), location)
}
6 changes: 6 additions & 0 deletions src/tests/server.ts
Original file line number Diff line number Diff line change
@@ -67,6 +67,12 @@ router.get("/headers", (request, response) => {
.send(template.replace("$HEADERS", JSON.stringify(request.headers, null, 4)))
})

router.get("/timestamp", (request, response) => {
const template = fs.readFileSync("src/tests/fixtures/timestamp.html").toString()
const timestamp = (new Date().getTime() / 1000).toString()
setTimeout(() => response.type("html").status(200).send(template.replaceAll("$TIMESTAMP", timestamp)), 500)
})

router.get("/delayed_response", (request, response) => {
const fixture = path.join(__dirname, "../../src/tests/fixtures/one.html")
setTimeout(() => response.status(200).sendFile(fixture), 1000)