Skip to content

Commit

Permalink
Merge pull request #2191 from inertiajs/fix-url-not-updating-after-sc…
Browse files Browse the repository at this point in the history
…roll

Fix for scroll + back bug
  • Loading branch information
joetannenbaum authored Jan 15, 2025
2 parents 79485d9 + 6f9199e commit 0021d16
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
29 changes: 13 additions & 16 deletions packages/core/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,21 @@ class EventHandler {
return
}

if (history.isValidState(state)) {
history
.decrypt(state.page)
.then((data) => {
currentPage.setQuietly(data, { preserveState: false }).then(() => {
Scroll.restore(history.getScrollRegions())
Scroll.restoreDocument()
fireNavigateEvent(currentPage.get())
})
})
.catch(() => {
this.onMissingHistoryItem()
})

return
if (!history.isValidState(state)) {
return this.onMissingHistoryItem()
}

this.onMissingHistoryItem()
history
.decrypt(state.page)
.then((data) => {
currentPage.setQuietly(data, { preserveState: false }).then(() => {
Scroll.restore(history.getScrollRegions())
fireNavigateEvent(currentPage.get())
})
})
.catch(() => {
this.onMissingHistoryItem()
})
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ class History {
SessionStorage.remove(historySessionStorageKeys.iv)
}

public setCurrent(page: Page): void {
this.current = page
}

public isValidState(state: any): boolean {
return !!state.page
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class CurrentPage {
return this.resolve(page.component).then((component) => {
this.page = page
this.cleared = false
history.setCurrent(page)
return this.swap({ component, page, preserveState })
})
}
Expand Down
2 changes: 2 additions & 0 deletions packages/vue3/test-app/Pages/History/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ defineProps<{ pageNumber: number; multiByte: string }>()

<div>This is page {{ pageNumber }}.</div>
<div>Multi byte character: {{ multiByte }}</div>

<div style="height: 5000px"></div>
</template>
13 changes: 12 additions & 1 deletion tests/history.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test('history can be cleared via props', async ({ page }) => {
await expect(requests.requests).toHaveLength(1)
})

test('multi byte strings can be encrypyed', async ({ page }) => {
test('multi byte strings can be encrypted', async ({ page }) => {
await clickAndWaitForResponse(page, 'Page 5', '/history/5')
const historyState5 = await page.evaluate(() => window.history.state)
// When history is encrypted, the page is an ArrayBuffer,
Expand All @@ -127,3 +127,14 @@ test('multi byte strings can be encrypyed', async ({ page }) => {
await expect(requests.requests).toHaveLength(0)
await expect(page.getByText('Multi byte character: 😃')).toBeVisible()
})

test('url will update after scrolling and pressing back', async ({ page }) => {
// Weird bug that surfaced after setting scroll restoration to manual
await page.waitForURL('/history/1')
await clickAndWaitForResponse(page, 'Page 5', '/history/5')
await page.evaluate(() => (window as any).scrollTo(0, 1000))
await page.goBack()
await page.waitForURL('/history/1')
await page.waitForTimeout(200)
await page.waitForURL('/history/1')
})

0 comments on commit 0021d16

Please sign in to comment.