From 6044b0add1c86b3bb5fd389db9fba0e34725b58d Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Thu, 15 Feb 2024 08:25:01 +0100 Subject: [PATCH] Add `Turbo.session.refresh()` for a short-hand refresh of current page --- src/core/session.js | 4 +++- src/tests/functional/page_refresh_tests.js | 7 +++++++ src/tests/functional/visit_tests.js | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/core/session.js b/src/core/session.js index eb0bd3117..372a7c01d 100644 --- a/src/core/session.js +++ b/src/core/session.js @@ -106,10 +106,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" }) } } diff --git a/src/tests/functional/page_refresh_tests.js b/src/tests/functional/page_refresh_tests.js index 45bb823d6..22a1d9322 100644 --- a/src/tests/functional/page_refresh_tests.js +++ b/src/tests/functional/page_refresh_tests.js @@ -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" }) +}) \ No newline at end of file diff --git a/src/tests/functional/visit_tests.js b/src/tests/functional/visit_tests.js index 782871747..065ebf22c 100644 --- a/src/tests/functional/visit_tests.js +++ b/src/tests/functional/visit_tests.js @@ -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")