async render()

in modules/common/engine/src/engine.ts [55:119]


  async render(opts: RenderOptions): Promise<string> {
    const { inlineCriticalCss = true } = opts;

    if (opts.publicPath && opts.documentFilePath && opts.url !== undefined) {
      const url = new URL(opts.url);
      // Remove leading forward slash.
      const pathname = url.pathname.substring(1);
      const pagePath = resolve(opts.publicPath, pathname, 'index.html');

      if (pagePath !== resolve(opts.documentFilePath)) {
        // View path doesn't match with prerender path.
        let pageExists = this.pageExists.get(pagePath);
        if (pageExists === undefined) {
          pageExists = await exists(pagePath);
          this.pageExists.set(pagePath, pageExists);
        }

        if (pageExists) {
          // Serve pre-rendered page.
          return fs.promises.readFile(pagePath, 'utf-8');
        }
      }
    }

    // if opts.document dosen't exist then opts.documentFilePath must
    const extraProviders = [...(opts.providers || []), ...(this.providers || [])];

    let doc = opts.document;
    if (!doc && opts.documentFilePath) {
      doc = await this.getDocument(opts.documentFilePath);
    }

    if (doc) {
      extraProviders.push({
        provide: INITIAL_CONFIG,
        useValue: {
          document: inlineCriticalCss
            ? // Workaround for https://github.com/GoogleChromeLabs/critters/issues/64
              doc.replace(
                / media="print" onload="this\.media='all'"><noscript><link .+?><\/noscript>/g,
                '>',
              )
            : doc,
          url: opts.url,
        },
      });
    }

    const moduleOrFactory = this.module || opts.bootstrap;
    const html = await renderModule(moduleOrFactory, { extraProviders });
    if (!inlineCriticalCss) {
      return html;
    }

    const { content, errors, warnings } = await this.inlineCriticalCssProcessor.process(html, {
      outputPath: opts.publicPath ?? (opts.documentFilePath ? dirname(opts.documentFilePath) : ''),
    });

    // eslint-disable-next-line no-console
    warnings?.forEach((m) => console.warn(m));
    // eslint-disable-next-line no-console
    errors?.forEach((m) => console.error(m));

    return content;
  }