Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

adapter for cookie handling during pre-render #268

Closed

Description

@zanettin

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)

Activity

  1. PatrickJS commented on Feb 11, 2016

    @PatrickJS
    Contributor

    there needs to be a better cookies module that supports universal

  2. byavv commented on Feb 12, 2016

    @byavv

    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.

  3. PatrickJS commented on Feb 15, 2016

    @PatrickJS
    Contributor

    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.

  4. angular-automatic-lock-bot commented on Sep 4, 2019

    @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.

  5. locked and limited conversation to collaborators on Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions