Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Trale/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,20 @@
{
OnPrepareResponse = context =>
{
// Comic frames live under a content hash (/stories/<id>/<hash>/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/<id>/<hash>/f1-800.webp) and the mini-app build (/assets/index-<hash>.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";
}
}
});

Expand Down
11 changes: 11 additions & 0 deletions src/Trale/miniapp-src/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
47 changes: 47 additions & 0 deletions tests/IntegrationTests/Site/CacheHeadersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using FluentAssertions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace IntegrationTests.Site;

/// <summary>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.</summary>
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<IWebHostEnvironment>().WebRootPath;
Directory.CreateDirectory(Path.Combine(_webRoot, Folder));
File.WriteAllText(Path.Combine(_webRoot, Folder, "index.html"), "<!doctype html><title>page</title>");
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");
}
}
Loading