Git Product home page Git Product logo

ngx-server-push's Introduction

Disclaimer

This library is an alpha version. It is NOT recommended to use this library in a production environment.

ngx-server-push

Enables HTTP/2 Server Push in Angular Universal apps.

This library is designed to be used behind an HTTP/2 reverse proxy as written here.

Usage

0. Prepare your Angular Universal app

Follow the official doc.

1. Generate a config file

Option 1. With a cli tool

$ npm install --save-dev @ngx-server-push/cli

# Build your client side angular app with --stats-json option
$ ng build --prod --stats-json

# Execute ngxServerPush command with specifying the generated stats file
$ npx ngxServerPush dist/browser/stats.json

You can change the path of the output file with -o (or --out) option, which defaults to <The same dir as stats.json>/ngx-server-push.config.json.

$ npx ngxServerPush dist/browser/stats.json -o dist/foo-bar.config.json

Option 2. With a webpack plugin

$ npm install --save-dev @ngx-server-push/webpack
// webpack.config.js
const { NgxServerPushWebpackPlugin } = require('@ngx-server-push/webpack');

module.exports = {
  // ...
  plugins: [
    new NgxServerPushWebpackPlugin()
  ],
  // ...
};

You can change the name of the output file with out option, which defaults to ngx-server-push.config.json.

new NgxServerPushWebpackPlugin({ out: 'foo-bar.config.json' })

2. Add providers for your server-side engine

$ npm install @ngx-server-push/express
// src/app/app.server.module.ts
+ import { requestResponseWrapperProvider } from '@ngx-server-push/express';
+ import { ngModuleFactoryLoaderProvider } from '@ngx-server-push/preload-link-headers';
// If you want to use 
// ...

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule
  ],
  providers: [
    // Add universal-only providers here
+   requestResponseWrapperProvider,
+   ngModuleFactoryLoaderProvider
  ],
  bootstrap: [ AppComponent ],
})
export class AppServerModule {}
// server.ts
+ import { providePushConfig } from '@ngx-server-push/common';
// ...

app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
-   provideModuleMap(LAZY_MODULE_MAP)
+   provideModuleMap(LAZY_MODULE_MAP),
+   // The require path should be relative to the build output path of the server.ts
+   providePushConfig(require('./browser/ngx-server-push.config.json'))
  ]
}));

// ...

// All regular routes use the Universal engine
app.get('*', (req, res) => {
-   res.render('index', { req });
+   res.render('index', { req, res });
});

// ...

TODO: Support other engines than Express.

Demo

https://ngx-server-push-demo.appspot.com/

Chrome devtool network tab top

https://ngx-server-push-demo.appspot.com/lazy

Chrome devtool network tab lazy

You can see that the lazily loaded chunk (0.8c6a7127511b5f2ffe40.js) is pushed as well in the second screenshot.

Source code: https://github.com/kimamula/universal-starter/tree/ngx-server-push

Limitation

Chrome users may have to disable Experimental QUIC protocol in about:flags to make HTTP/2 Server Push work on Google App Engine (as in the demo).

Related issues:

ngx-server-push's People

Contributors

kimamula avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

iketutg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.