11import * as express from 'express'
22import * as cookieParser from 'cookie-parser'
3+ import * as lru from 'lru-cache'
34import { resolve } from 'path'
45import { ngExpressEngine } from '@nguniversal/express-engine'
56import { AppServerModule } from './server.angular.module'
67import { stat, createReadStream } from 'fs'
78import { LRU_CACHE } from 'fusing-angular-cli/.build/modules/src/modules/firebase'
8- import * as lru from 'lru-cache'
9+ const ms = require('ms')
910
1011const environment = JSON.parse(process.env.FUSING_ANGULAR || '{}')
1112const isLocalDevelopmentServer = environment.ENV === 'dev'
@@ -25,6 +26,28 @@ const publicDir = `${dir}/public`
2526
2627require('reload')(expressApp)
2728
29+ function seconds(time: string) {
30+ return ms(time) / 1000
31+ }
32+
33+ function staticCacheOptionsGen(time: string, disableCacheForLocalDev = true) {
34+ return {
35+ index: false,
36+ setHeaders: (res: express.Response) => {
37+ res.setHeader(
38+ 'Expires',
39+ disableCacheForLocalDev
40+ ? new Date(Date.now() + seconds(time)).toUTCString()
41+ : new Date(Date.now()).toUTCString()
42+ )
43+ res.setHeader(
44+ 'Cache-Control',
45+ disableCacheForLocalDev ? 'max-age=0' : `max-age=${seconds(time)}`
46+ )
47+ }
48+ }
49+ }
50+
2851expressApp.use(cookieParser())
2952
3053expressApp.set('x-powered-by', false)
@@ -53,7 +76,10 @@ function writeJsHeaders(res: express.Response, contentLength: number, type: stri
5376 res.writeHead(200, {
5477 "Content-Type": "application/javascript",
5578 "Content-Encoding": type,
56- "Content-Length": contentLength
79+ "Content-Length": contentLength,
80+ "Cache-Control": isLocalDevelopmentServer
81+ ? "public, no-cache"
82+ : `public, max-age=${seconds('180d')}, s-maxage=${seconds('180d')}`
5783 })
5884}
5985
@@ -72,11 +98,11 @@ function checkReturnJsFile(filePath: string, res: express.Response, encoding: st
7298 })
7399}
74100
75- expressApp.use('/robots.txt', express.static(`${publicDir}/assets/robots.txt`))
76- expressApp.use('/assets', express.static(`${publicDir}/assets`))
77- expressApp.use('/favicon.ico', express.static(`${publicDir}/assets/favicons/favicon.ico`))
78- expressApp.use('/manifest.json', express.static(`${publicDir}/assets/favicons/manifest.json`))
79- expressApp.use('/js/ngsw.json', express.static(`${publicDir}/ngsw.json`))
101+ expressApp.use('/robots.txt', express.static(`${publicDir}/assets/robots.txt`, staticCacheOptionsGen('30d') ))
102+ expressApp.use('/assets', express.static(`${publicDir}/assets`, staticCacheOptionsGen('30d') ))
103+ expressApp.use('/favicon.ico', express.static(`${publicDir}/assets/favicons/favicon.ico`, staticCacheOptionsGen('30d') ))
104+ expressApp.use('/manifest.json', express.static(`${publicDir}/assets/favicons/manifest.json`, staticCacheOptionsGen('30d') ))
105+ expressApp.use('/js/ngsw.json', express.static(`${publicDir}/ngsw.json`, staticCacheOptionsGen('30d') ))
80106
81107expressApp.get('/js/**', (req, res) => {
82108 const encodings = (req.get('Accept-Encoding') || '').split(',').map(a => a.trim())
0 commit comments