ngx-cookieconsent icon indicating copy to clipboard operation
ngx-cookieconsent copied to clipboard

Always shows in angular universal

Open blasco opened this issue 4 years ago • 16 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

It always shows in angular universal. In the browser version works correctly, but not sure if it needs to be added in a different way when using angular universal. My guess is that the server is rendering it and showing it, even when the user has already accepted it. Any possible solution?

blasco avatar Jul 18 '20 20:07 blasco

Hi @blasco

the underlying cookieconsent library relies on client side cookies to determine whether or not user has given his consent. This information is stored as a cookie. I'm not sure you can set/store cookies in a server side rendering (which Angular Universal is)...

Maybe using this cookie-parser middleware? Never used it through, let's me know if it works for you.

tinesoft avatar Jul 19 '20 14:07 tinesoft

yes, I would like to have the option of not running the cookie consent automatically (so I can run it only when I check that the code is running on the browser). The problem is that including the module results in the window being shown. Is there await to prevent the automatic init?

blasco avatar Jul 19 '20 14:07 blasco

Any way to achieve this? The only thing I need is to init the module only if I'm in the client and not the server, which is easy to detect. The only problem I have is that the init is called when the module is included... is there a way to prevent this?

blasco avatar Aug 11 '20 11:08 blasco

It's very weird though, I don't see any cookie being place. Maybe the error has to do with that I'm using Angular 10?

blasco avatar Aug 11 '20 11:08 blasco

Hi blasco, one question , did you do something else apart from documentation ??, i could not get it to work in angular 10 universal, so i would really appreciate if you can help me or share me a little of the process of adding this cookieconsent. I am getting following error, while in single page app works great : ERROR TypeError: Cannot read property 'initialise' of undefined at ngx_cookieconsent_NgcCookieConsentService.init

nilot111 avatar Sep 14 '20 16:09 nilot111

@nilot111 I call the initialization only at the browser. Add a check that you are not on the server side to avoid these errors.

blasco avatar Sep 15 '20 04:09 blasco

@blasco thanks, but how can you add that checking if it initialize by itself with just adding in constructor . ??

Following documentation i need to add it to app.component.ts just like this : constructor(private ccService: NgcCookieConsentService){}

As my understanding this by itself call initialize, so how can i prevent it only executes in browser since it is in the constructor. thanks in advance.

nilot111 avatar Sep 15 '20 17:09 nilot111

@nilot111 I'm not using this library anymore, I don't remember how I made it work, but I think you are right and the init is called in the constructor

blasco avatar Sep 18 '20 07:09 blasco

Hi all,

Sorry for the late response...

The library is indeed automatically initialized in the constructor. This was intended for convenient reasons (to avoid the implicit call to ccService.init()) , but now I realize this may not be ideal for every use case, in particular when the initialization need to be conditionned.

I will update to change this behavior or at least configure it in the upcoming release of the library.

Stay tuned

tinesoft avatar Sep 18 '20 19:09 tinesoft

@tinesoft I am glad to hear that you will update. Thank you so much for giving time to this. Regards.

nilot111 avatar Sep 18 '20 21:09 nilot111

Did you manage to change this to not automatically call the init method?

It would be helpful if you can as I have a production site that needs this to work on using Angular Universal.

I'm happy to take a stab at changing if you want,

As an alternative solution for anyone else waiting for this, you can use Angular's Injector to conditionally inject into the plugin into the main compponent depending on the presence of a cookie.

The below link explains conditional injection.

https://stackoverflow.com/questions/43450259/how-to-conditionally-inject-service-into-component

l4nos avatar Jun 24 '21 14:06 l4nos

@tinesoft Why it is always opening the popup? How to set a cookie after clicking the accept button? How to check whether the cookie is already saved or not?

arindamINT avatar Nov 11 '21 12:11 arindamINT

I am also having a similar issue to the one arindamINT explains above - the popup always shows up every time the page loads and clicking "Got It" while using the "info" type does not set any cookie.

nascarjake avatar Dec 15 '21 06:12 nascarjake

Did you tried it on localhost? you cannot save cookies for localhost. you have to add localhost to host file.

BenceUszkai avatar Mar 31 '22 09:03 BenceUszkai

Did you tried it on localhost? you cannot save cookies for localhost. you have to add localhost to host file.

I tried with both, localhost as well as a staging server.

arindamINT avatar Apr 05 '22 04:04 arindamINT

@tinesoft Why it is always opening the popup? How to set a cookie after clicking the accept button? How to check whether the cookie is already saved or not?

arindamINT avatar Apr 05 '22 04:04 arindamINT