Have ideas to improve npm?Join in the discussion! »

    @ngx-cache/platform-server
    TypeScript icon, indicating that this package has built-in type declarations

    9.0.0 • Public • Published

    @ngx-cache/platform-server npm version npm downloads

    Server platform implementation of ngx-cache

    CircleCI coverage tested with jest Conventional Commits Angular Style Guide

    Please support this project by simply putting a Github star. Share this library with friends on Twitter and everywhere else you can.

    Table of contents:

    Getting started

    Installation

    You can install @ngx-cache/platform-server using npm

    npm install @ngx-cache/platform-server --save
    

    Note: You should have already installed @ngx-cache/core.

    Note: You should have also installed @ngx-cache/fs-storage.

    Examples

    • ng-seed/universal is an officially maintained seed project, showcasing common patterns and best practices for @ngx-cache/platform-server.

    Related packages

    The following packages may be used in conjunction with @ngx-cache/platform-server:

    Adding @ngx-cache/platform-server to your project (SystemJS)

    Add map for @ngx-cache/platform-server in your systemjs.config

    '@ngx-cache/platform-server': 'node_modules/@ngx-cache/platform-server/bundles/platform-server.umd.min.js'

    app.module configuration

    • Import ServerCacheModule using the mapping '@ngx-cache/platform-server' and append ServerCacheModule.forRoot({...}) within the imports property of app.server.module (considering the app.server.module is the server module in Angular Universal application).
    • Import CACHE injection token using the mapping '@ngx-cache/core', FsCacheService using the mapping '@ngx-cache/platform-server'.
    • Provide CACHE using FsCacheService, by calling the forRoot static method using the ServerCacheModule.
    • Import STORAGE injection token using the mapping '@ngx-cache/core', FsStorageService using the mapping '@ngx-cache/fs-storage'.
    • Provide STORAGE using FsStorageService, by calling the forRoot static method using the ServerCacheModule.
    • Import FsStorageLoader and fsStorageFactory using the mapping '@ngx-cache/fs-storage'.
    • Provide FsStorageLoader using fsStorageFactory, by calling the forRoot static method using the ServerCacheModule.
    • Pass CacheService together with ApplicationRef and TransferState to the implementation of bootstrapFactory method below.

    app.server.module.ts

    ...
    import { CacheService, CACHE, STORAGE } from '@ngx-cache/core';
    import { ServerCacheModule, FsCacheService } from '@ngx-cache/platform-server';
    import { fsStorageFactory, FsStorageLoader, FsStorageService } from '@ngx-cache/fs-storage';
     
    ...
     
     
    export function bootstrapFactory(appRef: ApplicationRef,
                                     transferState: TransferState,
                                     cache: CacheService): () => Subscription {
      return () => appRef.isStable
        .filter(stable => stable)
        .first()
        .subscribe(() => {
          transferState.set<any>(makeStateKey(cache.key), JSON.stringify(cache.dehydrate()));
        });
    }
     
     
    @NgModule({
      declarations: [
        AppComponent,
        ...
      ],
      ...
      imports: [
        ...
        ServerCacheModule.forRoot([
          {
            provide: CACHE,
            useClass: FsCacheService
          },
          {
            provide: STORAGE,
            useClass: FsStorageService
          },
          {
            provide: FsStorageLoader,
            useFactory: (fsStorageFactory)
          }
        ]),
      ],
      ...
      providers: [
        {
          provide: APP_BOOTSTRAP_LISTENER,
          useFactory: (bootstrapFactory),
          multi: true,
          deps: [
            ApplicationRef,
            TransferState,
            CacheService
          ]
        }
      ],
      bootstrap: [AppComponent]
    })
    export class AppServerModule {
    }

    👍 Yeah! @ngx-cache/platform-server will now provide server platform implementation to @ngx-cache/core.

    Credits

    License

    The MIT License (MIT)

    Copyright (c) 2019 Burak Tasci

    Install

    npm i @ngx-cache/platform-server

    DownloadsWeekly Downloads

    256

    Version

    9.0.0

    License

    MIT

    Unpacked Size

    119 kB

    Total Files

    24

    Last publish

    Collaborators

    • avatar