Skip to content

Angular 16 migration: TypeError: Cannot read properties of undefined (reading 'getElementById') #50138

@MarcoGlauser

Description

@MarcoGlauser

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

Don't known / other

Is this a regression?

No

Description

Migrating from angular 15.2.9 to 16.0.0 we encounter the stack trace below when running the universal server (dev & prod build).
spa dev mode works as expected without any noticeable issues.
Following the stack trace it's this piece of code that is encountering an issue:

function retrieveTransferredState(doc,appId){const script=doc.getElementById(appId+"-state");

The error occurs whether we provide client hydration or not.
We use TransferState throughout the application to send data from ssr to the client.

EnvironmentService, that seems to be the root of this error, injects TransferState in the constructor.

I don't quite know how to drill down on this issue, since this seems to be happening very early in app lifecycle. Any Idea on how to create a reproduction would be highly appreciated.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

TypeError: Cannot read properties of undefined (reading 'getElementById')
    at retrieveTransferredState (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2325653)
    at Object.initTransferState [as factory] (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2324438)
    at R3Injector.hydrate (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2320111)
    at R3Injector.get (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2318370)
    at injectInjectorOnly (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2186545)
    at Object.ɵɵinject (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2186715)
    at Object.EnvironmentService_Factory [as factory] (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:27288)
    at R3Injector.hydrate (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2320111)
    at R3Injector.get (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2318370)
    at injectInjectorOnly (/home/marco/FollowAlice/referral/frontend/dist/referral/server/main.js:1:2186545)

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

Angular CLI: 16.0.0
Node: 18.16.0
Package Manager: npm 9.5.1
OS: linux x64

Angular: 16.0.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, language-service, material, material-luxon-adapter
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker, youtube-player

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1600.0
@angular-devkit/build-angular   16.0.0
@angular-devkit/core            16.0.0
@angular-devkit/schematics      16.0.0
@nguniversal/builders           16.0.0
@nguniversal/express-engine     16.0.0
@schematics/angular             16.0.0
typescript                      4.9.5

Anything else?

No response

Metadata

Metadata

Assignees

Labels

P2The issue is important to a large percentage of users, with a workaroundarea: serverIssues related to server-side renderingcore: hydrationstate: has PR

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions