8000 fix(router): handle scrollRestoration error in restricted environment… · angular/angular@65c59dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 65c59dd

Browse files
arturovtthePunderWoman
authored andcommitted
fix(router): handle scrollRestoration error in restricted environments (#62186)
In this commit, setting `window.history.scrollRestoration` is wrapped in a try-catch block to prevent `SecurityError` exceptions in restricted contexts such as: - sandboxed iframes - partially navigated or inactive windows - test runners, extensions, or content previews If an error occurs, a runtime warning with error code [2400] is logged to the console. This avoids breaking app initialization and improves cross-browser safety. Unfortunately, it's not possible to perform any end-to-end testing of this fix. PR Close #62186
1 parent e7d259b commit 65c59dd

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

goldens/public-api/common/errors.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export const enum RuntimeErrorCode {
5353
// (undocumented)
5454
REQUIRED_INPUT_MISSING = 2954,
5555
// (undocumented)
56+
SCROLL_RESTORATION_UNSUPPORTED = 2400,
57+
// (undocumented)
5658
SUSPICIOUS_DATE_FORMAT = 2300,
5759
// (undocumented)
5860
TOO_MANY_PRELOADED_IMAGES = 2961,

packages/common/src/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export const enum RuntimeErrorCode {
3838
NO_PLURAL_MESSAGE_FOUND = 2308,
3939
VALUE_NOT_A_NUMBER = 2309,
4040

41+
// Miscellaneous errors
42+
SCROLL_RESTORATION_UNSUPPORTED = 2400,
43+
4144
// Keep 2800 - 2900 for Http Errors.
4245

4346
// Image directive errors

packages/common/src/viewport_scroller.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {inject, ɵɵdefineInjectable, DOCUMENT} from '@angular/core';
9+
import {
10+
inject,
11+
ɵɵdefineInjectable,
12+
DOCUMENT,
13+
ɵformatRuntimeError as formatRuntimeError,
14+
} from '@angular/core';
15+
import {RuntimeErrorCode} from './errors';
1016

1117
/**
1218
* Defines a scroll position manager. Implemented by `BrowserViewportScroller`.
@@ -131,7 +137,22 @@ export class BrowserViewportScroller implements ViewportScroller {
131137
* Disables automatic scroll restoration provided by the browser.
132138
*/
133139
setHistoryScrollRestoration(scrollRestoration: 'auto' | 'manual'): void {
134-
this.window.history.scrollRestoration = scrollRestoration;
140+
try {
141+
this.window.history.scrollRestoration = scrollRestoration;
142+
} catch {
143+
console.warn(
144+
formatRuntimeError(
145+
RuntimeErrorCode.SCROLL_RESTORATION_UNSUPPORTED,
146+
ngDevMode &&
147+
'Failed to set `window.history.scrollRestoration`. ' +
148+
'This may occur when:\n' +
149+
'• The script is running inside a sandboxed iframe\n' +
150+
'• The window is partially navigated or inactive\n' +
151+
'• The script is executed in an untrusted or special context (e.g., test runners, browser extensions, or content previews)\n' +
152+
'Scroll position may not be preserved across navigation.',
153+
),
154+
);
155+
}
135156
}
136157

137158
/**

0 commit comments

Comments
 (0)
0