Skip to content

Commit

Permalink
Cache snapshot before doing page refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeiffer committed Feb 15, 2024
1 parent fccb3a4 commit 818c278
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/core/drive/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ export class Visit {
return this.isSamePage
}

start() {
async start() {
if (this.state == VisitState.initialized) {
this.recordTimingMetric(TimingMetric.visitStart)
this.state = VisitState.started

if (this.isPageRefresh) {
await this.cacheSnapshot()
}

this.adapter.visitStarted(this)
this.delegate.visitStarted(this)
}
Expand Down Expand Up @@ -410,9 +415,14 @@ export class Visit {
}
}

cacheSnapshot() {
async cacheSnapshot() {
if (!this.snapshotCached) {
this.view.cacheSnapshot(this.snapshot).then((snapshot) => snapshot && this.visitCachedSnapshot(snapshot))
const snapshot = await this.view.cacheSnapshot(this.snapshot)

if (snapshot) {
this.visitCachedSnapshot(snapshot)
}

this.snapshotCached = true
}
}
Expand Down
1 change: 0 additions & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export class Session {
refresh(url, requestId) {
const isRecentRequest = requestId && this.recentRequests.has(requestId)
if (!isRecentRequest) {
this.cache.exemptPageFromPreview()
this.visit(url, { action: "replace" })
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/tests/functional/page_refresh_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ test("doesn't render previews when morphing", async ({ page }) => {
assert.equal(await title.textContent(), "Page to be refreshed")
})

test("it snapshots page before starting a page refresh", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")

await page.click("#form-submit")
await nextEventNamed(page, "turbo:before-cache")
})

async function assertPageScroll(page, top, left) {
const [scrollTop, scrollLeft] = await page.evaluate(() => {
return [
Expand Down

0 comments on commit 818c278

Please sign in to comment.