Skip to content

Commit

Permalink
Page refreshes: Use pathname instead of href to detect if locations a…
Browse files Browse the repository at this point in the history
…re equal

Before this commit, URLs had to be exactly the same for the possibility of a refresh with morphing.
Now, it only checks that the paths are equal, regardless of whether they have different search params.
  • Loading branch information
brunoprietog committed Nov 27, 2023
1 parent 1aa7a87 commit e7a2c7a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/drive/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class PageView extends View {
}

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

get snapshot() {
Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/page_refresh.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ <h2>Frame to be preserved</h2>
<h3>Element with Stimulus controller</h3>
</div>

<p><a id="replace-link" data-turbo-action="replace" href="/src/tests/fixtures/page_refresh.html?param=something">Link with params to refresh the page</a></p>
<p><a id="link" href="/src/tests/fixtures/one.html">Link to another page</a></p>

<form id="form" action="/__turbo/refresh" method="post" class="redirect">
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 @@ -17,6 +17,13 @@ test("renders a page refresh with morphing", async ({ page }) => {
await nextEventNamed(page, "turbo:render", { renderMethod: "morph" })
})

test("renders a page refresh with morphing when the paths are the same but search params are diferent", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")

await page.click("#replace-link")
await nextEventNamed(page, "turbo:render", { renderMethod: "morph" })
})

test("doesn't morph when the turbo-refresh-method meta tag is not 'morph'", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh_replace.html")

Expand Down

0 comments on commit e7a2c7a

Please sign in to comment.