Angular Universal TypeError: Cannot redefine property: __@ngrx/effects_create__
Hi! I am updating packages to latest, everything works fine with web app, but with server template (angular universal) i have such issue with some effects, it start works like that right after ngrx/store update:
TypeError: Cannot redefine property: __@ngrx/effects_create__
at Function.defineProperty (<anonymous>)
at Module.createEffect (/Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:134208:12)
at new PageEffects (/Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:385915:39)
at Object.PageEffects_Factory [as factory] (/Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:385949:61)
at R3Injector.hydrate (/Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:76476:35)
at R3Injector.get (/Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:76296:33)
at /Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:134660:45
at Array.map (<anonymous>)
at createEffectInstances (/Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:134660:20)
at Object.createEffects [as useFactory] (/Users/yehor/work/rentberry/web/dist/rentberry/server/main.js:134657:12)
Deps:
...
"@angular-devkit/build-angular": "^12.0.5",
"@angular/animations": "^12.0.5",
"@angular/cli": "^12.0.5",
"@angular/common": "^12.0.5",
"@angular/compiler": "^12.0.5",
"@angular/compiler-cli": "^12.0.5",
"@angular/core": "^12.0.5",
"@angular/forms": "^12.0.5",
"@angular/language-service": "^12.0.5",
"@angular/localize": "^12.0.5",
"@angular/platform-browser": "^12.0.5",
"@angular/platform-browser-dynamic": "^12.0.5",
"@angular/platform-server": "^12.0.5",
"@angular/router": "^12.0.5",
"@ngrx/effects": "^12.1.0",
"@ngrx/router-store": "^12.1.0",
"@ngrx/store": "^12.1.0",
"@ngrx/store-devtools": "^12.1.0",
"@nguniversal/express-engine": "^12.0.2",
"typescript": "4.2.4",
"zone.js": "~0.11.4",
"rxjs": "^6.5.3",
...
The effect to reproduce:
listenResize = createEffect(() => this._listenResize());
private _listenResize(): Observable<Action> {
if (isPlatformServer(this._platformId)) {
return EMPTY;
}
return fromEvent(window, 'resize').pipe(
startWith(() => null),
map(() => ({
isTablet: window.innerWidth >= 768 && window.innerWidth < 992,
isMobile: window.innerWidth < 768
})),
distinctUntilChanged(),
mergeMap((deviceState: DeviceState) => [
new SetMobile(deviceState.isMobile),
new SetTablet(deviceState.isTablet)
])
);
}
This effect is not the only i have issue with, buy the one of them.
Also to reproduce it correctly it might be required to clear site data in application browser tab.
Have anyone met such issue? :)
I've noticed that with the first server init everything works fine, but if i refresh page i get the error above
@YehorLitsov
createEffect adds the configuration ({ dispatch, useEffectsErrorHandler }) to the value returned from the callback.
In your example, EMPTY is returned from createEffect callback. So, when createEffect is called first time, it will mutate EMPTY observable (it will add config). Next time, the error will be thrown because createEffect will try to add effect configuration to the object that already has configuration, which is not allowed by default (check this).
Keep in mind that the same instance of EMPTY observable is shared throughout the application.
Here is the repo with simple reproduction.
As a workaround, you can switch:
if (isPlatformServer(this.platformId)) {
return EMPTY;
}
with:
if (isPlatformServer(this._platformId)) {
return () => EMPTY;
}
But I suggest you to take a look at OnRunEffects. Instead of if(isPlatformServer) in each window effect, you can do the following:
@Injectable()
export class WindowEffects implements OnRunEffects {
ngrxOnRunEffects(
resolvedEffects$: Observable<EffectNotification>
): Observable<EffectNotification> {
return isPlatformServer(this.platformId) ? EMPTY : resolvedEffects$;
}
}
Okay, thank you, i will try to resolve this in this way. But i find out that might be i can't use any other observables except that which has type as actions (or even just actions). So the way i rewrite is not to use fromEvent(window, 'resize') but to add and trigger resize action in root component. It solved the problem But anyway i will try your solution. Thanks