Skip to content

Commit 41d3dcc

Browse files
fix: add server static asset cache headers
1 parent ca0f48f commit 41d3dcc

File tree

6 files changed

+165
-25
lines changed

6 files changed

+165
-25
lines changed

package-lock.json

Lines changed: 95 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"jest-zone-patch": "0.0.8",
104104
"js-cookie": "^2.2.0",
105105
"lru-cache": "^4.1.3",
106+
"ms": "^2.1.1",
106107
"ng2-fused": "^0.5.1",
107108
"node-sass": "^4.9.2",
108109
"npm": "^6.2.0",

src/generators/angular-core.gen.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
appComponentCssTemplate,
77
appComponentHtmlTemplate,
88
appIndex,
9-
favicon
9+
favicon,
10+
ngsw
1011
} from '../templates/core/app'
1112
import { writeFile_, mkDirAndContinueIfExists_ } from '../utilities/rx-fs'
1213
import { forkJoin } from 'rxjs'
@@ -63,21 +64,10 @@ export function generateCoreAngularApp(projectDir: string, universal = true) {
6364
writeFile_(`${baseDir}/app.component.scss`, appComponentCssTemplate), // TODO: write component generator function instead
6465
writeFile_(`${baseDir}/app.component.html`, appComponentHtmlTemplate),
6566
writeFile_(`${baseDir}/index.pug`, appIndex),
66-
writeFile_(`${baseDir}/favicon.svg`, favicon)
67+
writeFile_(`${baseDir}/favicon.svg`, favicon),
68+
writeFile_(`${baseDir}/ngsw.json`, ngsw)
6769
])
6870
)
69-
// flatMap(() =>
70-
// mkDirAndContinueIfExists_(resolve(baseDir, 'home')).pipe(
71-
// flatMap(() =>
72-
// forkJoin([
73-
// // writeFile_(
74-
// // `${baseDir}/home/home.component.ts`,
75-
// // homeComponentTemplate
76-
// // )
77-
// ])
78-
// )
79-
// )
80-
// )
8171
)
8272
}
8373

src/templates/core/app/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as appComponentHtmlTemplate from './app.component.html.txt'
77
import * as homeComponentTemplate from './home.component.ts.txt'
88
import * as appIndex from './index.pug.txt'
99
import * as favicon from './favicon.svg.txt'
10+
import * as ngsw from './ngsw.json.txt'
1011

1112
export {
1213
appModuleTemplate,
@@ -17,5 +18,6 @@ export {
1718
appComponentCssTemplate,
1819
appComponentHtmlTemplate,
1920
appIndex,
20-
favicon
21+
favicon,
22+
ngsw
2123
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"index": "/index.html",
3+
"assetGroups": [
4+
{
5+
"name": "app",
6+
"installMode": "prefetch",
7+
"resources": {
8+
"files": ["/index.html", "/ngsw-worker.js", "/js/**/**", "!/js/**/*.(br|gzip)"]
9+
}
10+
},
11+
{
12+
"name": "assets",
13+
"installMode": "lazy",
14+
"updateMode": "lazy",
15+
"resources": {
16+
"files": ["/assets/**/**"]
17+
}
18+
},
19+
{
20+
"name": "fonts",
21+
"resources": {
22+
"urls": [
23+
"https://fonts.googleapis.com/**",
24+
"https://fonts.gstatic.com/**"
25+
]
26+
}
27+
}
28+
]
29+
}

src/templates/core/server/server.app.ts.txt

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import * as express from 'express'
22
import * as cookieParser from 'cookie-parser'
3+
import * as lru from 'lru-cache'
34
import { resolve } from 'path'
45
import { ngExpressEngine } from '@nguniversal/express-engine'
56
import { AppServerModule } from './server.angular.module'
67
import { stat, createReadStream } from 'fs'
78
import { LRU_CACHE } from 'fusing-angular-cli/.build/modules/src/modules/firebase'
8-
import * as lru from 'lru-cache'
9+
const ms = require('ms')
910

1011
const environment = JSON.parse(process.env.FUSING_ANGULAR || '{}')
1112
const isLocalDevelopmentServer = environment.ENV === 'dev'
@@ -25,6 +26,28 @@ const publicDir = `${dir}/public`
2526

2627
require('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+
2851
expressApp.use(cookieParser())
2952

3053
expressApp.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

81107
expressApp.get('/js/**', (req, res) => {
82108
const encodings = (req.get('Accept-Encoding') || '').split(',').map(a => a.trim())

0 commit comments

Comments
 (0)