Giter Club home page Giter Club logo

universal's Introduction

logo Angular Universal fallbacks

Part of Web APIs for Angular

npm version npm bundle size Travis (.org) Coveralls github

A set of fallbacks to seamlessly use @ng-web-apis/common in Angular Universal apps. These packages have synced versions down to minor.

How to use

Add constants imported from this package to providers of your ServerAppModule. Typically, you can also use these mocks for tests. Idea of this package is โ€” you shouldn't have to mock DOM on the server side or test isPlatformBrowser all the time. Instead, you leverage Angular DI system to abstract from implementation. When possible, this package will provide the same functionality on the server side as you have in browser. In other cases you will get type-safe mocks and you can at least be sure you will not have cannot read propery of null or undefined is not a function errors in SSR.

IMPORTANT: This library relies on Node.js v10 and above on your server side

Mocks

Add following line to your server.ts to mock native classes used in other @ng-web-apis packages:

import '@ng-web-apis/universal/mocks';

It is recommended to keep the import statement at the top of your server.ts file

Tokens

You can provide tokens from this package into your app.server.module.ts to have type safe mocks for global objects on server side with UniversalModule:

@NgModule({
    imports: [
        AppBrowserModule,
        ServerModule,
        UniversalModule, // <-- add this
    ],
    bootstrap: [AppComponent],
})
export class AppServerModule {}

Special cases

When you use plain SSR without prerender you can retrieve some of the information from requests. Use the following helpers to harvest that info:

server.ts:

import {provideLocation, provideUserAgent} from '@ng-web-apis/universal';
// ...
app.get('/**/*', (req: Request, res: Response) => {
    res.render('../dist/index', {
        req,
        res,
        providers: [provideLocation(req), provideUserAgent(req)],
    });
});

universal's People

Contributors

waterplea avatar marsibarsi avatar m-aditya avatar nsbarsukov avatar aspiring-open-source-dev avatar dependabot[bot] avatar

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.