Skip to content

Commit

Permalink
fix(api): add Cache-Control header on dynamic page to prevent caching…
Browse files Browse the repository at this point in the history
… by services like Cloudflare. (#389)

* Disable caching for dynamic page.

* Add test for Cache-Control header.
  • Loading branch information
valankar authored Sep 6, 2024
1 parent 0128fb8 commit c7e9f06
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/http/routes/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (s *ServeStrategy) ServeDynamic(c *gin.Context) {
}
writer.Flush()

c.Header("Cache-Control", "no-cache")
c.Header("Content-Type", "text/html")
c.Header("Content-Length", strconv.Itoa(buf.Len()))
c.Writer.Write(buf.Bytes())
Expand Down
20 changes: 19 additions & 1 deletion app/http/routes/strategies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ func TestServeStrategy_ServeDynamic(t *testing.T) {
expectedHeaderKey: "X-Sablier-Session-Status",
expectedHeaderValue: "not-ready",
},
{
name: "header requests no caching",
arg: arg{
body: models.DynamicRequest{
Names: []string{"nginx"},
DisplayName: "Test",
Theme: "hacker-terminal",
SessionDuration: 1 * time.Minute,
},
session: sessions.SessionState{
Instances: createMap([]*instance.State{
{Name: "nginx", Status: instance.NotReady},
}),
},
},
expectedHeaderKey: "Cache-Control",
expectedHeaderValue: "no-cache",
},
{
name: "header has ready value when session is ready",
arg: arg{
Expand Down Expand Up @@ -104,7 +122,7 @@ func TestServeStrategy_ServeDynamic(t *testing.T) {
SessionState: tt.arg.session,
},
StrategyConfig: config.NewStrategyConfig(),
Theme: theme,
Theme: theme,
}
recorder := httptest.NewRecorder()
c := GetTestGinContext(recorder)
Expand Down

0 comments on commit c7e9f06

Please sign in to comment.