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.

Create Universal Storage module #272

Closed

Description

@PatrickJS

we should allow users to use localStorage both in server/client

Activity

  1. changed the title [-]Create Universal localStorage module[/-] [+]Create Universal Storage module[/+] on Feb 26, 2016
  2. added this to the Beta.0 milestone on Mar 18, 2016
  3. removed this from the Beta.0 milestone on Sep 10, 2016
  4. zcoon commented on Sep 13, 2016

    @zcoon

    @gdi2290 as this has been taken off of the Beta.0 milestone, do you have a vision for when angular universal will offer localStorage support? Thank you in advance

  5. PatrickJS commented on Sep 13, 2016

    @PatrickJS
    ContributorAuthor

    I'm introducing a Universal Cache service that will be used in localStorage after the initial rc6 release

  6. cyrilletuzi commented on Sep 16, 2016

    @cyrilletuzi
    Contributor

    @gdi2290 For now localStorage is not accepted in my Universal app (everything else is working) and crashes the server, is there something special to import or is it not ready yet ?

  7. PatrickJS commented on Sep 16, 2016

    @PatrickJS
    ContributorAuthor

    @cyrilletuzi to transfer state from the server to the client you can use a lifecycle hook in Node and the helper flags isBrowser and isNode
    main.browser.ts

    import { isBrowser, isNode } from 'angular2-universal';
    
    @NgModule({
      bootstrap: [ App ],
      declarations: [ App, Home ],
      imports: [
        UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
        FormsModule,
        RouterModule.forRoot([
          { path: '', component: Home, pathMatch: 'full' }
        ])
      ]
    })
    export class MainModule {
      constructor(public appRef: ApplicationRef) {
        // lives in your browser main module
        var key = 'yourServiceWithState._state'
        if (UNIVERSAL_CACHE[key]) {
          localStorage[key] = UNIVERSAL_CACHE[key]
        }
      }
    
    }

    main.node.ts

    import { isBrowser, isNode } from 'angular2-universal';
    
    @NgModule({
      bootstrap: [ App ],
      declarations: [ App, Home ],
      imports: [
        UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
        FormsModule,
        RouterModule.forRoot([
          { path: '', component: Home, pathMatch: 'full' }
        ])
      ]
    })
    export class MainModule {
      constructor(public appRef: ApplicationRef) {
    
      }
      // lives in your node main module
      universalDoDehydrate(universalCache) {
        var yourServiceWithState = this.appRef.injector.get(YourServiceWithState)
        var key = 'yourServiceWithState._state'
        universalCache[key] = yourServiceWithState._state.justOneProp
      }
    }

    or if you're using the same entry file then it's

    import { isBrowser, isNode } from 'angular2-universal';
    
    @NgModule({
      bootstrap: [ App ],
      declarations: [ App, Home ],
      imports: [
        UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
        FormsModule,
        RouterModule.forRoot([
          { path: '', component: Home, pathMatch: 'full' }
        ])
      ]
    })
    export class MainModule {
      constructor(public appRef: ApplicationRef) {
        // lives in your browser main module
        if (isBrowser) {
         var key = 'yourServiceWithState._state'
          if (UNIVERSAL_CACHE[key]) {
            localStorage[key] = UNIVERSAL_CACHE[key]
          }
        }
      }
      // lives in your node main module
      universalDoDehydrate(universalCache) {
        var yourServiceWithState = this.appRef.injector.get(YourServiceWithState)
        var key = 'yourServiceWithState._state'
        universalCache[key] = yourServiceWithState._state.justOneProp
      }
    }
  8. PatrickJS commented on Sep 16, 2016

    @PatrickJS
    ContributorAuthor

    after I create universal lifecycle hooks for the client then this will be more seamless

  9. joshmvandercom commented on Oct 3, 2016

    @joshmvandercom

    @gdi2290 where does UNIVERSAL_CACHE come from? I am running ^2.0.11

  10. 6 remaining items

  11. PatrickJS commented on Dec 5, 2016

    @PatrickJS
    ContributorAuthor

    to transfer state from client to server you can simply make an http call

  12. MarkPieszak commented on Dec 9, 2016

    @MarkPieszak
    Contributor

    It sounds like you need some initial logic the moment before the app gets serialized, or during, so that it can decide what route to instead render, or components to show,etc ?

    What are you trying now?

  13. MarkPieszak commented on Dec 13, 2016

    @MarkPieszak
    Contributor

    @WWelling Which file were you trying to do it, let me take a look so I can see what you need.

  14. MarkPieszak commented on Dec 13, 2016

    @MarkPieszak
    Contributor

    Is everything working or are you worried about security with this? No worries! Sorry if I'm slow to respond, I've been trying to tackle of a few things at once, it's hard to find time for everything :)

  15. PatrickJS commented on Jan 8, 2017

    @PatrickJS
    ContributorAuthor

    won't be supported officially
    angular/angular#13822

  16. vforv commented on Apr 11, 2017

    @vforv
  17. fulls1z3 commented on Apr 14, 2017

    @fulls1z3

    I made it :)

    Have a look at ng-seed/universal for its usage.

    Quoted from fulls1z3/universal#5

    Check the implementation: just updated this repo as well as publishing 4 separate packages for cache operations both on server and browser platforms. Everything is handled via the @Cached(...) decorator on the method/loader/etc which you want it cached. Also, there's the possibility to use caching methods (has, get, set) using the caching API.

    You might ask why to keep the structure so complex and consume 4 different packages? The answer is, not all the use cases depend on caching on the server platform. Furthermore, this structure allows to integrate any kind of custom caching with the server or browser platform (ex: session, api, etc).

    Currently there's no documentation available for these packages, because these days I'm kinda overloaded with many different tasks/projects. I think in a week or two I'll try to provide a good documentation as well as unit tests for these packages.

    However, try to have a look on this repo - about the use of CacheService and its instruments. I think it won't be difficult to understand the concepts, as I've kept the code style as DRY as possible.

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

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions