PillowPillow / ng2-webstorage

Localstorage and sessionstorage manager - Angular service
MIT License
428 stars 92 forks source link

How to Support Server Rendering with ngx-webstorage #50

Closed MapleYe closed 7 years ago

MapleYe commented 7 years ago

Doing an online shop project. Decorate cartline array with @LocalStorage(). The shopping cart works perfectly on the client side. But it failed (cannot continue loading the website) when I tried server rendering. So is it possible to get the item count when the app is loaded first from the server?

PillowPillow commented 7 years ago

Hi, the library currently doesn't support server side rendering. I'm not familiar with the server side rendering so if you have a sample repository that i could use to fix the issue, that would be great.

A PR would also be appreciated :).

misha130 commented 7 years ago

You might be able to solve this issue on your own by extending WebStorage library and useClass. Instead of using window.localStorage(since it doesn't exist) you'd have to use the server side storage capabilities whatever they are(IndexDb, WebDB, etc)

MapleYe commented 7 years ago

Thanks misha130. I'll give them a try.

dylanwatsonsoftware commented 6 years ago

For others coming later, I was able to workaround this by simple wrapping code that accessed the webstorage with:

import { Inject, PLATFORM_ID } from "@angular/core";
import { isPlatformBrowser } from "@angular/common";

// Service definition goes here

  constructor(
    private localStorage: LocalStorageService,
    @Inject(PLATFORM_ID) private platformId: Object) {
    if (isPlatformBrowser(platformId)) {
        // Code that uses localStorage
    }
  }
AnthonyNahas commented 6 years ago

@lotsabackscatter this will only work with injected services...

what about the decorators ? ;)