@@ -8,6 +8,7 @@ import { match as reachMatch } from "@gatsbyjs/reach-router"
8
8
import onExit from "signal-exit"
9
9
import report from "gatsby-cli/lib/reporter"
10
10
import telemetry from "gatsby-telemetry"
11
+ import { isMatch , Options as MicroMatchOptions } from "micromatch"
11
12
12
13
import { detectPortInUseAndPrompt } from "../utils/detect-port-in-use-and-prompt"
13
14
import { getConfigFile } from "../bootstrap/get-config-file"
@@ -98,6 +99,36 @@ const matchPathRouter =
98
99
return next ( )
99
100
}
100
101
102
+ const setCacheHeaders = (
103
+ req : express . Request ,
104
+ res : express . Response ,
105
+ next : express . NextFunction
106
+ ) : void => {
107
+ function match (
108
+ pattern : string | ReadonlyArray < string > ,
109
+ options ?: MicroMatchOptions
110
+ ) : boolean {
111
+ return isMatch ( req . path , pattern , options )
112
+ }
113
+
114
+ if ( req . method !== `GET` ) {
115
+ next ( )
116
+ return
117
+ }
118
+
119
+ if ( ( match ( `/static/**` ) || match ( `/**.+(js|css)` ) ) && ! match ( `/sw.js` ) ) {
120
+ res . header ( `Cache-control` , `public, max-age=31536000, immutable` )
121
+
122
+ next ( )
123
+ return
124
+ }
125
+
126
+ res . header ( `Cache-control` , `public, max-age=0, must-revalidate` )
127
+
128
+ next ( )
129
+ return
130
+ }
131
+
101
132
module . exports = async ( program : IServeProgram ) : Promise < void > => {
102
133
telemetry . trackCli ( `SERVE_START` )
103
134
telemetry . startBackgroundUpdate ( )
@@ -132,6 +163,7 @@ module.exports = async (program: IServeProgram): Promise<void> => {
132
163
app . use ( telemetry . expressMiddleware ( `SERVE` ) )
133
164
134
165
router . use ( compression ( ) )
166
+ app . use ( setCacheHeaders )
135
167
136
168
router . use (
137
169
configureTrailingSlash (
0 commit comments