Open platform to collect and prioritize feedback
fix: do not cache 404 assets
goenning832d74284b64
diff --git a/app/middlewares/cache_test.go b/app/middlewares/cache_test.goindex cb1811e13..a5fa7af8b 100644--- a/app/middlewares/cache_test.go+++ b/app/middlewares/cache_test.go@@ -11,7 +11,7 @@ import ("github.com/getfider/fider/app/pkg/web")-func TestCache(t *testing.T) {+func TestCache_StatusOK(t *testing.T) {RegisterT(t)server := mock.NewServer()@@ -25,3 +25,18 @@ func TestCache(t *testing.T) {Expect(status).Equals(http.StatusOK)Expect(response.Header().Get("Cache-Control")).Equals("public, max-age=300")}++func TestCache_StatusNotFound(t *testing.T) {+ RegisterT(t)++ server := mock.NewServer()+ server.Use(middlewares.ClientCache(5 * time.Minute))+ handler := func(c *web.Context) error {+ return c.NotFound()+ }++ status, response := server.Execute(handler)++ Expect(status).Equals(http.StatusNotFound)+ Expect(response.Header().Get("Cache-Control")).Equals("")+}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex ee0211807..d52f4914a 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -476,7 +476,11 @@ func (c *Context) Image(contentType string, b []byte) error {// Blob sends a blob response with status code and content type.func (c *Context) Blob(code int, contentType string, b []byte) error {+ if code >= 400 {+ c.Response.Header().Del("Cache-Control")+ }c.Response.Header().Set("Content-Type", contentType)+c.ResponseStatusCode = codec.Response.WriteHeader(code)_, err := c.Response.Write(b)@@ -485,6 +489,10 @@ func (c *Context) Blob(code int, contentType string, b []byte) error {// NoContent sends a response with no body and a status code.func (c *Context) NoContent(code int) error {+ if code >= 400 {+ c.Response.Header().Del("Cache-Control")+ }+c.ResponseStatusCode = codec.Response.WriteHeader(code)return nildiff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.goindex e9bc38bba..bdc6ada49 100644--- a/app/pkg/web/engine.go+++ b/app/pkg/web/engine.go@@ -296,6 +296,7 @@ func (g *Group) Static(prefix, root string) {http.ServeFile(c.Response, c.Request.instance, filePath)return nil}+ c.Response.Header().Del("Cache-Control")return c.NotFound()}} else {diff --git a/e2e/_init_.ts b/e2e/_init_.tsindex 4ade12053..e4f24c48d 100644--- a/e2e/_init_.ts+++ b/e2e/_init_.ts@@ -1,6 +1,8 @@/* eslint-disable @typescript-eslint/no-var-requires */require("isomorphic-fetch")+process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"+require("ts-node").register({transpileOnly: true,compilerOptions: {diff --git a/e2e/features/http.feature b/e2e/features/http.featurenew file mode 100644index 000000000..c7d2b8889--- /dev/null+++ b/e2e/features/http.feature@@ -0,0 +1,11 @@+Feature: HTTP++ Scenario: Cache Control is set on valid resources+ Given I send a "GET" request to "/assets/assets.json"+ Then I should see http status 200+ And I should see a "Cache-Control" header with value "public, max-age=31536000"++ Scenario: Cache Control is not set on invalid resources+ Given I send a "GET" request to "/assets/invalid.js"+ Then I should see http status 404+ And I should not see a "Cache-Control" header\ No newline at end of filediff --git a/e2e/step_definitions/http.steps.ts b/e2e/step_definitions/http.steps.tsnew file mode 100644index 000000000..c5a344393--- /dev/null+++ b/e2e/step_definitions/http.steps.ts@@ -0,0 +1,20 @@+import { Given, Then } from "@cucumber/cucumber"+import expect from "expect"++let response: Response++Given("I send a {string} request to {string}", async function (method: string, path: string) {+ response = await fetch(`https://login.dev.fider.io:3000${path}`, { method })+})++Then("I should see http status {int}", async function (code: number) {+ expect(response.status).toEqual(code)+})++Then("I should not see a {string} header", async function (headerName: string) {+ expect(response.headers.get(headerName)).toBeNull()+})++Then("I should see a {string} header with value {string}", async function (headerName: string, headerValue: string) {+ expect(response.headers.get(headerName)).toBe(headerValue)+})diff --git a/package-lock.json b/package-lock.jsonindex 7bd0a8897..dd04374cf 100644--- a/package-lock.json+++ b/package-lock.json@@ -19593,6 +19593,7 @@"@babel/types": "^7.11.5","@lingui/babel-plugin-extract-messages": "^3.10.2","@lingui/conf": "^3.10.2",+ "babel-plugin-macros": "^3.0.1","bcp-47": "^1.0.7","chalk": "^4.1.0","chokidar": "3.5.1",@@ -27748,6 +27749,7 @@"dev": true,"requires": {"purgecss": "^4.0.3",+ "webpack": "^5.4.0","webpack-sources": "^2.0.0"}},