title: How to configure cache in Angular Universal
body: <p>I followed a tutorial for integrating Universal in our Angular 9 app.
  Now we have server.ts configured.</p>
  
  <p>In every tutorial I see this:</p>
  
  <pre><code>// Example Express Rest API endpoints
  // app.get('/api/**', (req, res) =&gt; { });
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>server.get(
          '*.*',
          express.static(distFolder, {
              maxAge: '1y',
          })
      );
  </code></pre>
  
  <p>If we leave it like that I suppose every request is cached, also what goes to /api. Is this right?
  Our app has lots of "product pages" where stock and price could change frequently. So we need to have always fresh stock and price. For this reason I wouldn't cache data from server that is returned from our API calls that are in "/api/" path (or <a href="https://api.my-app.com" rel="nofollow noreferrer">https://api.my-app.com</a>" if absolute path is needed). Leaving everything like written above should cache everything, if I'm not wrong. I made a test changing a price directly in the database and I expected to see the old one, but it's not like that, I see always the fresh one. 
  So my doubt is that cache mechanism dosn't work. And also when I navigate through components I continue to see every api calls (not cached)</p>
  
  <p>Can anybody explain me better this mechanism please?</p>
  
  <p>Another question about this is: If cache works, how to clear it in node? Node caches in memory everything. I supposed cache would be cleared when we stop node. Is this right?</p>
  
  <p>This is my package json:</p>
  
  <pre><code>"scripts": {
      "ng": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng",
      "start": "npm run ng -- serve",
      "serve:server": "node ./dist-server/main.js",
      "serve:server:debug": "node --inspect ./dist-server/main.js",
      "start:server": "npm run build:server &amp;&amp; node ./dist-server/main.js",
      "start:server:debug": "npm run build:server &amp;&amp; node ./dist-server/main.js --inspect",
      "build": "npm run ng -- build",
      "build:server": "ng run my-app:server:production",
      "ssr:watch": "ng run my-app:serve-ssr:production",
      "demo:ssr:watch": "ng run universal-demo:serve-ssr:dev",
      "build-all": "npm-run-all build-production build:server-app:prod",
      "start:express-server": "ts-node -P ./src/tsconfig.server.json ./server.ts",
      "prerender": "ts-node -P ./server.tsconfig.json ./prerender.ts",
      "prerender:debug": "ts-node -P ./server.tsconfig.json --inspect ./prerender.ts",
      "test": "npm run ng -- test",
      "lint": "npm run ng -- lint",
      "e2e": "npm run ng -- e2e",
      "analyze": "webpack-bundle-analyzer dist/stats.json",
      "compodoc": "npx compodoc -p src/tsconfig.app.json",
      "build:stats": "ng build --stats-json --prod",
      "build-preprod": "ng build --configuration preprod --index=/src/index/preprod/index.html",
      "build-production": "ng build --configuration production --index=/src/index/production/index.html",
      "build-staging": "ng build --configuration staging --index=/src/index/staging/index.html"
    }
  </code></pre>
  
  <p>This is my angular.json:</p>
  
  <pre><code>{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
      "my-app": {
        "root": "",
        "sourceRoot": "src",
        "projectType": "application",
        "prefix": "app",
        "schematics": {
          "@schematics/angular:component": {
            "style": "scss"
          }
        },
        "architect": {
          "build": {
            "builder": "@angular-devkit/build-angular:browser",
            "options": {
              "aot": true,
              "outputPath": "dist",
              "index": "src/index.html",
              "main": "src/main.ts",
              "polyfills": "src/polyfills.ts",
              "tsConfig": "src/tsconfig.app.json",
              "stylePreprocessorOptions": {
                "includePaths": ["src/assets/sass"]
              },
              "assets": [
                "src/assets",
                "src/favicon.ico",
                "src/manifest.json",
                "src/firebase-messaging-sw.js",
                "src/main-sw.js",
                "src/assets/js/intersection-observer.js"
              ],
              "styles": [
                "src/styles/styles.scss",
                "src/styles/my-app.scss",
                "node_modules/font-awesome/css/font-awesome.css"
              ],
              "scripts": [],
              "es5BrowserSupport": true
            },
            "configurations": {
              "staging": {
                "budgets": [
                  {
                    "type": "anyComponentStyle",
                    "maximumWarning": "6kb"
                  }
                ],
                "optimization": true,
                "outputHashing": "all",
                "sourceMap": false,
                "extractCss": true,
                "namedChunks": false,
                "aot": true,
                "extractLicenses": true,
                "vendorChunk": false,
                "buildOptimizer": true,
                "ngswConfigPath": "src/app/config/ngsw-config.json",
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.staging.ts"
                  }
                ],
                "serviceWorker": true
              },
              "preprod": {
                "budgets": [
                  {
                    "type": "anyComponentStyle",
                    "maximumWarning": "6kb"
                  }
                ],
                "optimization": true,
                "outputHashing": "all",
                "sourceMap": false,
                "extractCss": true,
                "namedChunks": false,
                "aot": true,
                "extractLicenses": true,
                "vendorChunk": false,
                "buildOptimizer": true,
                "ngswConfigPath": "src/app/config/ngsw-config.json",
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.preprod.ts"
                  }
                ],
                "serviceWorker": true
              },
              "production": {
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                  }
                ],
                "optimization": true,
                "outputHashing": "all",
                "sourceMap": true,
                "extractCss": true,
                "namedChunks": true,
                "aot": true,
                "extractLicenses": true,
                "vendorChunk": false,
                "buildOptimizer": true,
                "ngswConfigPath": "src/app/config/ngsw-config.json",
                "budgets": [
                  {
                    "type": "initial",
                    "maximumWarning": "2mb",
                    "maximumError": "5mb"
                  },
                  {
                    "type": "anyComponentStyle",
                    "maximumWarning": "6kb"
                  }
                ],
                "serviceWorker": true
              }
            }
          },
          "serve": {
            "builder": "@angular-devkit/build-angular:dev-server",
            "options": {
              "browserTarget": "my-app:build"
            },
            "configurations": {
              "production": {
                "browserTarget": "my-app:build:production"
              }
            }
          },
          "extract-i18n": {
            "builder": "@angular-devkit/build-angular:extract-i18n",
            "options": {
              "browserTarget": "my-app:build"
            }
          },
          "test": {
            "builder": "@angular-devkit/build-angular:karma",
            "options": {
              "main": "src/test.ts",
              "polyfills": "src/polyfills.ts",
              "tsConfig": "src/tsconfig.spec.json",
              "karmaConfig": "src/karma.conf.js",
              "styles": ["src/styles.scss"],
              "scripts": [],
              "assets": ["src/favicon.ico", "src/assets"]
            }
          },
          "lint": {
            "builder": "@angular-devkit/build-angular:tslint",
            "options": {
              "tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"],
              "exclude": ["**/node_modules/**"]
            }
          },
          "server": {
            "builder": "@angular-devkit/build-angular:server",
            "options": {
              "outputPath": "dist-server",
              "main": "server.ts",
              "tsConfig": "src/tsconfig.server.json",
              "stylePreprocessorOptions": {
                "includePaths": ["src/assets/sass"]
              }
            },
            "configurations": {
              "dev": {
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                  }
                ],
                "optimization": true
              },
              "production": {
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                  }
                ],
                "optimization": true
              }
            }
          },
          "serve-ssr": {
            "builder": "@nguniversal/builders:ssr-dev-server",
            "options": {
              "browserTarget": "my-app:build",
              "serverTarget": "my-app:server"
            },
            "configurations": {
              "production": {
                "browserTarget": "my-app:build:production",
                "serverTarget": "my-app:server:production"
              }
            }
          },
          "prerender": {
            "builder": "@nguniversal/builders:prerender",
            "options": {
              "browserTarget": "my-app:build:production",
              "serverTarget": "my-app:server:production",
              "routes": ["/"]
            },
            "configurations": {
              "production": {}
            }
          }
        }
      },
      "my-app-e2e": {
        "root": "e2e/",
        "projectType": "application",
        "prefix": "",
        "architect": {
          "e2e": {
            "builder": "@angular-devkit/build-angular:protractor",
            "options": {
              "protractorConfig": "e2e/protractor.conf.js",
              "devServerTarget": "my-app:serve"
            },
            "configurations": {
              "production": {
                "devServerTarget": "my-app:serve:production"
              }
            }
          },
          "lint": {
            "builder": "@angular-devkit/build-angular:tslint",
            "options": {
              "tsConfig": "e2e/tsconfig.e2e.json",
              "exclude": ["**/node_modules/**"]
            }
          }
        }
      }
    },
    "defaultProject": "my-app"
  }
  </code></pre>
  
  <p>Thanks</p>