Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always shows in angular universal #88

Open
blasco opened this issue Jul 18, 2020 · 16 comments
Open

Always shows in angular universal #88

blasco opened this issue Jul 18, 2020 · 16 comments
Assignees

Comments

@blasco
Copy link

blasco commented Jul 18, 2020

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?

@tinesoft
Copy link
Owner

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.

@blasco
Copy link
Author

blasco commented Jul 19, 2020

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
Copy link
Author

blasco commented Aug 11, 2020

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
Copy link
Author

blasco commented Aug 11, 2020

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?

@nilot111
Copy link

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

@blasco
Copy link
Author

blasco commented Sep 15, 2020

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

@nilot111
Copy link

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

@blasco
Copy link
Author

blasco commented Sep 18, 2020

@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

@tinesoft
Copy link
Owner

tinesoft commented Sep 18, 2020

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

@nilot111
Copy link

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

@l4nos
Copy link

l4nos commented Jun 24, 2021

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

@arindamINT
Copy link

arindamINT commented Nov 11, 2021

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

@nascarjake
Copy link

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.

@BenceUszkai
Copy link

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

@arindamINT
Copy link

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
Copy link

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

@tinesoft tinesoft self-assigned this Oct 14, 2022
@tinesoft tinesoft pinned this issue Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants