Socket
Socket
Sign inDemoInstall

@ng-web-apis/universal

Package Overview
Dependencies
8
Maintainers
4
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ng-web-apis/universal

A set of fallback for @ng-web-apis/common for Angular Universal


Version published
Weekly downloads
3K
increased by22.37%
Maintainers
4
Install size
346 kB
Created
Weekly downloads
 

Readme

Source

logo Angular Universal fallbacks

npm version npm bundle size codecov

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.

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 {}

Alternatively, if you have a standalone app that is initialized using the bootstrapApplication function, you can import UNIVERSAL_PROVIDERS in the following manner:

const serverConfig: ApplicationConfig = {
  providers: [
    provideServerRendering(),
    UNIVERSAL_PROVIDERS, // <-- add this
  ],
};

const config = mergeApplicationConfig(appConfig, serverConfig);
const bootstrap = () => bootstrapApplication(AppComponent, config);

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)],
  });
});

Keywords

FAQs

Last updated on 01 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc