Skip to content

Commit e6cc650

Browse files
committed
fix: make non-existent asset paths return a 404
Before, if a file was not found we would serve the app.
1 parent f24cb5c commit e6cc650

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

site/site.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
205205
case reqFile == "bin" || strings.HasPrefix(reqFile, "bin/"):
206206
h.handler.ServeHTTP(rw, r)
207207
return
208+
// If requesting assets, serve straight up with caching.
209+
case reqFile == "assets" || strings.HasPrefix(reqFile, "assets/"):
210+
// It could make sense to cache 404s, but the problem is that during an
211+
// upgrade a load balance may route partially to the old server, and that
212+
// would make new asset paths get cached as 404s and not load even once the
213+
// new server was in place. To combat that, only cache if we have the file.
214+
if h.exists(reqFile) && ShouldCacheFile(reqFile) {
215+
rw.Header().Add("Cache-Control", "public, max-age=31536000, immutable")
216+
}
217+
// If the asset does not exist, this will return a 404.
218+
h.handler.ServeHTTP(rw, r)
219+
return
208220
// If the original file path exists we serve it.
209221
case h.exists(reqFile):
210222
if ShouldCacheFile(reqFile) {

0 commit comments

Comments
 (0)