Skip to content

Commit

Permalink
Add refresh visit action
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeiffer committed Feb 15, 2024
1 parent 617e6d0 commit b4cf069
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/core/drive/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class PageView extends View {
}

renderPage(snapshot, isPreview = false, willRender = true, visit) {
const shouldMorphPage = this.isPageRefresh(visit) && this.snapshot.shouldMorphPage
const shouldMorphPage = this.isMorphable(visit) && this.snapshot.shouldMorphPage
const rendererClass = shouldMorphPage ? MorphRenderer : PageRenderer

const renderer = new rendererClass(this.snapshot, snapshot, PageRenderer.renderElement, isPreview, willRender)
Expand Down Expand Up @@ -55,12 +55,20 @@ export class PageView extends View {
return this.snapshotCache.get(location)
}

isPageRefresh(visit) {
isReplacingSamePage(visit) {
return !visit || (this.lastRenderedLocation.pathname === visit.location.pathname && visit.action === "replace")
}

isPageRefresh(visit) {
return visit && visit.action === "refresh"
}

isMorphable(visit) {
return this.isPageRefresh(visit) || this.isReplacingSamePage(visit)
}

shouldPreserveScrollPosition(visit) {
return this.isPageRefresh(visit) && this.snapshot.shouldPreserveScrollPosition
return this.isMorphable(visit) && this.snapshot.shouldPreserveScrollPosition
}

get snapshot() {
Expand Down
4 changes: 3 additions & 1 deletion src/core/drive/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export const SystemStatusCode = {
export const Direction = {
advance: "forward",
restore: "back",
replace: "none"
replace: "none",
refresh: "none"
}

export class Visit {
Expand Down Expand Up @@ -388,6 +389,7 @@ export class Visit {
getHistoryMethodForAction(action) {
switch (action) {
case "replace":
case "refresh":
return history.replaceState
case "advance":
case "restore":
Expand Down
4 changes: 3 additions & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ export class Session {

refresh(url, requestId) {
const isRecentRequest = requestId && this.recentRequests.has(requestId)
const refreshUrl = url || document.baseURI

if (!isRecentRequest) {
this.cache.exemptPageFromPreview()
this.visit(url, { action: "replace" })
this.visit(refreshUrl, { action: "refresh" })
}
}

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 @@ -203,3 +203,10 @@ async function assertPageScroll(page, top, left) {
expect(scrollTop).toEqual(top)
expect(scrollLeft).toEqual(left)
}

test("Turbo.session.refresh() will refresh current page", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")
await page.evaluate(() => window.Turbo.session.refresh())

await nextEventNamed(page, "turbo:render", { renderMethod: "morph" })
})
7 changes: 7 additions & 0 deletions src/tests/functional/visit_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ test("Visit direction attribute on a replace visit", async ({ page }) => {
await assertVisitDirectionAttribute(page, "none")
})

test("Visit direction when refreshing", async ({ page }) => {
page.evaluate(() => window.Turbo.session.refresh())

await assertVisitDirectionAttribute(page, "none")
})


test("Turbo history state after a reload", async ({ page }) => {
await page.click("#same-origin-link")
await nextEventNamed(page, "turbo:load")
Expand Down

0 comments on commit b4cf069

Please sign in to comment.