You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
adapter for cookie handling during pre-render #268
I just tried to test a working angular2 app using cookies with angular universal. It seams that cookies (using ng2-cookies) crashes on the pre-rendering because window is undefined. It would be great, if you could provide a adapter to get cookies from request header till pre-rendering the page. thanks for all your excellent work on this!
detailed error:
ORIGINAL EXCEPTION: ReferenceError: window is not defined
ORIGINAL STACKTRACE:
ReferenceError: window is not defined
at Function.Cookie.getCookie (/Users/development/tests/angular2/universal-starter/node_modules/ng2-cookies/src/services/cookie.js:14:24)
at new PersonalizedContent (/Users/development/tests/angular2/universal-starter/dist/server/bundle.js:224:50)
at /Users/development/tests/angular2/universal-starter/node_modules/angular2/src/core/reflection/reflection_capabilities.js:13:47
at Injector._instantiate (/Users/development/tests/angular2/universal-starter/node_modules/angular2/src/core/di/injector.js:757:27)
at Injector._instantiateProvider (/Users/development/tests/angular2/universal-starter/node_modules/angular2/src/core/di/injector.js:714:25)
at Injector._new (/Users/development/tests/angular2/universal-starter/node_modules/angular2/src/core/di/injector.js:703:21)
at InjectorInlineStrategy.instantiateProvider (/Users/development/tests/angular2/universal-starter/node_modules/angular2/src/core/di/injector.js:203:30)
at ElementDirectiveInlineStrategy.init (/Users/development/tests/angular2/universal-starter/node_modules/angular2/src/core/linker/element.js:558:24)
at new AppElement (/Users/development/tests/angular2/universal-starter/node_modules/angular2/src/core/linker/element.js:235:28)
at HostViewFactory.viewFactory_HostPersonalizedContent0 [as viewFactory] (viewFactory_HostPersonalizedContent:73:38)
As far as I understand, if you need window or any DOM element, you need create kinda service and init it outside of server rendering process (in bootstrap for instance). May be it's not the best design, but works for me:
bootstrap(App,PROVIDERS)// <--- bootstrap works only in a browser, where "window" exists.then((appRef: ComponentRef)=>{
let storage: MyStorage=appRef.injector.get(MyStorage);//<--- service, which uses window storage.initStorage(window.localStorage);//<--- init it here },error=>console.log(error))
@gdi2290, Here I hinted at exactly what you'd said about support of universal. That is why I got out with the design like this. I'd be appreciated any ideas how to make it better without tricks.
Closing for issue #273 this is more of a documentation issue explaining how to create universal services. Either way, we should provide a simple solution if only as an example.
Activity
PatrickJS commentedon Feb 11, 2016
there needs to be a better cookies module that supports universal
byavv commentedon Feb 12, 2016
As far as I understand, if you need window or any DOM element, you need create kinda service and init it outside of server rendering process (in bootstrap for instance). May be it's not the best design, but works for me:
@gdi2290, Here I hinted at exactly what you'd said about support of universal. That is why I got out with the design like this. I'd be appreciated any ideas how to make it better without tricks.
PatrickJS commentedon Feb 15, 2016
Closing for issue #273 this is more of a documentation issue explaining how to create universal services. Either way, we should provide a simple solution if only as an example.
angular-automatic-lock-bot commentedon Sep 4, 2019
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.