fix: load admin assets from stable paths

This commit is contained in:
3252a8
2026-05-22 15:39:45 +03:00
parent cfe7c4ec5f
commit 4c0a798050
3 changed files with 48 additions and 15 deletions
+7 -8
View File
@@ -1372,10 +1372,10 @@ def _resolve_webapp_js_asset_name() -> str:
def _resolve_webapp_admin_js_asset_name() -> str:
return _resolve_hashed_js_asset_name(
kind="admin-js",
base_name="subscription_webapp_admin",
)
# The admin bundle is lazy-loaded from the already running Mini App. In
# deployments where nginx serves static files in front of aiohttp, stale
# hashed admin filenames can 404 even though the runtime build asset exists.
return _set_cached_asset_name("admin-js", "subscription_webapp_admin.js")
def _resolve_hashed_js_asset_name(*, kind: str, base_name: str) -> str:
@@ -1405,10 +1405,9 @@ def _resolve_webapp_css_asset_name() -> str:
def _resolve_webapp_admin_css_asset_name() -> str:
return _resolve_hashed_css_asset_name(
kind="admin-css",
base_name="subscription_webapp_admin",
)
# Keep the lazy-loaded admin stylesheet on the stable build filename for
# the same reason as the JS bundle above.
return _set_cached_asset_name("admin-css", "subscription_webapp_admin.css")
def _resolve_hashed_css_asset_name(*, kind: str, base_name: str) -> str:
+38 -4
View File
@@ -635,7 +635,10 @@
link.rel = "stylesheet";
link.href = href;
link.onload = () => resolve();
link.onerror = () => reject(new Error(`stylesheet_load_failed:${href}`));
link.onerror = () => {
link.remove();
reject(new Error(`stylesheet_load_failed:${href}`));
};
document.head.appendChild(link);
});
}
@@ -648,11 +651,34 @@
script.src = src;
script.async = true;
script.onload = () => resolve();
script.onerror = () => reject(new Error(`script_load_failed:${src}`));
script.onerror = () => {
script.remove();
reject(new Error(`script_load_failed:${src}`));
};
document.head.appendChild(script);
});
}
async function appendStylesheetWithFallback(id, href, fallbackName) {
const fallbackHref = resolveWebappAssetPath("", fallbackName);
try {
await appendStylesheetOnce(id, href);
} catch (error) {
if (!fallbackHref || href === fallbackHref) throw error;
await appendStylesheetOnce(id, fallbackHref);
}
}
async function appendScriptWithFallback(id, src, fallbackName) {
const fallbackSrc = resolveWebappAssetPath("", fallbackName);
try {
await appendScriptOnce(id, src);
} catch (error) {
if (!fallbackSrc || src === fallbackSrc) throw error;
await appendScriptOnce(id, fallbackSrc);
}
}
function readAdminBundleApi() {
const bundle = window.SubscriptionWebAppAdmin;
return bundle?.mount ? bundle : null;
@@ -672,8 +698,16 @@
adminBundlePromise = (async () => {
const cssHref = resolveWebappAssetPath(CFG.adminCssAsset, "subscription_webapp_admin.css");
const jsSrc = resolveWebappAssetPath(CFG.adminJsAsset, "subscription_webapp_admin.js");
await appendStylesheetOnce("subscription-webapp-admin-css", cssHref);
await appendScriptOnce("subscription-webapp-admin-js", jsSrc);
await appendStylesheetWithFallback(
"subscription-webapp-admin-css",
cssHref,
"subscription_webapp_admin.css",
);
await appendScriptWithFallback(
"subscription-webapp-admin-js",
jsSrc,
"subscription_webapp_admin.js",
);
const loaded = readAdminBundleApi();
if (!loaded) throw new Error("admin_bundle_missing_mount");
adminBundleApi = loaded;
+3 -3
View File
@@ -488,7 +488,7 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
"subscription_webapp.min.22222222.js",
)
def test_resolve_webapp_admin_asset_names_prefer_latest_minified_builds(self):
def test_resolve_webapp_admin_asset_names_use_stable_runtime_builds(self):
with tempfile.TemporaryDirectory() as tmpdir:
asset_dir = Path(tmpdir)
(asset_dir / "subscription_webapp_admin.js").write_text(
@@ -513,11 +513,11 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
with patch.object(webapp_assets, "ASSET_DIR", asset_dir):
self.assertEqual(
subscription_webapp._resolve_webapp_admin_js_asset_name(),
"subscription_webapp_admin.min.22222222.js",
"subscription_webapp_admin.js",
)
self.assertEqual(
subscription_webapp._resolve_webapp_admin_css_asset_name(),
"subscription_webapp_admin.22222222.css",
"subscription_webapp_admin.css",
)
async def test_js_asset_route_sets_immutable_cache_control_for_minified_asset(self):