diff --git a/src/Jiaowu.Api/Controllers/BusinessSettingsController.cs b/src/Jiaowu.Api/Controllers/BusinessSettingsController.cs index 92838bb..8938770 100644 --- a/src/Jiaowu.Api/Controllers/BusinessSettingsController.cs +++ b/src/Jiaowu.Api/Controllers/BusinessSettingsController.cs @@ -16,6 +16,19 @@ public sealed class BusinessSettingsController(AppDbContext db, BusinessSettings [AllowAnonymous] public IActionResult Branding() => Ok(reader.Brand()); + [HttpGet("manifest.webmanifest")] + [AllowAnonymous] + public IActionResult Manifest() + { + var brand = reader.Brand(); + Response.Headers.CacheControl = "no-store"; + return new JsonResult(new { + name = brand.ProductName, short_name = brand.ShortName, description = brand.Description, + theme_color = "#152550", background_color = "#152550", display = "standalone", start_url = "/", scope = "/", + icons = new[] { new { src = "/favicon.svg", sizes = "any", type = "image/svg+xml", purpose = "any maskable" } } + }) { ContentType = "application/manifest+json" }; + } + [HttpGet] [Authorize(Roles = SystemRoles.SuperAdmin)] public IActionResult Get() => Ok(reader.Read()); diff --git a/tests/Jiaowu.Api.Tests/BusinessSettingsTests.cs b/tests/Jiaowu.Api.Tests/BusinessSettingsTests.cs index 141ec77..91e1a47 100644 --- a/tests/Jiaowu.Api.Tests/BusinessSettingsTests.cs +++ b/tests/Jiaowu.Api.Tests/BusinessSettingsTests.cs @@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Http; +using System.Text.Json; namespace Jiaowu.Api.Tests; @@ -37,5 +39,11 @@ public sealed class BusinessSettingsTests Assert.Equal("NEW", nextRequest.Document().DocumentNumberPrefix); Assert.Equal(30, nextRequest.Read().GradeAppealWindowDays); Assert.Single(await db.BusinessSettings.ToListAsync()); + var publicController = new BusinessSettingsController(db, nextRequest) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } }; + var manifest = Assert.IsType(publicController.Manifest()); + var manifestJson = JsonSerializer.SerializeToElement(manifest.Value); + Assert.Equal("新产品", manifestJson.GetProperty("name").GetString()); + Assert.Equal("新简称", manifestJson.GetProperty("short_name").GetString()); + Assert.Equal("no-store", publicController.Response.Headers.CacheControl.ToString()); } } diff --git a/web/src/config/branding.ts b/web/src/config/branding.ts index e3a023f..c68dedc 100644 --- a/web/src/config/branding.ts +++ b/web/src/config/branding.ts @@ -17,4 +17,5 @@ export async function refreshBranding() { if (!response.ok) throw new Error('Unable to load branding') Object.assign(branding, await response.json() as AppBranding) document.title = branding.productName + document.querySelector('meta[name="description"]')?.setAttribute('content', branding.description) } diff --git a/web/vite.config.ts b/web/vite.config.ts index 5cfe66d..44189a7 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -92,6 +92,20 @@ export default defineConfig(({ mode }) => { ], }, }), + { + name: 'runtime-branding-manifest', + enforce: 'post', + transformIndexHtml: { + order: 'post', + handler(html: string) { + if (isCapacitor) return html + const apiBase = (env.VITE_API_BASE_URL || '/api').replace(/\/$/, '') + // A manifest hosted on another origin cannot safely use this site's start URL and scope. + if (!apiBase.startsWith('/') || apiBase.startsWith('//')) return html + return html.replace(/()/, `$1${escapeHtml(apiBase)}/business-settings/manifest.webmanifest$2`) + }, + }, + }, ], base: isCapacitor ? './' : '/', build: {