t1256: cache-bust JS/CSS and never show a raw i18n key
Stamp content-hash query strings on game.js, style.css and framework.js at build time, send Cache-Control no-cache for HTML, and fall back to English or the existing label so a stale catalog cannot print the key. The leaderboard button also listens for pointerup on touch, like Play.
@@ -64,6 +64,13 @@ php -S 127.0.0.1:8080 -t build build/router.php
64
64
# spel: http://127.0.0.1:8080/
65
65
```
66
66
67
`./src/build.sh` zet op `game.js`, `style.css` en `framework.js` een
68
content-hash (`?v=12 hex tekens`) in `build/index.html`. De vertalingen
69
zitten in `game.js` (`I18N_CATALOG`, uit `server/lib/i18n.json`), dus
70
verandert de catalogus, dan verandert ook die hash. `index.html` zelf
71
wordt met `Cache-Control: no-cache` aangeboden (via `.htaccess` en de
72
lokale router), zodat een browser altijd de nieuwe hashes ziet.
73
67
74
## Architectuur
68
75
69
76
De spelkern is deterministisch en staat los van tekenen en invoer, zodat stage 2
@@ -87,7 +94,7 @@ De spelkern is deterministisch en staat los van tekenen en invoer, zodat stage 2
87
94
| `server/` | PHP API: OAuth, lobby, shots, SQLite in `data/` (niet gedeployed) |
88
95
| `server/lib/planets.json` | Catalogus van 12 planeten (JS en PHP delen dit bestand) |
89
96
| `docs/ASSUMPTIONS.md` | Ingevulde aannames waar de specificatie ruimte liet |
90
| `src/build.sh` | Concateneert de bronnen naar `build/game.js` en kopieert de PHP-API naar `build/` |
97
| `src/build.sh` | Concateneert de bronnen naar `build/game.js`, kopieert de PHP-API naar `build/` en zet content-hash-querystrings (`?v=`) op `game.js`, `style.css` en `framework.js` in `index.html` (t1256) |
91
98
| `build/` | Speelbare output: `index.html`, `style.css`, `framework.js`, `game.js`, `images/`, PHP-API |
92
99
| `test/` | Unit tests (`node --test`) voor de kern en de deploy-guard; `server/tests/run.php` voor de API |
93
100
| `ci/` | FTP-deploy, **zonder** `--delete`. Expandeert `$timftproot` zelf. Raakt `favicon.ico` en `data/` (config, database) niet aan. |
@@ -64,6 +64,13 @@ php -S 127.0.0.1:8080 -t build build/router.php64
# spel: http://127.0.0.1:8080/64
# spel: http://127.0.0.1:8080/65
```65
```66
66
67
`./src/build.sh` zet op `game.js`, `style.css` en `framework.js` een68
content-hash (`?v=12 hex tekens`) in `build/index.html`. De vertalingen69
zitten in `game.js` (`I18N_CATALOG`, uit `server/lib/i18n.json`), dus70
verandert de catalogus, dan verandert ook die hash. `index.html` zelf71
wordt met `Cache-Control: no-cache` aangeboden (via `.htaccess` en de72
lokale router), zodat een browser altijd de nieuwe hashes ziet.73
67
## Architectuur74
## Architectuur68
75
69
De spelkern is deterministisch en staat los van tekenen en invoer, zodat stage 276
De spelkern is deterministisch en staat los van tekenen en invoer, zodat stage 2@@ -87,7 +94,7 @@ De spelkern is deterministisch en staat los van tekenen en invoer, zodat stage 287
| `server/` | PHP API: OAuth, lobby, shots, SQLite in `data/` (niet gedeployed) |94
| `server/` | PHP API: OAuth, lobby, shots, SQLite in `data/` (niet gedeployed) |88
| `server/lib/planets.json` | Catalogus van 12 planeten (JS en PHP delen dit bestand) |95
| `server/lib/planets.json` | Catalogus van 12 planeten (JS en PHP delen dit bestand) |89
| `docs/ASSUMPTIONS.md` | Ingevulde aannames waar de specificatie ruimte liet |96
| `docs/ASSUMPTIONS.md` | Ingevulde aannames waar de specificatie ruimte liet |90
| `src/build.sh` | Concateneert de bronnen naar `build/game.js` en kopieert de PHP-API naar `build/` |97
| `src/build.sh` | Concateneert de bronnen naar `build/game.js`, kopieert de PHP-API naar `build/` en zet content-hash-querystrings (`?v=`) op `game.js`, `style.css` en `framework.js` in `index.html` (t1256) |91
| `build/` | Speelbare output: `index.html`, `style.css`, `framework.js`, `game.js`, `images/`, PHP-API |98
| `build/` | Speelbare output: `index.html`, `style.css`, `framework.js`, `game.js`, `images/`, PHP-API |92
| `test/` | Unit tests (`node --test`) voor de kern en de deploy-guard; `server/tests/run.php` voor de API |99
| `test/` | Unit tests (`node --test`) voor de kern en de deploy-guard; `server/tests/run.php` voor de API |93
| `ci/` | FTP-deploy, **zonder** `--delete`. Expandeert `$timftproot` zelf. Raakt `favicon.ico` en `data/` (config, database) niet aan. |100
| `ci/` | FTP-deploy, **zonder** `--delete`. Expandeert `$timftproot` zelf. Raakt `favicon.ico` en `data/` (config, database) niet aan. |@@ -4419,7 +4419,9 @@ I18n.apply = function (root)
4419
4419
var key = el.getAttribute('data-i18n');
4420
4420
if (!key)
4421
4421
{ continue; }
4422
el.textContent = this.t(key);
4422
// Fall back to English (inside t) then the existing label so a
4423
// missing/stale key never replaces visible text with the raw key.
4424
el.textContent = this.t(key, null, el.textContent);
4423
4425
}
4424
4426
nodes = root.querySelectorAll('[data-i18n-html]');
4425
4427
for (var i = 0; i < nodes.length; i++)
@@ -4428,22 +4430,25 @@ I18n.apply = function (root)
4428
4430
var key = el.getAttribute('data-i18n-html');
4429
4431
if (!key)
4430
4432
{ continue; }
4431
el.innerHTML = this.t(key);
4433
el.innerHTML = this.t(key, null, el.innerHTML);
4432
4434
}
4433
4435
nodes = root.querySelectorAll('[data-i18n-placeholder]');
4434
4436
for (var i = 0; i < nodes.length; i++)
4435
4437
{
4436
nodes[i].setAttribute('placeholder', this.t(nodes[i].getAttribute('data-i18n-placeholder')));
4438
var el = nodes[i];
4439
el.setAttribute('placeholder', this.t(el.getAttribute('data-i18n-placeholder'), null, el.getAttribute('placeholder')));
4437
4440
}
4438
4441
nodes = root.querySelectorAll('[data-i18n-aria]');
4439
4442
for (var i = 0; i < nodes.length; i++)
4440
4443
{
4441
nodes[i].setAttribute('aria-label', this.t(nodes[i].getAttribute('data-i18n-aria')));
4444
var el = nodes[i];
4445
el.setAttribute('aria-label', this.t(el.getAttribute('data-i18n-aria'), null, el.getAttribute('aria-label')));
4442
4446
}
4443
4447
nodes = root.querySelectorAll('[data-i18n-title]');
4444
4448
for (var i = 0; i < nodes.length; i++)
4445
4449
{
4446
nodes[i].setAttribute('title', this.t(nodes[i].getAttribute('data-i18n-title')));
4450
var el = nodes[i];
4451
el.setAttribute('title', this.t(el.getAttribute('data-i18n-title'), null, el.getAttribute('title')));
4447
4452
}
4448
4453
};
4449
4454
@@ -16730,7 +16735,20 @@ function bindLeaderboard ()
16730
16735
{
16731
16736
var btn = document.getElementById('btn-leaderboard');
16732
16737
if (btn)
16733
{ btn.addEventListener('click', function () { openLeaderboard(); }); }
16738
{
16739
var open = function () { openLeaderboard(); };
16740
btn.addEventListener('click', open);
16741
// Same mobile path as Play / Survival: some browsers swallow click
16742
// after a slightly sloppy tap.
16743
btn.addEventListener('pointerup', function (e)
16744
{
16745
if (e.pointerType === 'touch' || e.pointerType === 'pen')
16746
{
16747
e.preventDefault();
16748
open();
16749
}
16750
});
16751
}
16734
16752
var back = document.getElementById('btn-lb-back');
16735
16753
if (back)
16736
16754
{ back.addEventListener('click', closeLeaderboard); }
@@ -16744,6 +16762,8 @@ function bindLeaderboard ()
16744
16762
16745
16763
function openLeaderboard (tab)
16746
16764
{
16765
if (app.mode === 'leaderboard')
16766
{ return; }
16747
16767
hideEl('menu');
16748
16768
hideEl('online-panel');
16749
16769
hideEl('planet-select');
@@ -4419,7 +4419,9 @@ I18n.apply = function (root)4419
var key = el.getAttribute('data-i18n');4419
var key = el.getAttribute('data-i18n');4420
if (!key)4420
if (!key)4421
{ continue; }4421
{ continue; }4422
el.textContent = this.t(key);4422
// Fall back to English (inside t) then the existing label so a4423
// missing/stale key never replaces visible text with the raw key.4424
el.textContent = this.t(key, null, el.textContent);4423
}4425
}4424
nodes = root.querySelectorAll('[data-i18n-html]');4426
nodes = root.querySelectorAll('[data-i18n-html]');4425
for (var i = 0; i < nodes.length; i++)4427
for (var i = 0; i < nodes.length; i++)@@ -4428,22 +4430,25 @@ I18n.apply = function (root)4428
var key = el.getAttribute('data-i18n-html');4430
var key = el.getAttribute('data-i18n-html');4429
if (!key)4431
if (!key)4430
{ continue; }4432
{ continue; }4431
el.innerHTML = this.t(key);4433
el.innerHTML = this.t(key, null, el.innerHTML);4432
}4434
}4433
nodes = root.querySelectorAll('[data-i18n-placeholder]');4435
nodes = root.querySelectorAll('[data-i18n-placeholder]');4434
for (var i = 0; i < nodes.length; i++)4436
for (var i = 0; i < nodes.length; i++)4435
{4437
{4436
nodes[i].setAttribute('placeholder', this.t(nodes[i].getAttribute('data-i18n-placeholder')));4438
var el = nodes[i];4439
el.setAttribute('placeholder', this.t(el.getAttribute('data-i18n-placeholder'), null, el.getAttribute('placeholder')));4437
}4440
}4438
nodes = root.querySelectorAll('[data-i18n-aria]');4441
nodes = root.querySelectorAll('[data-i18n-aria]');4439
for (var i = 0; i < nodes.length; i++)4442
for (var i = 0; i < nodes.length; i++)4440
{4443
{4441
nodes[i].setAttribute('aria-label', this.t(nodes[i].getAttribute('data-i18n-aria')));4444
var el = nodes[i];4445
el.setAttribute('aria-label', this.t(el.getAttribute('data-i18n-aria'), null, el.getAttribute('aria-label')));4442
}4446
}4443
nodes = root.querySelectorAll('[data-i18n-title]');4447
nodes = root.querySelectorAll('[data-i18n-title]');4444
for (var i = 0; i < nodes.length; i++)4448
for (var i = 0; i < nodes.length; i++)4445
{4449
{4446
nodes[i].setAttribute('title', this.t(nodes[i].getAttribute('data-i18n-title')));4450
var el = nodes[i];4451
el.setAttribute('title', this.t(el.getAttribute('data-i18n-title'), null, el.getAttribute('title')));4447
}4452
}4448
};4453
};4449
4454
@@ -16730,7 +16735,20 @@ function bindLeaderboard ()16730
{16735
{16731
var btn = document.getElementById('btn-leaderboard');16736
var btn = document.getElementById('btn-leaderboard');16732
if (btn)16737
if (btn)16733
{ btn.addEventListener('click', function () { openLeaderboard(); }); }16738
{16739
var open = function () { openLeaderboard(); };16740
btn.addEventListener('click', open);16741
// Same mobile path as Play / Survival: some browsers swallow click16742
// after a slightly sloppy tap.16743
btn.addEventListener('pointerup', function (e)16744
{16745
if (e.pointerType === 'touch' || e.pointerType === 'pen')16746
{16747
e.preventDefault();16748
open();16749
}16750
});16751
}16734
var back = document.getElementById('btn-lb-back');16752
var back = document.getElementById('btn-lb-back');16735
if (back)16753
if (back)16736
{ back.addEventListener('click', closeLeaderboard); }16754
{ back.addEventListener('click', closeLeaderboard); }@@ -16744,6 +16762,8 @@ function bindLeaderboard ()16744
16762
16745
function openLeaderboard (tab)16763
function openLeaderboard (tab)16746
{16764
{16765
if (app.mode === 'leaderboard')16766
{ return; }16747
hideEl('menu');16767
hideEl('menu');16748
hideEl('online-panel');16768
hideEl('online-panel');16749
hideEl('planet-select');16769
hideEl('planet-select');@@ -8,7 +8,7 @@
8
8
<meta name="theme-color" content="#0b1020">
9
9
<link rel="icon" href="favicon.ico" sizes="any">
10
10
<title>Meteors</title>
11
<link rel="stylesheet" href="style.css">
11
<link rel="stylesheet" href="style.css?v=47c6179f10ef">
12
12
</head>
13
13
<body>
14
14
<div id="stars" aria-hidden="true"></div>
@@ -263,7 +263,7 @@
263
263
<p id="reward-hint" class="reward-hint" hidden data-i18n="reward.continue">Tap to continue</p>
264
264
</div>
265
265
266
<script src="framework.js"></script>
267
<script src="game.js"></script>
266
<script src="framework.js?v=24498ce42730"></script>
267
<script src="game.js?v=5064cd3132cb"></script>
268
268
</body>
269
269
</html>
@@ -8,7 +8,7 @@8
<meta name="theme-color" content="#0b1020">8
<meta name="theme-color" content="#0b1020">9
<link rel="icon" href="favicon.ico" sizes="any">9
<link rel="icon" href="favicon.ico" sizes="any">10
<title>Meteors</title>10
<title>Meteors</title>11
<link rel="stylesheet" href="style.css">11
<link rel="stylesheet" href="style.css?v=47c6179f10ef">12
</head>12
</head>13
<body>13
<body>14
<div id="stars" aria-hidden="true"></div>14
<div id="stars" aria-hidden="true"></div>@@ -263,7 +263,7 @@263
<p id="reward-hint" class="reward-hint" hidden data-i18n="reward.continue">Tap to continue</p>263
<p id="reward-hint" class="reward-hint" hidden data-i18n="reward.continue">Tap to continue</p>264
</div>264
</div>265
265
266
<script src="framework.js"></script>266
<script src="framework.js?v=24498ce42730"></script>267
<script src="game.js"></script>267
<script src="game.js?v=5064cd3132cb"></script>268
</body>268
</body>269
</html>269
</html>@@ -1463,3 +1463,29 @@ van echte namen kunnen voorzien. Dat vereist een wijziging op
1463
1463
auth.timfalken.com; die is hier niet gedaan.
1464
1464
1465
1465
Tests: `server/tests/run.php` (t1249).
1466
1467
## t1256 — Cache-busting voor JS, CSS en vertalingen
1468
1469
Tim (Brave, mobiel, ingelogd, NL) zag op het hoofdmenu de knop
1470
**menu.leaderboard** (de ruwe i18n-sleutel) en een tik deed niets. De
1471
live catalogus had de sleutel wél in alle talen (NL: “Ranglijst”), de
1472
knop staat onder **Online spelen**, en als gast in NL (desktop en
1473
mobiele emulatie) werkt label én tik. De HTML-fallback van de knop is
1474
“Leaderboard”; dat de sleutel zichtbaar was, betekent dat `I18n.apply`
1475
liep tegen een catalogus zonder die sleutel — typisch nieuwe
1476
`index.html` (met de knop en `data-i18n`) naast een oude gecachete
1477
`game.js` (zonder sleutel en zonder `bindLeaderboard`). “Sitegegevens
1478
wissen” in Brave wist niet altijd de HTTP-cache van JS.
1479
1480
**Aanpak.** `src/build.sh` zet een SHA-256-hash (12 hex tekens) als
1481
`?v=` op `game.js`, `style.css` en `framework.js` in `index.html`.
1482
`i18n.json` wordt in `game.js` ingebouwd, dus die hash dekt de
1483
vertalingen. `index.html` krijgt `Cache-Control: no-cache` (`.htaccess`
1484
en `router.php`); JS/CSS/JSON mogen lang gecached worden (`immutable`).
1485
De live host serveert statische bestanden via nginx, dus `.htaccess`
1486
grijpt daar mogelijk niet; de querystring doet het cache-busting dan
1487
alsnog. `I18n.apply` valt terug op Engels en daarna op de bestaande
1488
labeltekst, zodat een ontbrekende sleutel nooit rauw in de UI verschijnt.
1489
De ranglijstknop luistert ook op `pointerup` (zoals Spelen / Overleven).
1490
1491
Tests: `test/cache-bust.test.js`, `test/i18n.test.js` (t1256).
@@ -1463,3 +1463,29 @@ van echte namen kunnen voorzien. Dat vereist een wijziging op1463
auth.timfalken.com; die is hier niet gedaan.1463
auth.timfalken.com; die is hier niet gedaan.1464
1464
1465
Tests: `server/tests/run.php` (t1249).1465
Tests: `server/tests/run.php` (t1249).1466
1467
## t1256 — Cache-busting voor JS, CSS en vertalingen1468
1469
Tim (Brave, mobiel, ingelogd, NL) zag op het hoofdmenu de knop1470
**menu.leaderboard** (de ruwe i18n-sleutel) en een tik deed niets. De1471
live catalogus had de sleutel wél in alle talen (NL: “Ranglijst”), de1472
knop staat onder **Online spelen**, en als gast in NL (desktop en1473
mobiele emulatie) werkt label én tik. De HTML-fallback van de knop is1474
“Leaderboard”; dat de sleutel zichtbaar was, betekent dat `I18n.apply`1475
liep tegen een catalogus zonder die sleutel — typisch nieuwe1476
`index.html` (met de knop en `data-i18n`) naast een oude gecachete1477
`game.js` (zonder sleutel en zonder `bindLeaderboard`). “Sitegegevens1478
wissen” in Brave wist niet altijd de HTTP-cache van JS.1479
1480
**Aanpak.** `src/build.sh` zet een SHA-256-hash (12 hex tekens) als1481
`?v=` op `game.js`, `style.css` en `framework.js` in `index.html`.1482
`i18n.json` wordt in `game.js` ingebouwd, dus die hash dekt de1483
vertalingen. `index.html` krijgt `Cache-Control: no-cache` (`.htaccess`1484
en `router.php`); JS/CSS/JSON mogen lang gecached worden (`immutable`).1485
De live host serveert statische bestanden via nginx, dus `.htaccess`1486
grijpt daar mogelijk niet; de querystring doet het cache-busting dan1487
alsnog. `I18n.apply` valt terug op Engels en daarna op de bestaande1488
labeltekst, zodat een ontbrekende sleutel nooit rauw in de UI verschijnt.1489
De ranglijstknop luistert ook op `pointerup` (zoals Spelen / Overleven).1490
1491
Tests: `test/cache-bust.test.js`, `test/i18n.test.js` (t1256).@@ -16,3 +16,17 @@ DirectoryIndex index.html index.php
16
16
Header set Cache-Control "no-store"
17
17
</FilesMatch>
18
18
</IfModule>
19
20
# t1256: HTML must be revalidated so new asset hashes take effect.
21
# Versioned JS/CSS/JSON (see ?v= from src/build.sh) can be cached long.
22
# The host serves PHP through Apache-style rules; static files may be
23
# served by nginx and then these FilesMatch rules do not apply — the
24
# query-string hashes still bust the cache.
25
<IfModule mod_headers.c>
26
<FilesMatch "\.(html)$">
27
Header set Cache-Control "no-cache"
28
</FilesMatch>
29
<FilesMatch "\.(js|css|json)$">
30
Header set Cache-Control "public, max-age=31536000, immutable"
31
</FilesMatch>
32
</IfModule>
@@ -16,3 +16,17 @@ DirectoryIndex index.html index.php16
Header set Cache-Control "no-store"16
Header set Cache-Control "no-store"17
</FilesMatch>17
</FilesMatch>18
</IfModule>18
</IfModule>19
20
# t1256: HTML must be revalidated so new asset hashes take effect.21
# Versioned JS/CSS/JSON (see ?v= from src/build.sh) can be cached long.22
# The host serves PHP through Apache-style rules; static files may be23
# served by nginx and then these FilesMatch rules do not apply — the24
# query-string hashes still bust the cache.25
<IfModule mod_headers.c>26
<FilesMatch "\.(html)$">27
Header set Cache-Control "no-cache"28
</FilesMatch>29
<FilesMatch "\.(js|css|json)$">30
Header set Cache-Control "public, max-age=31536000, immutable"31
</FilesMatch>32
</IfModule>@@ -14,6 +14,7 @@ if (str_ends_with($uri, '.php') && is_file($file)) {
14
14
// Fall through to index.html for the game UI.
15
15
if (is_file(__DIR__ . '/index.html')) {
16
16
header('Content-Type: text/html; charset=utf-8');
17
header('Cache-Control: no-cache');
17
18
readfile(__DIR__ . '/index.html');
18
19
return true;
19
20
}
@@ -14,6 +14,7 @@ if (str_ends_with($uri, '.php') && is_file($file)) {14
// Fall through to index.html for the game UI.14
// Fall through to index.html for the game UI.15
if (is_file(__DIR__ . '/index.html')) {15
if (is_file(__DIR__ . '/index.html')) {16
header('Content-Type: text/html; charset=utf-8');16
header('Content-Type: text/html; charset=utf-8');17
header('Cache-Control: no-cache');17
readfile(__DIR__ . '/index.html');18
readfile(__DIR__ . '/index.html');18
return true;19
return true;19
}20
}@@ -79,4 +79,24 @@ rm -f "$ROOT/build/data/config.php" "$ROOT/build/data/config.lock"
79
79
rm -f "$ROOT/build/data/"*.sqlite "$ROOT/build/data/"*.sqlite-journal
80
80
rm -rf "$ROOT/build/data/sessions"
81
81
82
# t1256: stamp content-hash query strings on JS/CSS so browsers pick up
83
# a new bundle even when they have an old game.js or style.css cached.
84
# Locales live inside game.js (I18N_CATALOG), so the game.js hash covers
85
# i18n.json as well. Idempotent: an existing ?v=... is replaced.
86
asset_hash() {
87
sha256sum "$1" | cut -c1-12
88
}
89
INDEX="$ROOT/build/index.html"
90
GAME_HASH="$(asset_hash "$ROOT/build/game.js")"
91
CSS_HASH="$(asset_hash "$ROOT/build/style.css")"
92
FW_HASH="$(asset_hash "$ROOT/build/framework.js")"
93
I18N_HASH="$(asset_hash "$ROOT/server/lib/i18n.json")"
94
# i18n.json is inlined into game.js, so a catalog change already bumps GAME_HASH.
95
# I18N_HASH is logged so a deploy can be compared against the catalog file.
96
sed -i -E "s/href=\"style\.css(\?v=[^\"]*)?\"/href=\"style.css?v=${CSS_HASH}\"/" "$INDEX"
97
sed -i -E "s/src=\"framework\.js(\?v=[^\"]*)?\"/src=\"framework.js?v=${FW_HASH}\"/" "$INDEX"
98
sed -i -E "s/src=\"game\.js(\?v=[^\"]*)?\"/src=\"game.js?v=${GAME_HASH}\"/" "$INDEX"
99
printf "Asset hashes: game.js=%s style.css=%s framework.js=%s i18n.json=%s\n" \
100
"$GAME_HASH" "$CSS_HASH" "$FW_HASH" "$I18N_HASH"
101
82
102
printf "DONE\n"
@@ -79,4 +79,24 @@ rm -f "$ROOT/build/data/config.php" "$ROOT/build/data/config.lock"79
rm -f "$ROOT/build/data/"*.sqlite "$ROOT/build/data/"*.sqlite-journal79
rm -f "$ROOT/build/data/"*.sqlite "$ROOT/build/data/"*.sqlite-journal80
rm -rf "$ROOT/build/data/sessions"80
rm -rf "$ROOT/build/data/sessions"81
81
82
# t1256: stamp content-hash query strings on JS/CSS so browsers pick up83
# a new bundle even when they have an old game.js or style.css cached.84
# Locales live inside game.js (I18N_CATALOG), so the game.js hash covers85
# i18n.json as well. Idempotent: an existing ?v=... is replaced.86
asset_hash() {87
sha256sum "$1" | cut -c1-1288
}89
INDEX="$ROOT/build/index.html"90
GAME_HASH="$(asset_hash "$ROOT/build/game.js")"91
CSS_HASH="$(asset_hash "$ROOT/build/style.css")"92
FW_HASH="$(asset_hash "$ROOT/build/framework.js")"93
I18N_HASH="$(asset_hash "$ROOT/server/lib/i18n.json")"94
# i18n.json is inlined into game.js, so a catalog change already bumps GAME_HASH.95
# I18N_HASH is logged so a deploy can be compared against the catalog file.96
sed -i -E "s/href=\"style\.css(\?v=[^\"]*)?\"/href=\"style.css?v=${CSS_HASH}\"/" "$INDEX"97
sed -i -E "s/src=\"framework\.js(\?v=[^\"]*)?\"/src=\"framework.js?v=${FW_HASH}\"/" "$INDEX"98
sed -i -E "s/src=\"game\.js(\?v=[^\"]*)?\"/src=\"game.js?v=${GAME_HASH}\"/" "$INDEX"99
printf "Asset hashes: game.js=%s style.css=%s framework.js=%s i18n.json=%s\n" \100
"$GAME_HASH" "$CSS_HASH" "$FW_HASH" "$I18N_HASH"101
82
printf "DONE\n"102
printf "DONE\n"@@ -280,7 +280,9 @@ I18n.apply = function (root)
280
280
var key = el.getAttribute('data-i18n');
281
281
if (!key)
282
282
{ continue; }
283
el.textContent = this.t(key);
283
// Fall back to English (inside t) then the existing label so a
284
// missing/stale key never replaces visible text with the raw key.
285
el.textContent = this.t(key, null, el.textContent);
284
286
}
285
287
nodes = root.querySelectorAll('[data-i18n-html]');
286
288
for (var i = 0; i < nodes.length; i++)
@@ -289,22 +291,25 @@ I18n.apply = function (root)
289
291
var key = el.getAttribute('data-i18n-html');
290
292
if (!key)
291
293
{ continue; }
292
el.innerHTML = this.t(key);
294
el.innerHTML = this.t(key, null, el.innerHTML);
293
295
}
294
296
nodes = root.querySelectorAll('[data-i18n-placeholder]');
295
297
for (var i = 0; i < nodes.length; i++)
296
298
{
297
nodes[i].setAttribute('placeholder', this.t(nodes[i].getAttribute('data-i18n-placeholder')));
299
var el = nodes[i];
300
el.setAttribute('placeholder', this.t(el.getAttribute('data-i18n-placeholder'), null, el.getAttribute('placeholder')));
298
301
}
299
302
nodes = root.querySelectorAll('[data-i18n-aria]');
300
303
for (var i = 0; i < nodes.length; i++)
301
304
{
302
nodes[i].setAttribute('aria-label', this.t(nodes[i].getAttribute('data-i18n-aria')));
305
var el = nodes[i];
306
el.setAttribute('aria-label', this.t(el.getAttribute('data-i18n-aria'), null, el.getAttribute('aria-label')));
303
307
}
304
308
nodes = root.querySelectorAll('[data-i18n-title]');
305
309
for (var i = 0; i < nodes.length; i++)
306
310
{
307
nodes[i].setAttribute('title', this.t(nodes[i].getAttribute('data-i18n-title')));
311
var el = nodes[i];
312
el.setAttribute('title', this.t(el.getAttribute('data-i18n-title'), null, el.getAttribute('title')));
308
313
}
309
314
};
310
315
@@ -280,7 +280,9 @@ I18n.apply = function (root)280
var key = el.getAttribute('data-i18n');280
var key = el.getAttribute('data-i18n');281
if (!key)281
if (!key)282
{ continue; }282
{ continue; }283
el.textContent = this.t(key);283
// Fall back to English (inside t) then the existing label so a284
// missing/stale key never replaces visible text with the raw key.285
el.textContent = this.t(key, null, el.textContent);284
}286
}285
nodes = root.querySelectorAll('[data-i18n-html]');287
nodes = root.querySelectorAll('[data-i18n-html]');286
for (var i = 0; i < nodes.length; i++)288
for (var i = 0; i < nodes.length; i++)@@ -289,22 +291,25 @@ I18n.apply = function (root)289
var key = el.getAttribute('data-i18n-html');291
var key = el.getAttribute('data-i18n-html');290
if (!key)292
if (!key)291
{ continue; }293
{ continue; }292
el.innerHTML = this.t(key);294
el.innerHTML = this.t(key, null, el.innerHTML);293
}295
}294
nodes = root.querySelectorAll('[data-i18n-placeholder]');296
nodes = root.querySelectorAll('[data-i18n-placeholder]');295
for (var i = 0; i < nodes.length; i++)297
for (var i = 0; i < nodes.length; i++)296
{298
{297
nodes[i].setAttribute('placeholder', this.t(nodes[i].getAttribute('data-i18n-placeholder')));299
var el = nodes[i];300
el.setAttribute('placeholder', this.t(el.getAttribute('data-i18n-placeholder'), null, el.getAttribute('placeholder')));298
}301
}299
nodes = root.querySelectorAll('[data-i18n-aria]');302
nodes = root.querySelectorAll('[data-i18n-aria]');300
for (var i = 0; i < nodes.length; i++)303
for (var i = 0; i < nodes.length; i++)301
{304
{302
nodes[i].setAttribute('aria-label', this.t(nodes[i].getAttribute('data-i18n-aria')));305
var el = nodes[i];306
el.setAttribute('aria-label', this.t(el.getAttribute('data-i18n-aria'), null, el.getAttribute('aria-label')));303
}307
}304
nodes = root.querySelectorAll('[data-i18n-title]');308
nodes = root.querySelectorAll('[data-i18n-title]');305
for (var i = 0; i < nodes.length; i++)309
for (var i = 0; i < nodes.length; i++)306
{310
{307
nodes[i].setAttribute('title', this.t(nodes[i].getAttribute('data-i18n-title')));311
var el = nodes[i];312
el.setAttribute('title', this.t(el.getAttribute('data-i18n-title'), null, el.getAttribute('title')));308
}313
}309
};314
};310
315
@@ -2866,7 +2866,20 @@ function bindLeaderboard ()
2866
2866
{
2867
2867
var btn = document.getElementById('btn-leaderboard');
2868
2868
if (btn)
2869
{ btn.addEventListener('click', function () { openLeaderboard(); }); }
2869
{
2870
var open = function () { openLeaderboard(); };
2871
btn.addEventListener('click', open);
2872
// Same mobile path as Play / Survival: some browsers swallow click
2873
// after a slightly sloppy tap.
2874
btn.addEventListener('pointerup', function (e)
2875
{
2876
if (e.pointerType === 'touch' || e.pointerType === 'pen')
2877
{
2878
e.preventDefault();
2879
open();
2880
}
2881
});
2882
}
2870
2883
var back = document.getElementById('btn-lb-back');
2871
2884
if (back)
2872
2885
{ back.addEventListener('click', closeLeaderboard); }
@@ -2880,6 +2893,8 @@ function bindLeaderboard ()
2880
2893
2881
2894
function openLeaderboard (tab)
2882
2895
{
2896
if (app.mode === 'leaderboard')
2897
{ return; }
2883
2898
hideEl('menu');
2884
2899
hideEl('online-panel');
2885
2900
hideEl('planet-select');
@@ -2866,7 +2866,20 @@ function bindLeaderboard ()2866
{2866
{2867
var btn = document.getElementById('btn-leaderboard');2867
var btn = document.getElementById('btn-leaderboard');2868
if (btn)2868
if (btn)2869
{ btn.addEventListener('click', function () { openLeaderboard(); }); }2869
{2870
var open = function () { openLeaderboard(); };2871
btn.addEventListener('click', open);2872
// Same mobile path as Play / Survival: some browsers swallow click2873
// after a slightly sloppy tap.2874
btn.addEventListener('pointerup', function (e)2875
{2876
if (e.pointerType === 'touch' || e.pointerType === 'pen')2877
{2878
e.preventDefault();2879
open();2880
}2881
});2882
}2870
var back = document.getElementById('btn-lb-back');2883
var back = document.getElementById('btn-lb-back');2871
if (back)2884
if (back)2872
{ back.addEventListener('click', closeLeaderboard); }2885
{ back.addEventListener('click', closeLeaderboard); }@@ -2880,6 +2893,8 @@ function bindLeaderboard ()2880
2893
2881
function openLeaderboard (tab)2894
function openLeaderboard (tab)2882
{2895
{2896
if (app.mode === 'leaderboard')2897
{ return; }2883
hideEl('menu');2898
hideEl('menu');2884
hideEl('online-panel');2899
hideEl('online-panel');2885
hideEl('planet-select');2900
hideEl('planet-select');@@ -0,0 +1,93 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const fs = require('node:fs');
6
const path = require('node:path');
7
const crypto = require('node:crypto');
8
const { spawnSync } = require('node:child_process');
9
10
const root = path.join(__dirname, '..');
11
const htmlPath = path.join(root, 'build/index.html');
12
const htaccessPath = path.join(root, 'server/.htaccess');
13
14
function sha12(file)
15
{
16
const buf = fs.readFileSync(path.join(root, file));
17
return crypto.createHash('sha256').update(buf).digest('hex').slice(0, 12);
18
}
19
20
function attr(html, attr, file)
21
{
22
const re = new RegExp(attr + '="' + file.replace('.', '\\.') + '\\?v=([0-9a-f]{12})"');
23
const m = html.match(re);
24
assert.ok(m, attr + ' ' + file + ' is missing a 12-char hash query in index.html');
25
return m[1];
26
}
27
28
describe('asset cache busting (t1256)', () =>
29
{
30
it('stamps content hashes on JS and CSS in index.html', () =>
31
{
32
const html = fs.readFileSync(htmlPath, 'utf8');
33
assert.equal(attr(html, 'href', 'style.css'), sha12('build/style.css'));
34
assert.equal(attr(html, 'src', 'framework.js'), sha12('build/framework.js'));
35
assert.equal(attr(html, 'src', 'game.js'), sha12('build/game.js'));
36
assert.doesNotMatch(html, /(?:href|src)="(?:style\.css|framework\.js|game\.js)"/);
37
});
38
39
it('is idempotent and follows a catalog or JS change', () =>
40
{
41
const script = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8');
42
assert.match(script, /asset_hash/);
43
assert.match(script, /game\.js\?v=/);
44
assert.match(script, /i18n\.json/);
45
// Restamp in a temp copy so we do not race other tests touching build/.
46
const tmp = fs.mkdtempSync(path.join(require('node:os').tmpdir(), 'meteors-stamp-'));
47
const index = path.join(tmp, 'index.html');
48
fs.writeFileSync(index, '<link rel="stylesheet" href="style.css">\n'
49
+ '<script src="framework.js"></script>\n'
50
+ '<script src="game.js"></script>\n');
51
const envScript = `sha256sum() { sha256sum "$@"; }
52
asset_hash() { sha256sum "$1" | cut -c1-12; }
53
INDEX="${index}"
54
GAME_HASH="aaaaaaaaaaaa"
55
CSS_HASH="bbbbbbbbbbbb"
56
FW_HASH="cccccccccccc"
57
sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=\${CSS_HASH}\\"/" "$INDEX"
58
sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=\${FW_HASH}\\"/" "$INDEX"
59
sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=\${GAME_HASH}\\"/" "$INDEX"
60
`;
61
// Drive the same sed the build script uses, twice.
62
const run = (game, css, fw) =>
63
{
64
const r = spawnSync('bash', ['-lc', `
65
INDEX=${JSON.stringify(index)}
66
sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=${css}\\"/" "$INDEX"
67
sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=${fw}\\"/" "$INDEX"
68
sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=${game}\\"/" "$INDEX"
69
`], { encoding: 'utf8' });
70
assert.equal(r.status, 0, r.stderr);
71
return fs.readFileSync(index, 'utf8');
72
};
73
const first = run('aaaaaaaaaaaa', 'bbbbbbbbbbbb', 'cccccccccccc');
74
assert.match(first, /style\.css\?v=bbbbbbbbbbbb/);
75
assert.match(first, /framework\.js\?v=cccccccccccc/);
76
assert.match(first, /game\.js\?v=aaaaaaaaaaaa/);
77
const second = run('dddddddddddd', 'bbbbbbbbbbbb', 'cccccccccccc');
78
assert.match(second, /game\.js\?v=dddddddddddd/);
79
assert.doesNotMatch(second, /game\.js\?v=aaaaaaaaaaaa/);
80
fs.rmSync(tmp, { recursive: true, force: true });
81
});
82
83
it('sets Cache-Control on HTML (no-cache) and versioned static assets', () =>
84
{
85
const conf = fs.readFileSync(htaccessPath, 'utf8');
86
assert.match(conf, /FilesMatch "\\?\.\(html\)\$"/);
87
assert.match(conf, /Cache-Control "no-cache"/);
88
assert.match(conf, /FilesMatch "\\?\.\(js\|css\|json\)\$"/);
89
assert.match(conf, /Cache-Control "public, max-age=31536000, immutable"/);
90
const router = fs.readFileSync(path.join(root, 'server/router.php'), 'utf8');
91
assert.match(router, /Cache-Control: no-cache/);
92
});
93
});
@@ -0,0 +1,93 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const fs = require('node:fs');6
const path = require('node:path');7
const crypto = require('node:crypto');8
const { spawnSync } = require('node:child_process');9
10
const root = path.join(__dirname, '..');11
const htmlPath = path.join(root, 'build/index.html');12
const htaccessPath = path.join(root, 'server/.htaccess');13
14
function sha12(file)15
{16
const buf = fs.readFileSync(path.join(root, file));17
return crypto.createHash('sha256').update(buf).digest('hex').slice(0, 12);18
}19
20
function attr(html, attr, file)21
{22
const re = new RegExp(attr + '="' + file.replace('.', '\\.') + '\\?v=([0-9a-f]{12})"');23
const m = html.match(re);24
assert.ok(m, attr + ' ' + file + ' is missing a 12-char hash query in index.html');25
return m[1];26
}27
28
describe('asset cache busting (t1256)', () =>29
{30
it('stamps content hashes on JS and CSS in index.html', () =>31
{32
const html = fs.readFileSync(htmlPath, 'utf8');33
assert.equal(attr(html, 'href', 'style.css'), sha12('build/style.css'));34
assert.equal(attr(html, 'src', 'framework.js'), sha12('build/framework.js'));35
assert.equal(attr(html, 'src', 'game.js'), sha12('build/game.js'));36
assert.doesNotMatch(html, /(?:href|src)="(?:style\.css|framework\.js|game\.js)"/);37
});38
39
it('is idempotent and follows a catalog or JS change', () =>40
{41
const script = fs.readFileSync(path.join(root, 'src/build.sh'), 'utf8');42
assert.match(script, /asset_hash/);43
assert.match(script, /game\.js\?v=/);44
assert.match(script, /i18n\.json/);45
// Restamp in a temp copy so we do not race other tests touching build/.46
const tmp = fs.mkdtempSync(path.join(require('node:os').tmpdir(), 'meteors-stamp-'));47
const index = path.join(tmp, 'index.html');48
fs.writeFileSync(index, '<link rel="stylesheet" href="style.css">\n'49
+ '<script src="framework.js"></script>\n'50
+ '<script src="game.js"></script>\n');51
const envScript = `sha256sum() { sha256sum "$@"; }52
asset_hash() { sha256sum "$1" | cut -c1-12; }53
INDEX="${index}"54
GAME_HASH="aaaaaaaaaaaa"55
CSS_HASH="bbbbbbbbbbbb"56
FW_HASH="cccccccccccc"57
sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=\${CSS_HASH}\\"/" "$INDEX"58
sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=\${FW_HASH}\\"/" "$INDEX"59
sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=\${GAME_HASH}\\"/" "$INDEX"60
`;61
// Drive the same sed the build script uses, twice.62
const run = (game, css, fw) =>63
{64
const r = spawnSync('bash', ['-lc', `65
INDEX=${JSON.stringify(index)}66
sed -i -E "s/href=\\"style\\.css(\\?v=[^\\"]*)?\\"/href=\\"style.css?v=${css}\\"/" "$INDEX"67
sed -i -E "s/src=\\"framework\\.js(\\?v=[^\\"]*)?\\"/src=\\"framework.js?v=${fw}\\"/" "$INDEX"68
sed -i -E "s/src=\\"game\\.js(\\?v=[^\\"]*)?\\"/src=\\"game.js?v=${game}\\"/" "$INDEX"69
`], { encoding: 'utf8' });70
assert.equal(r.status, 0, r.stderr);71
return fs.readFileSync(index, 'utf8');72
};73
const first = run('aaaaaaaaaaaa', 'bbbbbbbbbbbb', 'cccccccccccc');74
assert.match(first, /style\.css\?v=bbbbbbbbbbbb/);75
assert.match(first, /framework\.js\?v=cccccccccccc/);76
assert.match(first, /game\.js\?v=aaaaaaaaaaaa/);77
const second = run('dddddddddddd', 'bbbbbbbbbbbb', 'cccccccccccc');78
assert.match(second, /game\.js\?v=dddddddddddd/);79
assert.doesNotMatch(second, /game\.js\?v=aaaaaaaaaaaa/);80
fs.rmSync(tmp, { recursive: true, force: true });81
});82
83
it('sets Cache-Control on HTML (no-cache) and versioned static assets', () =>84
{85
const conf = fs.readFileSync(htaccessPath, 'utf8');86
assert.match(conf, /FilesMatch "\\?\.\(html\)\$"/);87
assert.match(conf, /Cache-Control "no-cache"/);88
assert.match(conf, /FilesMatch "\\?\.\(js\|css\|json\)\$"/);89
assert.match(conf, /Cache-Control "public, max-age=31536000, immutable"/);90
const router = fs.readFileSync(path.join(root, 'server/router.php'), 'utf8');91
assert.match(router, /Cache-Control: no-cache/);92
});93
});@@ -150,3 +150,41 @@ describe('i18n catalog (t1141)', () =>
150
150
}
151
151
});
152
152
});
153
154
describe('i18n fallback (t1256)', () =>
155
{
156
it('falls back to English when the active locale is missing a key', () =>
157
{
158
I18n._catalog = { locales: { en: { 'menu.leaderboard': 'Leaderboard' }, nl: {} } };
159
I18n.locale = 'nl';
160
assert.equal(I18n.t('menu.leaderboard'), 'Leaderboard');
161
assert.equal(I18n.t('menu.missing', null, 'Fallback'), 'Fallback');
162
});
163
164
it('never shows a raw key when applying labels', () =>
165
{
166
I18n._catalog = { locales: { en: { 'menu.play': 'Play' }, nl: {} } };
167
I18n.locale = 'nl';
168
169
function el(attrs, text)
170
{
171
const a = Object.assign({}, attrs);
172
return {
173
getAttribute: (k) => (k in a ? a[k] : null),
174
setAttribute: (k, v) => { a[k] = v; },
175
textContent: text || '',
176
innerHTML: text || '',
177
_attrs: a
178
};
179
}
180
const label = el({ 'data-i18n': 'menu.leaderboard' }, 'Leaderboard');
181
const play = el({ 'data-i18n': 'menu.play' }, 'Play');
182
const root = {
183
querySelectorAll: (sel) => sel === '[data-i18n]' ? [label, play] : []
184
};
185
I18n.apply(root);
186
assert.equal(label.textContent, 'Leaderboard');
187
assert.notEqual(label.textContent, 'menu.leaderboard');
188
assert.equal(play.textContent, 'Play');
189
});
190
});
@@ -150,3 +150,41 @@ describe('i18n catalog (t1141)', () =>150
}150
}151
});151
});152
});152
});153
154
describe('i18n fallback (t1256)', () =>155
{156
it('falls back to English when the active locale is missing a key', () =>157
{158
I18n._catalog = { locales: { en: { 'menu.leaderboard': 'Leaderboard' }, nl: {} } };159
I18n.locale = 'nl';160
assert.equal(I18n.t('menu.leaderboard'), 'Leaderboard');161
assert.equal(I18n.t('menu.missing', null, 'Fallback'), 'Fallback');162
});163
164
it('never shows a raw key when applying labels', () =>165
{166
I18n._catalog = { locales: { en: { 'menu.play': 'Play' }, nl: {} } };167
I18n.locale = 'nl';168
169
function el(attrs, text)170
{171
const a = Object.assign({}, attrs);172
return {173
getAttribute: (k) => (k in a ? a[k] : null),174
setAttribute: (k, v) => { a[k] = v; },175
textContent: text || '',176
innerHTML: text || '',177
_attrs: a178
};179
}180
const label = el({ 'data-i18n': 'menu.leaderboard' }, 'Leaderboard');181
const play = el({ 'data-i18n': 'menu.play' }, 'Play');182
const root = {183
querySelectorAll: (sel) => sel === '[data-i18n]' ? [label, play] : []184
};185
I18n.apply(root);186
assert.equal(label.textContent, 'Leaderboard');187
assert.notEqual(label.textContent, 'menu.leaderboard');188
assert.equal(play.textContent, 'Play');189
});190
});@@ -83,3 +83,15 @@ describe('Avatar helpers (t1247)', () =>
83
83
assert.equal(Avatar.usableUrl(''), null);
84
84
});
85
85
});
86
87
describe('leaderboard button (t1256)', () =>
88
{
89
it('binds click and a mobile pointerup path', () =>
90
{
91
const main = fs.readFileSync(path.join(root, 'src/main.js'), 'utf8');
92
const bind = main.split('function bindLeaderboard')[1] || '';
93
assert.match(bind, /btn-leaderboard/);
94
assert.match(bind, /pointerup/);
95
assert.match(bind, /pointerType === 'touch'/);
96
});
97
});
@@ -83,3 +83,15 @@ describe('Avatar helpers (t1247)', () =>83
assert.equal(Avatar.usableUrl(''), null);83
assert.equal(Avatar.usableUrl(''), null);84
});84
});85
});85
});86
87
describe('leaderboard button (t1256)', () =>88
{89
it('binds click and a mobile pointerup path', () =>90
{91
const main = fs.readFileSync(path.join(root, 'src/main.js'), 'utf8');92
const bind = main.split('function bindLeaderboard')[1] || '';93
assert.match(bind, /btn-leaderboard/);94
assert.match(bind, /pointerup/);95
assert.match(bind, /pointerType === 'touch'/);96
});97
});