fix: load admin assets from stable paths
This commit is contained in:
@@ -1372,10 +1372,10 @@ def _resolve_webapp_js_asset_name() -> str:
|
|||||||
|
|
||||||
|
|
||||||
def _resolve_webapp_admin_js_asset_name() -> str:
|
def _resolve_webapp_admin_js_asset_name() -> str:
|
||||||
return _resolve_hashed_js_asset_name(
|
# The admin bundle is lazy-loaded from the already running Mini App. In
|
||||||
kind="admin-js",
|
# deployments where nginx serves static files in front of aiohttp, stale
|
||||||
base_name="subscription_webapp_admin",
|
# 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:
|
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:
|
def _resolve_webapp_admin_css_asset_name() -> str:
|
||||||
return _resolve_hashed_css_asset_name(
|
# Keep the lazy-loaded admin stylesheet on the stable build filename for
|
||||||
kind="admin-css",
|
# the same reason as the JS bundle above.
|
||||||
base_name="subscription_webapp_admin",
|
return _set_cached_asset_name("admin-css", "subscription_webapp_admin.css")
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _resolve_hashed_css_asset_name(*, kind: str, base_name: str) -> str:
|
def _resolve_hashed_css_asset_name(*, kind: str, base_name: str) -> str:
|
||||||
|
|||||||
+38
-4
@@ -635,7 +635,10 @@
|
|||||||
link.rel = "stylesheet";
|
link.rel = "stylesheet";
|
||||||
link.href = href;
|
link.href = href;
|
||||||
link.onload = () => resolve();
|
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);
|
document.head.appendChild(link);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -648,11 +651,34 @@
|
|||||||
script.src = src;
|
script.src = src;
|
||||||
script.async = true;
|
script.async = true;
|
||||||
script.onload = () => resolve();
|
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);
|
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() {
|
function readAdminBundleApi() {
|
||||||
const bundle = window.SubscriptionWebAppAdmin;
|
const bundle = window.SubscriptionWebAppAdmin;
|
||||||
return bundle?.mount ? bundle : null;
|
return bundle?.mount ? bundle : null;
|
||||||
@@ -672,8 +698,16 @@
|
|||||||
adminBundlePromise = (async () => {
|
adminBundlePromise = (async () => {
|
||||||
const cssHref = resolveWebappAssetPath(CFG.adminCssAsset, "subscription_webapp_admin.css");
|
const cssHref = resolveWebappAssetPath(CFG.adminCssAsset, "subscription_webapp_admin.css");
|
||||||
const jsSrc = resolveWebappAssetPath(CFG.adminJsAsset, "subscription_webapp_admin.js");
|
const jsSrc = resolveWebappAssetPath(CFG.adminJsAsset, "subscription_webapp_admin.js");
|
||||||
await appendStylesheetOnce("subscription-webapp-admin-css", cssHref);
|
await appendStylesheetWithFallback(
|
||||||
await appendScriptOnce("subscription-webapp-admin-js", jsSrc);
|
"subscription-webapp-admin-css",
|
||||||
|
cssHref,
|
||||||
|
"subscription_webapp_admin.css",
|
||||||
|
);
|
||||||
|
await appendScriptWithFallback(
|
||||||
|
"subscription-webapp-admin-js",
|
||||||
|
jsSrc,
|
||||||
|
"subscription_webapp_admin.js",
|
||||||
|
);
|
||||||
const loaded = readAdminBundleApi();
|
const loaded = readAdminBundleApi();
|
||||||
if (!loaded) throw new Error("admin_bundle_missing_mount");
|
if (!loaded) throw new Error("admin_bundle_missing_mount");
|
||||||
adminBundleApi = loaded;
|
adminBundleApi = loaded;
|
||||||
|
|||||||
@@ -488,7 +488,7 @@ class WebAppAssetTests(unittest.IsolatedAsyncioTestCase):
|
|||||||
"subscription_webapp.min.22222222.js",
|
"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:
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
asset_dir = Path(tmpdir)
|
asset_dir = Path(tmpdir)
|
||||||
(asset_dir / "subscription_webapp_admin.js").write_text(
|
(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):
|
with patch.object(webapp_assets, "ASSET_DIR", asset_dir):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
subscription_webapp._resolve_webapp_admin_js_asset_name(),
|
subscription_webapp._resolve_webapp_admin_js_asset_name(),
|
||||||
"subscription_webapp_admin.min.22222222.js",
|
"subscription_webapp_admin.js",
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
subscription_webapp._resolve_webapp_admin_css_asset_name(),
|
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):
|
async def test_js_asset_route_sets_immutable_cache_control_for_minified_asset(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user