Skip to content

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

Closed

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

Activity

  1. self-assigned this
    on Dec 22, 2023
  2. added
    area: common
    on Dec 22, 2023
  3. added this to the needsTriage milestone on Dec 22, 2023
  4. added a commit that references this issue on Jan 3, 2024
    bd9f89d
  5. added a commit that references this issue on Jan 23, 2024
    2e18235
  6. added a commit that references this issue on Jan 26, 2024
    fbdc3c2
  7. added a commit that references this issue on Jan 26, 2024
    745bfa2
  8. angular-automatic-lock-bot commented on Feb 3, 2024

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: commonstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions