angular/angularfire

Upgrading to 16+ breaks pre-rendering with SSR

tmk1991 opened this issue · 2 comments

When moving from @angular/fire 7.6.1 to 16 or 17, the prerendering with SSR is failing.

The following works:
"@angular/common": "^17.3.12",
"@angular/fire": "^7.6.1",
"firebase": "^10.14.1"

The error during prerender with any @angular/fire 16 or 17 version is down below. I assume the issue is that it cannot load the modules the right way with the server for prerender but not sure why. I'm using
"target": "ES2022",
"module": "ES2022",
in my TS config.

Error
It gets to prerendering stage (so the project builds fine).

⠋ Prerendering 1 route(s) to /dist/retail/browser.../dist/retail/server/main.js:1

Then I get a ton of the compiled code

(()=>{var installedChunks,leafPrototypes,getProto,__webpack_modules__={778:(__unused_webpack_module,__webpack_exports__2,__webpack_require__2)=>{"use strict";__webpack_require__2.d(__webpack_exports__2,{P:()=>GalleryComponentsModule});var _shared_shared_module__WEBPACK_IMPORTED_MODULE_0__=__webpack_require__2(65393),_angular_common__WEBPACK_IMPORTED_MODULE_3__=__webpack_require__2(94556),_nebular_theme__WEBPACK_IMPORTED_MODULE_4__=__webpack_require__2(67214),_shop_shop_components_shop_components_module__WEBPACK_IMPORTED_MODULE_1__=__webpack_require__2(56241),_angular_core__WEBPACK_IMPORTED_MODULE_2__=__webpack_require__2(42249);let GalleryComponentsModule=(()=>{class GalleryComponentsModule2{static{this.\u0275fac=function(t){return new(t||
.... (Continues for a LONG time)

Then I see this syntax error:

SyntaxError: Unexpected identifier '#target'
    at wrapSafe (node:internal/modules/cjs/loader:1281:20)
    at Module._compile (node:internal/modules/cjs/loader:1321:27)
    at Object..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Function._load (node:internal/modules/cjs/loader:1024:12)
    at Module.require (node:internal/modules/cjs/loader:1233:19)
    at require (node:internal/modules/helpers:179:18)
    at /Users/troykoss/workspace/russells/website/retail/node_modules/@angular-devkit/build-angular/src/builders/prerender/routes-extractor-worker.js:44:143
    at _ZoneDelegate.invoke (/Users/troykoss/workspace/russells/website/retail/node_modules/zone.js/bundles/zone.umd.js:416:32)
    at ZoneImpl.run (/Users/troykoss/workspace/russells/website/retail/node_modules/zone.js/bundles/zone.umd.js:147:47)

I'm not sure how to debug further. I can't seem to see what the difference in 16+ and 7.6.1 is that would cause this issue unless its something to do with commonjs to esm.

I was going to post on the SSR repo but since this issue spawns specifically around an angular/fire update, I thought it would be more appropriate here.

This is the server.ts:

import 'zone.js';

import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr';
import * as express from 'express';
import { existsSync } from 'fs';
import { join } from 'path';
import { AppServerModule } from './src/main.server';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/retail/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html'))
    ? join(distFolder, 'index.original.html')
    : join(distFolder, 'index.html');

  const commonEngine = new CommonEngine();

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get(
    '*.*',
    express.static(distFolder, {
      maxAge: '1y'
    })
  );

  // All regular routes use the Angular engine
  server.get('*', (req, res, next) => {
    const { protocol, originalUrl, baseUrl, headers } = req;

    commonEngine
      .render({
        bootstrap: AppServerModule,
        documentFilePath: indexHtml,
        url: `${protocol}://${headers.host}${originalUrl}`,
        publicPath: distFolder,
        providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }]
      })
      .then((html) => res.send(html))
      .catch((err) => next(err));
  });

  return server;
}

function run(): void {
  const port = process.env['PORT'] || 4000;

  // Start up the Node server
  const server = app();
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = (mainModule && mainModule.filename) || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  run();
}

export default AppServerModule;

This issue does not seem to follow the issue template. Make sure you provide all the required information.

If I change prerender to not dynamically discover routes and provide just 1 route, it gives the same error but on a different part of the prerendering

SyntaxError: Unexpected identifier '#target'
    at wrapSafe (node:internal/modules/cjs/loader:1486:18)
    at Module._compile (node:internal/modules/cjs/loader:1528:20)
    at Object..js (node:internal/modules/cjs/loader:1706:10)
    at Module.load (node:internal/modules/cjs/loader:1289:32)
    at Function._load (node:internal/modules/cjs/loader:1108:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
    at Module.require (node:internal/modules/cjs/loader:1311:12)
    at require (node:internal/modules/helpers:136:16)
    at /Users/xxx/workspace/xxx/website/retail/node_modules/@angular-devkit/build-angular/src/builders/prerender/render-worker.js:53:178

Prerender code where failure occurs:
async function render({ indexFile, deployUrl, minifyCss, outputPath, serverBundlePath, route, inlineCriticalCss, }) {
const result = {};
const browserIndexOutputPath = path.join(outputPath, indexFile);
const outputFolderPath = path.join(outputPath, route);
const outputIndexPath = path.join(outputFolderPath, 'index.html');
const { ɵSERVER_CONTEXT, AppServerModule, renderModule, renderApplication, default: bootstrapAppFn, } = (await Promise.resolve(${serverBundlePath}).then(s => __importStar(require(s))));