Skip to content

ERROR ReferenceError: window is not defined when using withInMemoryScrolling with SSR #53682

@kuncevic

Description

@kuncevic

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

Yes

Description

I migrated an Angular v16 app to v17. When attempting to prerender it, an error occurs: ERROR ReferenceError: 'window' is not defined.

To replicate run npm run dev:ssr and then hit http://localhost:4200 or run npm run prerender

The prerender would run with no errors if you comment this lines https://github.com/kuncevic/v17-withIn-memory-scrolling-issue/blob/master/projects/consulting/src/app/app.config.ts#L16-L19

Please provide a link to a minimal reproduction of the bug

https://github.com/kuncevic/v17-withIn-memory-scrolling-issue

Please provide the exception or error you saw

ReferenceError: window is not defined
    at factory (webpack:///node_modules/@angular/common/fesm2022/common.mjs:5184:72)
    at callback (webpack:///node_modules/@angular/core/fesm2022/core.mjs:6168:43)
    at runInInjectorProfilerContext (webpack:///node_modules/@angular/core/fesm2022/core.mjs:867:9)
    at hydrate (webpack:///node_modules/@angular/core/fesm2022/core.mjs:6167:17)
    at get (webpack:///node_modules/@angular/core/fesm2022/core.mjs:6037:33)
    at injectInjectorOnly (webpack:///node_modules/@angular/core/fesm2022/core.mjs:911:40)
    at ɵɵinject (webpack:///node_modules/@angular/core/fesm2022/core.mjs:917:60)
    at inject (webpack:///node_modules/@angular/core/fesm2022/core.mjs:1001:12)
    at useFactory (webpack:///node_modules/@angular/router/fesm2022/router.mjs:6476:48)
    at factory (webpack:///node_modules/@angular/core/fesm2022/core.mjs:6267:38)


the error indicate the below line

class ViewportScroller {
    // De-sugared tree-shakable injection
    // See #23917
    /** @nocollapse */
    static { this.ɵprov = ɵɵdefineInjectable({
        token: ViewportScroller,
        providedIn: 'root',
        factory: () => new BrowserViewportScroller(ɵɵinject(DOCUMENT), window) <--error here
    }); }
}


### Please provide the environment you discovered this bug in (run `ng version`)

```true
Angular CLI: 17.0.7
Node: 18.18.2
Package Manager: yarn 1.22.19
OS: darwin arm64

Angular: 17.0.7
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1700.7
@angular-devkit/build-angular   17.0.7
@angular-devkit/core            17.0.7
@angular-devkit/schematics      17.0.7
@angular/cdk                    17.0.4
@angular/material               17.0.4
@schematics/angular             17.0.7
ng-packagr                      17.0.3
rxjs                            7.8.1
typescript                      5.2.2
zone.js                         0.14.2

Anything else?

No response

Metadata

Metadata

Assignees

Labels

area: commonIssues related to APIs in the @angular/common packagestate: has PR

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions