From bfa3c62159b7b13b8262747665336e5d0ebfa7a5 Mon Sep 17 00:00:00 2001 From: Dmitry Afonchenko Date: Sat, 7 Nov 2026 21:04:08 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B8=D0=BD=D0=B8-=D0=B0=D0=BF=D0=BF=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=B2=D1=8B=D0=BA=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D0=B2=D0=B0=D0=B5=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D1=81=D1=80=D0=B0=D0=B7=D1=83,=20=D0=B1=D0=B5?= =?UTF-8?q?=D0=B7=20=D0=B1=D0=B5=D0=BB=D0=BE=D0=B3=D0=BE=20=D1=8D=D0=BA?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B0=20=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TAcLBj2Fn8nAKE4qWyn5es --- src/Trale/Program.cs | 14 ++++-- src/Trale/miniapp-src/src/main.tsx | 11 +++++ .../Site/CacheHeadersTests.cs | 47 +++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/IntegrationTests/Site/CacheHeadersTests.cs diff --git a/src/Trale/Program.cs b/src/Trale/Program.cs index 57273f81..16b45423 100644 --- a/src/Trale/Program.cs +++ b/src/Trale/Program.cs @@ -85,12 +85,20 @@ { OnPrepareResponse = context => { - // Comic frames live under a content hash (/stories///f1-800.webp): the same URL - // never serves different bytes, so the browser may keep them for good. - if (context.Context.Request.Path.StartsWithSegments("/stories")) + // Comic frames (/stories///f1-800.webp) and the mini-app build (/assets/index-.js) live + // under a content hash: the same URL never serves different bytes, so the browser may keep them for good. + var path = context.Context.Request.Path; + if (path.StartsWithSegments("/stories") || path.StartsWithSegments("/assets")) { context.Context.Response.Headers.CacheControl = "public, max-age=31536000, immutable"; } + // A page names the build by its hash. Kept without asking, an old page after a release points at a + // build that is gone, and the mini-app opens on a blank screen until reloaded — so pages are checked + // every time (an unchanged one comes back as a short 304). + else if (context.File.Name.EndsWith(".html", StringComparison.OrdinalIgnoreCase)) + { + context.Context.Response.Headers.CacheControl = "no-cache"; + } } }); diff --git a/src/Trale/miniapp-src/src/main.tsx b/src/Trale/miniapp-src/src/main.tsx index fc4c081a..770aa1fa 100644 --- a/src/Trale/miniapp-src/src/main.tsx +++ b/src/Trale/miniapp-src/src/main.tsx @@ -71,6 +71,17 @@ if (tg) { // rewrites the address. reportCampaignOpenFromUrl() +// Приложение открыто со времён прошлой сборки, а её куски после выката уже удалены — перезагрузиться на новую. +// Один раз за сессию, чтобы не зациклиться, если кусок не грузится по другой причине. +window.addEventListener('vite:preloadError', (event) => { + try { + if (sessionStorage.getItem('reloaded-for-new-build')) return + sessionStorage.setItem('reloaded-for-new-build', '1') + } catch { return } + event.preventDefault() + window.location.reload() +}) + // Скрипт запустился — сразу, пока человек не закрыл приложение на загрузке (analytics/appEvents.ts). appEvents.opened() diff --git a/tests/IntegrationTests/Site/CacheHeadersTests.cs b/tests/IntegrationTests/Site/CacheHeadersTests.cs new file mode 100644 index 00000000..c9b60a88 --- /dev/null +++ b/tests/IntegrationTests/Site/CacheHeadersTests.cs @@ -0,0 +1,47 @@ +using FluentAssertions; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; + +namespace IntegrationTests.Site; + +/// A page is checked with the server every time, so after a release it never points at a build that is gone; +/// the build itself, named by its hash, is kept for good. +public class CacheHeadersTests : TestBase +{ + private const string Folder = "zz-cache-headers-test"; + private const string Build = "index-zzcachetest.js"; + private string _webRoot = null!; + + [SetUp] + public void CreateFiles() + { + _webRoot = _testServer.Services.GetRequiredService().WebRootPath; + Directory.CreateDirectory(Path.Combine(_webRoot, Folder)); + File.WriteAllText(Path.Combine(_webRoot, Folder, "index.html"), "page"); + Directory.CreateDirectory(Path.Combine(_webRoot, "assets")); + File.WriteAllText(Path.Combine(_webRoot, "assets", Build), "console.log(1)"); + } + + [TearDown] + public void RemoveFiles() + { + Directory.Delete(Path.Combine(_webRoot, Folder), recursive: true); + File.Delete(Path.Combine(_webRoot, "assets", Build)); + } + + [Test] + public async Task A_page_is_checked_every_time() + { + var response = await _testServer.CreateClient().GetAsync($"/{Folder}/"); + + response.Headers.CacheControl!.NoCache.Should().BeTrue(); + } + + [Test] + public async Task The_build_is_kept_for_good() + { + var response = await _testServer.CreateClient().GetAsync($"/assets/{Build}"); + + response.Headers.CacheControl!.ToString().Should().Be("public, max-age=31536000, immutable"); + } +}