Skip to content

Commit

Permalink
Add Turbo.session.refresh() for a short-hand refresh of current page
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeiffer committed Feb 15, 2024
1 parent 07308e9 commit b620ba4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ export class Session {
}

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

if (!isRecentRequest) {
this.cache.exemptPageFromPreview()
this.visit(url, { action: "refresh" })
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 @@ -319,3 +319,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 b620ba4

Please sign in to comment.