Skip to content

Commit 335572d

Browse files
revamp
1 parent c63bdad commit 335572d

6 files changed

Lines changed: 220 additions & 317 deletions

File tree

‎.changeset/tall-buses-bow.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
'@tanstack/vue-router': patch
66
---
77

8-
fix: scroll restorating without throttling
8+
fix: scroll restoration without throttling

‎e2e/react-router/basepath-file-based/tests/scroll-restoration-session-storage-error.test.ts‎

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
/* eslint-disable */
22
import { expect, test } from '@playwright/test'
3-
import type { Page } from '@playwright/test'
4-
5-
const trackConsole = (page: Page) => {
6-
const consoleWarnings: Array<string> = []
7-
8-
page.on('console', (msg) => {
9-
if (msg.type() === 'warning') {
10-
consoleWarnings.push(msg.text())
11-
}
12-
})
13-
14-
return consoleWarnings
15-
}
163

174
test.describe('Scroll Restoration with Session Storage Error', () => {
185
test('should not crash when sessionStorage.setItem throws an error', async ({
196
page,
207
}) => {
21-
const consoleWarnings = trackConsole(page)
22-
238
await page.goto('/app/scroll-error')
249
await page.waitForLoadState('networkidle')
2510

@@ -32,32 +17,19 @@ test.describe('Scroll Restoration with Session Storage Error', () => {
3217
await page.evaluate(() => window.scrollTo(0, 200))
3318
await page.waitForTimeout(150)
3419

35-
await page.click('a[href="/app/about"]')
36-
await page.waitForLoadState('networkidle')
37-
38-
await page.goBack()
20+
await page.reload()
3921
await page.waitForLoadState('networkidle')
4022

41-
expect(
42-
consoleWarnings.some((warning) =>
43-
warning.includes(
44-
'[ts-router] Could not persist scroll restoration state to sessionStorage.',
45-
),
46-
),
47-
).toBeTruthy()
48-
4923
const heading = page.locator('h1:has-text("Scroll Error Test")')
5024
await expect(heading).toBeVisible()
5125

5226
const scrollPosition = await page.evaluate(() => window.scrollY)
5327
expect(scrollPosition).not.toBe(200)
5428
})
5529

56-
test('should surface warning when sessionStorage quota is exceeded', async ({
30+
test('should not crash when sessionStorage quota is exceeded', async ({
5731
page,
5832
}) => {
59-
const consoleWarnings = trackConsole(page)
60-
6133
await page.goto('/app/scroll-error')
6234
await page.waitForLoadState('networkidle')
6335

@@ -78,20 +50,9 @@ test.describe('Scroll Restoration with Session Storage Error', () => {
7850
await page.evaluate(() => window.scrollTo(0, 200))
7951
await page.waitForTimeout(150)
8052

81-
await page.click('a[href="/app/about"]')
53+
await page.reload()
8254
await page.waitForLoadState('networkidle')
8355

84-
await page.goBack()
85-
await page.waitForLoadState('networkidle')
86-
87-
expect(
88-
consoleWarnings.some((warning) =>
89-
warning.includes(
90-
'[ts-router] Could not persist scroll restoration state to sessionStorage.',
91-
),
92-
),
93-
).toBeTruthy()
94-
9556
const heading = page.locator('h1:has-text("Scroll Error Test")')
9657
await expect(heading).toBeVisible()
9758

‎packages/react-router/tests/Scripts.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('ssr scripts', () => {
162162
expect(await screen.findByTestId('index')).toBeInTheDocument()
163163

164164
expect(container.innerHTML).toEqual(
165-
`<div><div data-testid="root">root</div><div data-testid="index">index</div><script src="script.js"></script><script src="script3.js"></script></div>`,
165+
`<div><div data-testid="root">root</div><div data-testid="index">index</div><script></script><script src="script.js"></script><script src="script3.js"></script></div>`,
166166
)
167167
})
168168
})

‎packages/router-core/src/index.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,7 @@ export { isNotFound, notFound } from './not-found'
397397

398398
export {
399399
defaultGetScrollRestorationKey,
400-
getScrollRestorationScript,
401400
getScrollRestorationScriptForRouter,
402-
restoreScroll,
403401
storageKey,
404402
getCssSelector,
405403
scrollRestorationCache,
@@ -408,8 +406,6 @@ export {
408406
} from './scroll-restoration'
409407

410408
export type {
411-
RestoreScrollOptions,
412-
InlineScrollRestorationScriptOptions,
413409
ScrollRestorationOptions,
414410
ScrollRestorationEntry,
415411
} from './scroll-restoration'

‎packages/router-core/src/scroll-restoration-inline.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,41 @@ export default function (options: {
1919
if (
2020
options.shouldScrollRestoration &&
2121
elementEntries &&
22+
typeof elementEntries === 'object' &&
2223
Object.keys(elementEntries).length > 0
2324
) {
2425
for (const elementSelector in elementEntries) {
2526
const entry = elementEntries[elementSelector]
2627

28+
if (!entry || typeof entry !== 'object') {
29+
continue
30+
}
31+
32+
const scrollX = entry.scrollX
33+
const scrollY = entry.scrollY
34+
35+
if (!Number.isFinite(scrollX) || !Number.isFinite(scrollY)) {
36+
continue
37+
}
38+
2739
if (elementSelector === 'window') {
2840
window.scrollTo({
29-
top: entry.scrollY,
30-
left: entry.scrollX,
41+
top: scrollY,
42+
left: scrollX,
3143
behavior: options.behavior,
3244
})
3345
} else if (elementSelector) {
34-
const element = document.querySelector(elementSelector)
46+
let element
47+
48+
try {
49+
element = document.querySelector(elementSelector)
50+
} catch {
51+
continue
52+
}
53+
3554
if (element) {
36-
element.scrollLeft = entry.scrollX
37-
element.scrollTop = entry.scrollY
55+
element.scrollLeft = scrollX
56+
element.scrollTop = scrollY
3857
}
3958
}
4059
}

0 commit comments

Comments
 (0)