fix(common): server-side rendering error when using in-memory scrolling (#53683)

Fixes that the `BrowserViewportScroller` was throwing an error during server-side rendering because it was accessing `window` directly. Also removes some assertions that aren't necessary anymore.

Fixes #53682.

PR Close #53683
This commit is contained in:
Kristiyan Kostadinov
2023-12-27 17:53:30 +02:00
committed by Andrew Scott
parent fad9e952ae
commit bd9f89d1c8
+8 -25
View File
@@ -6,9 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ɵɵdefineInjectable, ɵɵinject} from '@angular/core';
import {inject, PLATFORM_ID, ɵɵdefineInjectable} from '@angular/core';
import {DOCUMENT} from './dom_tokens';
import {isPlatformBrowser} from './platform_id';
@@ -24,7 +25,9 @@ export abstract class ViewportScroller {
static ɵprov = /** @pureOrBreakMyCode */ ɵɵdefineInjectable({
token: ViewportScroller,
providedIn: 'root',
factory: () => new BrowserViewportScroller(ɵɵinject(DOCUMENT), window)
factory: () => isPlatformBrowser(inject(PLATFORM_ID)) ?
new BrowserViewportScroller(inject(DOCUMENT), window) :
new NullViewportScroller()
});
/**
@@ -88,11 +91,7 @@ export class BrowserViewportScroller implements ViewportScroller {
* @returns The position in screen coordinates.
*/
getScrollPosition(): [number, number] {
if (this.supportsScrolling()) {
return [this.window.pageXOffset, this.window.pageYOffset];
} else {
return [0, 0];
}
return [this.window.scrollX, this.window.scrollY];
}
/**
@@ -100,9 +99,7 @@ export class BrowserViewportScroller implements ViewportScroller {
* @param position The new position in screen coordinates.
*/
scrollToPosition(position: [number, number]): void {
if (this.supportsScrolling()) {
this.window.scrollTo(position[0], position[1]);
}
this.window.scrollTo(position[0], position[1]);
}
/**
@@ -117,10 +114,6 @@ export class BrowserViewportScroller implements ViewportScroller {
* @see https://html.spec.whatwg.org/#scroll-to-fragid
*/
scrollToAnchor(target: string): void {
if (!this.supportsScrolling()) {
return;
}
const elSelected = findAnchorFromDocument(this.document, target);
if (elSelected) {
@@ -139,9 +132,7 @@ export class BrowserViewportScroller implements ViewportScroller {
* Disables automatic scroll restoration provided by the browser.
*/
setHistoryScrollRestoration(scrollRestoration: 'auto'|'manual'): void {
if (this.supportsScrolling()) {
this.window.history.scrollRestoration = scrollRestoration;
}
this.window.history.scrollRestoration = scrollRestoration;
}
/**
@@ -157,14 +148,6 @@ export class BrowserViewportScroller implements ViewportScroller {
const offset = this.offset();
this.window.scrollTo(left - offset[0], top - offset[1]);
}
private supportsScrolling(): boolean {
try {
return !!this.window && !!this.window.scrollTo && 'pageXOffset' in this.window;
} catch {
return false;
}
}
}
function findAnchorFromDocument(document: Document, target: string): HTMLElement|null {