How to use the @angular/platform-server.platformDynamicServer function in @angular/platform-server

To help you get started, we’ve selected a few @angular/platform-server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github asadsahi / AspNetCoreSpa / ClientApp / boot.server.ts View on Github external
export default createServerRenderer(params => {
    const providers = [
        { provide: INITIAL_CONFIG, useValue: { document: '', url: params.url } },
        { provide: APP_BASE_HREF, useValue: params.baseUrl },
        { provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
    ];

    return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {
        const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
        const state = moduleRef.injector.get(PlatformState);
        const zone: NgZone = moduleRef.injector.get(NgZone);

        return new Promise((resolve, reject) => {
            zone.onError.subscribe((errorInfo: any) => reject(errorInfo));
            appRef.isStable.first(isStable => isStable).subscribe(() => {
                // Because 'onStable' fires before 'onError', we have to delay slightly before
                // completing the request in case there's an error to report
                setImmediate(() => {
                    resolve({
                        html: state.renderToString()
                    });
                    moduleRef.destroy();
                });
            });
github Swastika-IO / sio.core / src / Swastika.Messenger.Web / ClientApp / boot.server.ts View on Github external
export default createServerRenderer(params => {
    const providers = [
        { provide: INITIAL_CONFIG, useValue: { document: '', url: params.url } },
        { provide: APP_BASE_HREF, useValue: params.baseUrl },
        { provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
    ];

    return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {
        const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
        const state = moduleRef.injector.get(PlatformState);
        const zone = moduleRef.injector.get(NgZone);

        return new Promise((resolve, reject) => {
            //zone.onError.subscribe((errorInfo: any) => reject(errorInfo));
            appRef.isStable.first(isStable => isStable).subscribe(() => {
                // Because 'onStable' fires before 'onError', we have to delay slightly before
                // completing the request in case there's an error to report
                setImmediate(() => {
                    resolve({
                        html: state.renderToString()
                    });
                    moduleRef.destroy();
                });
            });
github angular / universal / modules / aspnetcore-engine / src / main.ts View on Github external
export async function ngAspnetCoreEngine(options: Readonly)
  : Promise {
  if (!options.appSelector) {
    const selector = `" appSelector: '<${appSelector}>' "`;
    throw new Error(`appSelector is required! Pass in ${selector},
     for your root App component.`);
  }

  // Grab the DOM "selector" from the passed in Template  for example = "app-root"
  appSelector = options.appSelector.substring(1, options.appSelector.indexOf('>'));

  const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory);
  const compiler: Compiler = compilerFactory.createCompiler([
    {
      providers: [
        { provide: ResourceLoader, useClass: FileLoader, deps: [] }
      ]
    }
  ]);

  const moduleOrFactory = options.ngModule;
  if (!moduleOrFactory) {
    throw new Error('You must pass in a NgModule or NgModuleFactory to be bootstrapped');
  }

  const extraProviders = [
    ...(options.providers || []),
    getReqResProviders(options.request.origin, options.request.data.request),
github apollographql / apollo-angular / tests / integration.spec.ts View on Github external
test('using long form should work', async(() => {
      const platform =
        platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: doc}}]);

      platform.bootstrapModule(AsyncServerModule)
        .then((moduleRef) => {
          const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
          return toPromise.call(first.call(
            filter.call(applicationRef.isStable, (isStable: boolean) => isStable)));
          })
          .then(() => {
            const str = platform.injector.get(PlatformState).renderToString();
            expect(clearNgVersion(str)).toMatchSnapshot();
            platform.destroy();
            called = true;
          });
    }));
github TrilonIO / aspnetcore-angular-universal / ClientApp / polyfills / temporary-aspnetcore-engine.ts View on Github external
export function ngAspnetCoreEngine(
  options: IEngineOptions
): Promise<{ html: string, globals: { styles: string, title: string, meta: string, transferData?: {}, [key: string]: any } }> {

  options.providers = options.providers || [];

  const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory);
  const compiler: Compiler = compilerFactory.createCompiler([
    {
      providers: [
        { provide: ResourceLoader, useClass: FileLoader }
      ]
    }
  ]);

  return new Promise((resolve, reject) => {

    try {
      const moduleOrFactory = options.ngModule;
      if (!moduleOrFactory) {
        throw new Error('You must pass in a NgModule or NgModuleFactory to be bootstrapped');
      }
github marclaval / optimize-angular-app / scripts / prerender.js View on Github external
"use strict";

const fs = require('fs');
require('core-js/client/core.js');
require('zone.js/dist/zone-node.js');
require('zone.js/dist/long-stack-trace-zone');
require('rxjs/Rx');

const co = require('@angular/core');
const ps = require('@angular/platform-server');
const prerenderModule = require('../tmp/app/module.prerender.server');

const document = fs.readFileSync('./public/prerender/index.html', 'utf-8');
co.enableProdMode();

const platform = ps.platformDynamicServer([{
  provide: ps.INITIAL_CONFIG,
  useValue: {
    document: document,
    url: 'http://localhost:8080'
  }
}]);
platform.bootstrapModule(prerenderModule.AppModule).then(moduleRef => {
  const state = moduleRef.injector.get(ps.PlatformState);
  const appRef = moduleRef.injector.get(co.ApplicationRef);

  appRef._isStable
    .filter((isStable) => isStable)
    .first()
    .subscribe((stable) => {
      try {
        fs.mkdirSync('./tmp/prerender');
github dimangulov / expenses / src / Expenses / Client / polyfills / temporary-aspnetcore-engine.ts View on Github external
export function ngAspnetCoreEngine(
  options: IEngineOptions
): Promise<{ html: string, globals: { styles: string, title: string, meta: string, transferData?: {}, [key: string]: any } }> {

  options.providers = options.providers || [];

  const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory);
  const compiler: Compiler = compilerFactory.createCompiler([
    {
      providers: [
        { provide: ResourceLoader, useClass: FileLoader }
      ]
    }
  ]);

  return new Promise((resolve, reject) => {

    try {
      const moduleOrFactory = options.ngModule;
      if (!moduleOrFactory) {
        throw new Error('You must pass in a NgModule or NgModuleFactory to be bootstrapped');
      }
github angular / universal / modules / common / engine / src / engine.ts View on Github external
getCompiler(): Compiler {
    const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory);

    return compilerFactory.createCompiler([
      {providers: [{provide: ResourceLoader, useClass: FileLoader, deps: []}]}
    ]);
  }