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

Introduce refresh visit action and method to refresh the current page #1186

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/core/drive/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export class PageView extends View {
}

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

return this.lastRenderedLocation.pathname === visit.location.pathname && (visit.action === "replace" || visit.action === "refresh")
}

shouldPreserveScrollPosition(visit) {
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 @@ -389,6 +390,7 @@ export class Visit {
getHistoryMethodForAction(action) {
switch (action) {
case "replace":
case "refresh":
return history.replaceState
case "advance":
case "restore":
Expand Down
2 changes: 1 addition & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function registerAdapter(adapter) {
* @param location Location to visit (a URL or path)
* @param options Options to apply
* @param options.action Type of history navigation to apply ("restore",
* "replace" or "advance")
* "replace", "refresh" or "advance")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the refresh action should be reserved for internal use, just as the restore action is.

* @param options.historyChanged Specifies whether the browser history has
* already been changed for this visit or not
* @param options.referrer Specifies the referrer of this visit such that
Expand Down
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: "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 @@ -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
Loading