'In an Angular 8 SSR app, there is a memory leak

I have an angular SSR app that causes memory leaks on the server

it keeps on increasing memory on the server when the application is used and opened by many users.Unsubscribing subscriptions on ngOnDestroy doesn't work, still memory leaks persist

Not able to find whats the issue and whats the correct way to solve the problem

I am attaching the server.ts code

import "zone.js/dist/zone-node";

import * as express from "express";

import { join } from "path";

const domino = require("domino");
const fs = require("fs");
const path = require("path");

const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), "dist/browser");

const template = fs
  .readFileSync(path.join(DIST_FOLDER, "index.html"))
  .toString();
const win = domino.createWindow(template);

(global as any)["window"] = win;
(global as any)["KeyboardEvent"] = win.KeyboardEvent;
(global as any)["HTMLInputElement"] = win.HTMLInputElement;
(global as any)["MouseEvent"] = win.MouseEvent;
(global as any)["Event"] = win.Event;
(global as any)["document"] = win.document;
(global as any)["navigator"] = win.navigator;
(global as any)["FormData"] = win.FormData;
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {
  AppServerModuleNgFactory,
  LAZY_MODULE_MAP,
  ngExpressEngine,
  provideModuleMap,
} = require("./dist/server/main");

app.engine(
  "html",
  ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [provideModuleMap(LAZY_MODULE_MAP)],
  })
);

app.set("view engine", "html");
app.set("views", DIST_FOLDER);

app.get(
  "*.*",
  express.static(DIST_FOLDER, {
    maxAge: "1y",
  })
);

app.get(/.*aspx$/, function (req, res) {
  res.redirect(301, "WEBSITE URL");
});

// All regular routes use the Universal engine
app.get("*", (req, res, next) => {
  // special for robots.txt
  if (req.url === "/robots.txt") {
    next();
    return;
  }

  res.render("index", { req });
});

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source