4c6ba31821c7343fcd04c5633126e9c3d0e5c3fb

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Cache-bust audio URLs with a per-round nonce

Browsers cache media resources aggressively, so a new <audio> element
reusing src=/audio served the previous round's clip even with
Cache-Control: no-store. Add a monotonically increasing nonce to Game and
append it as ?n=<nonce> to both /audio and /audio/long, giving every clip a
unique URL that forces a fresh fetch each round.

Diff

 1diff --git a/server.go b/server.go
 2index 75cb30afa2cf3142ffe561eb212beb470d5aec2c..4994467efbd674ccc0509eb5bb2dc960bf6f1e63 100644
 3--- a/server.go
 4+++ b/server.go
 5@@ -26,6 +26,7 @@ type Game struct {
 6 	pos             int
 7 	rounds          int
 8 	played          int
 9+	nonce           int
10 	score           int
11 	answer          Song
12 	startSec        int
13@@ -61,6 +62,7 @@ func newGame(catalog []Song, rounds int) *Game {
14 // generation is wired in by a later stage.
15 func (g *Game) beginRound() {
16 	g.answer = g.nextSong()
17+	g.nonce++
18 	g.hinted = false
19 	g.result = nil
20 	g.startSec = -1
21@@ -169,6 +171,7 @@ type viewData struct {
22 	Round       int
23 	TotalRounds int
24 	Dots        []bool
25+	Nonce       int
26 	Answer      Song
27 	Hinted      bool
28 	Done        bool
29@@ -190,6 +193,7 @@ func (g *Game) viewData() viewData {
30 		Round:       round,
31 		TotalRounds: g.rounds,
32 		Dots:        dots,
33+		Nonce:       g.nonce,
34 		Answer:      g.answer,
35 		Hinted:      g.hinted,
36 		Done:        g.done,
37diff --git a/web/templates/layout.html b/web/templates/layout.html
38index b48a6a415d8d7ccee809484f45e7d66b30009e30..f7c0e5a0312fff8bd49ddcf86d483ce7dd024d98 100644
39--- a/web/templates/layout.html
40+++ b/web/templates/layout.html
41@@ -19,7 +19,7 @@
42       var a = document.getElementById('clip');
43       if (!a) return;
44       a.pause();
45-      a.src = '/audio/long';
46+      a.src = '/audio/long?n=' + window.clipNonce;
47       a.load();
48       a.play().catch(function () {});
49     }
50diff --git a/web/templates/playing.html b/web/templates/playing.html
51index 8c417cfc07851f8bc33bc32831e0544c33b0835b..ed454107ddae63947ffc8fbed328acfb4ec84cc5 100644
52--- a/web/templates/playing.html
53+++ b/web/templates/playing.html
54@@ -2,9 +2,10 @@
55 <div class="now-playing">
56   <span class="eq"><i></i><i></i><i></i></span> Now playing
57 </div>
58-<audio id="clip" src="/audio" autoplay></audio>
59+<audio id="clip" src="/audio?n={{.Nonce}}" autoplay></audio>
60 <script>
61   (function () {
62+    window.clipNonce = {{.Nonce}};
63     var a = document.getElementById('clip');
64     if (a) { a.play().catch(function () {}); }
65     var t = document.getElementById('g-title');