How handle cookies in angular 7 server side rendering












0















I recently implemented SSR on my angular 7 project for SEO. I've been using ngx-cookie-service. I noticed an error when i first ran the app with SSR, culprit was this.cookieService.check when attempting to render the app. In one of my components I have in ngOnInit(), it performs a method call that does the following:



const exists = this.cookieService.check('cookiename');
if (exists) {
showSpoilerCookieValue = this.cookieService.get('cookiename');
}


Temporarily i have removed the method that calls this logic to get the app to work.



How can i get this to work? Is there a better NPM package out there for cookies?



I saw posts recommending ngx-utils/cookies. However it doesn't appear to support @nguniversal/express-engine. There is a pull request fix this git pull that allows express-engine. for ngx-utils/cookies



I stupidly wasted time getting the ngx-utils/cookies, applying the pull, upgrading package.json with the latest angular, but didn't end up getting to test it as i ran out of time. I should have done more research before going down this path.



Is there a package cookie that will work for angular 7 SSR? im on angular 7.1 right now.










share|improve this question





























    0















    I recently implemented SSR on my angular 7 project for SEO. I've been using ngx-cookie-service. I noticed an error when i first ran the app with SSR, culprit was this.cookieService.check when attempting to render the app. In one of my components I have in ngOnInit(), it performs a method call that does the following:



    const exists = this.cookieService.check('cookiename');
    if (exists) {
    showSpoilerCookieValue = this.cookieService.get('cookiename');
    }


    Temporarily i have removed the method that calls this logic to get the app to work.



    How can i get this to work? Is there a better NPM package out there for cookies?



    I saw posts recommending ngx-utils/cookies. However it doesn't appear to support @nguniversal/express-engine. There is a pull request fix this git pull that allows express-engine. for ngx-utils/cookies



    I stupidly wasted time getting the ngx-utils/cookies, applying the pull, upgrading package.json with the latest angular, but didn't end up getting to test it as i ran out of time. I should have done more research before going down this path.



    Is there a package cookie that will work for angular 7 SSR? im on angular 7.1 right now.










    share|improve this question



























      0












      0








      0








      I recently implemented SSR on my angular 7 project for SEO. I've been using ngx-cookie-service. I noticed an error when i first ran the app with SSR, culprit was this.cookieService.check when attempting to render the app. In one of my components I have in ngOnInit(), it performs a method call that does the following:



      const exists = this.cookieService.check('cookiename');
      if (exists) {
      showSpoilerCookieValue = this.cookieService.get('cookiename');
      }


      Temporarily i have removed the method that calls this logic to get the app to work.



      How can i get this to work? Is there a better NPM package out there for cookies?



      I saw posts recommending ngx-utils/cookies. However it doesn't appear to support @nguniversal/express-engine. There is a pull request fix this git pull that allows express-engine. for ngx-utils/cookies



      I stupidly wasted time getting the ngx-utils/cookies, applying the pull, upgrading package.json with the latest angular, but didn't end up getting to test it as i ran out of time. I should have done more research before going down this path.



      Is there a package cookie that will work for angular 7 SSR? im on angular 7.1 right now.










      share|improve this question
















      I recently implemented SSR on my angular 7 project for SEO. I've been using ngx-cookie-service. I noticed an error when i first ran the app with SSR, culprit was this.cookieService.check when attempting to render the app. In one of my components I have in ngOnInit(), it performs a method call that does the following:



      const exists = this.cookieService.check('cookiename');
      if (exists) {
      showSpoilerCookieValue = this.cookieService.get('cookiename');
      }


      Temporarily i have removed the method that calls this logic to get the app to work.



      How can i get this to work? Is there a better NPM package out there for cookies?



      I saw posts recommending ngx-utils/cookies. However it doesn't appear to support @nguniversal/express-engine. There is a pull request fix this git pull that allows express-engine. for ngx-utils/cookies



      I stupidly wasted time getting the ngx-utils/cookies, applying the pull, upgrading package.json with the latest angular, but didn't end up getting to test it as i ran out of time. I should have done more research before going down this path.



      Is there a package cookie that will work for angular 7 SSR? im on angular 7.1 right now.







      angular cookies serverside-rendering angular7 ssr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 4:27









      Goncalo Peres

      1,4541519




      1,4541519










      asked Nov 20 '18 at 19:20









      Neophyte.netNeophyte.net

      1821311




      1821311
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Using



          @ngx-utils/cookies


          You need to check if you are in the browser or not



          import { isPlatformBrowser } from '@angular/common';
          import { CookiesService } from '@ngx-utils/cookies';
          constructor(
          private cookieService: CookiesService,
          @Inject(PLATFORM_ID) private platformId: Object
          ) {}

          isLoggedIn(): Promise<boolean> {
          return new Promise<any>((resolve, reject) => {
          if (isPlatformBrowser(this.platformId)) {
          // get the cookie here
          this.cookieService.get('token')
          resolve('something here')
          }
          });
          }


          Hope this helps!! ... same thing if you want to use window object you will need to do



          ...
          if (isPlatformBrowser(this.platformId)) {
          window.scrollTop() // or whatever you do with window object
          }





          share|improve this answer


























          • thanks ill check it out tomorrow

            – Neophyte.net
            Nov 23 '18 at 8:06











          • yep that did the trick. the stupid thing is, this code was already in my app.module.ts from following the SSR rendering example from the angular site. nice and simple solution.

            – Neophyte.net
            Nov 23 '18 at 22:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400084%2fhow-handle-cookies-in-angular-7-server-side-rendering%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Using



          @ngx-utils/cookies


          You need to check if you are in the browser or not



          import { isPlatformBrowser } from '@angular/common';
          import { CookiesService } from '@ngx-utils/cookies';
          constructor(
          private cookieService: CookiesService,
          @Inject(PLATFORM_ID) private platformId: Object
          ) {}

          isLoggedIn(): Promise<boolean> {
          return new Promise<any>((resolve, reject) => {
          if (isPlatformBrowser(this.platformId)) {
          // get the cookie here
          this.cookieService.get('token')
          resolve('something here')
          }
          });
          }


          Hope this helps!! ... same thing if you want to use window object you will need to do



          ...
          if (isPlatformBrowser(this.platformId)) {
          window.scrollTop() // or whatever you do with window object
          }





          share|improve this answer


























          • thanks ill check it out tomorrow

            – Neophyte.net
            Nov 23 '18 at 8:06











          • yep that did the trick. the stupid thing is, this code was already in my app.module.ts from following the SSR rendering example from the angular site. nice and simple solution.

            – Neophyte.net
            Nov 23 '18 at 22:40
















          2














          Using



          @ngx-utils/cookies


          You need to check if you are in the browser or not



          import { isPlatformBrowser } from '@angular/common';
          import { CookiesService } from '@ngx-utils/cookies';
          constructor(
          private cookieService: CookiesService,
          @Inject(PLATFORM_ID) private platformId: Object
          ) {}

          isLoggedIn(): Promise<boolean> {
          return new Promise<any>((resolve, reject) => {
          if (isPlatformBrowser(this.platformId)) {
          // get the cookie here
          this.cookieService.get('token')
          resolve('something here')
          }
          });
          }


          Hope this helps!! ... same thing if you want to use window object you will need to do



          ...
          if (isPlatformBrowser(this.platformId)) {
          window.scrollTop() // or whatever you do with window object
          }





          share|improve this answer


























          • thanks ill check it out tomorrow

            – Neophyte.net
            Nov 23 '18 at 8:06











          • yep that did the trick. the stupid thing is, this code was already in my app.module.ts from following the SSR rendering example from the angular site. nice and simple solution.

            – Neophyte.net
            Nov 23 '18 at 22:40














          2












          2








          2







          Using



          @ngx-utils/cookies


          You need to check if you are in the browser or not



          import { isPlatformBrowser } from '@angular/common';
          import { CookiesService } from '@ngx-utils/cookies';
          constructor(
          private cookieService: CookiesService,
          @Inject(PLATFORM_ID) private platformId: Object
          ) {}

          isLoggedIn(): Promise<boolean> {
          return new Promise<any>((resolve, reject) => {
          if (isPlatformBrowser(this.platformId)) {
          // get the cookie here
          this.cookieService.get('token')
          resolve('something here')
          }
          });
          }


          Hope this helps!! ... same thing if you want to use window object you will need to do



          ...
          if (isPlatformBrowser(this.platformId)) {
          window.scrollTop() // or whatever you do with window object
          }





          share|improve this answer















          Using



          @ngx-utils/cookies


          You need to check if you are in the browser or not



          import { isPlatformBrowser } from '@angular/common';
          import { CookiesService } from '@ngx-utils/cookies';
          constructor(
          private cookieService: CookiesService,
          @Inject(PLATFORM_ID) private platformId: Object
          ) {}

          isLoggedIn(): Promise<boolean> {
          return new Promise<any>((resolve, reject) => {
          if (isPlatformBrowser(this.platformId)) {
          // get the cookie here
          this.cookieService.get('token')
          resolve('something here')
          }
          });
          }


          Hope this helps!! ... same thing if you want to use window object you will need to do



          ...
          if (isPlatformBrowser(this.platformId)) {
          window.scrollTop() // or whatever you do with window object
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 21:47

























          answered Nov 20 '18 at 21:36









          freepowderfreepowder

          2896




          2896













          • thanks ill check it out tomorrow

            – Neophyte.net
            Nov 23 '18 at 8:06











          • yep that did the trick. the stupid thing is, this code was already in my app.module.ts from following the SSR rendering example from the angular site. nice and simple solution.

            – Neophyte.net
            Nov 23 '18 at 22:40



















          • thanks ill check it out tomorrow

            – Neophyte.net
            Nov 23 '18 at 8:06











          • yep that did the trick. the stupid thing is, this code was already in my app.module.ts from following the SSR rendering example from the angular site. nice and simple solution.

            – Neophyte.net
            Nov 23 '18 at 22:40

















          thanks ill check it out tomorrow

          – Neophyte.net
          Nov 23 '18 at 8:06





          thanks ill check it out tomorrow

          – Neophyte.net
          Nov 23 '18 at 8:06













          yep that did the trick. the stupid thing is, this code was already in my app.module.ts from following the SSR rendering example from the angular site. nice and simple solution.

          – Neophyte.net
          Nov 23 '18 at 22:40





          yep that did the trick. the stupid thing is, this code was already in my app.module.ts from following the SSR rendering example from the angular site. nice and simple solution.

          – Neophyte.net
          Nov 23 '18 at 22:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400084%2fhow-handle-cookies-in-angular-7-server-side-rendering%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          Npm cannot find a required file even through it is in the searched directory

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith