tarinai/service-worker.js
2026-06-24 18:49:17 +09:00

64 lines
3.2 KiB
JavaScript

"use strict";
const APP_VERSION = "15.24.53";
const CACHE_NAME = `tarinai-colony-${APP_VERSION}`;
const v = `v=${APP_VERSION}`;
// Generated from app_manifest.json. Run scripts/generate_app_files.py after changing static files.
const coreScriptNames = [
"version", "event_bus", "performance_manager", "registry_base", "disease_registry", "effect_registry", "sound_pack", "data", "item_visual_registry", "food_registry", "input_mode_manager", "math", "physics_helpers", "assets", "audio", "render", "sim_core", "structures", "items", "ants", "health", "tarinai", "tarinai_action_spec", "tarinai_behavior_state", "tarinai_identity_social", "tarinai_action_state", "tarinai_disease_nest", "tarinai_item_effects", "tarinai_needs_items", "tarinai_social_move_life", "tarinai_render", "world", "world_view", "family_graph", "world_family_social", "world_combat_effects", "world_environment", "world_spatial_budget", "world_ants_system", "weather_system", "world_update", "world_placement_log", "world_bootstrap", "text_catalog", "ui", "ui_log", "ui_helpers", "ui_selected", "snapshot_system", "save_system", "patch_helpers", "freeze_system", "relation_event_system", "ui_layout_dialogs", "ui_tools", "ui_bind", "ui_charts", "ui_family_data", "ui_family_async", "ui_family_layout", "ui_family_paths", "ui_family_render", "main", "debug_tools"
];
const CORE_ASSETS = [
"./",
"./index.html",
`./css/base.css?${v}`,
`./css/layout.css?${v}`,
`./css/panel.css?${v}`,
`./css/components.css?${v}`,
`./css/mobile.css?${v}`,
...coreScriptNames.map(name => `./js/${name}.js?${v}`),
];
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(CORE_ASSETS)).catch(() => undefined));
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key)))));
self.clients.claim();
});
function isStaticAsset(request) {
const url = new URL(request.url);
return /\.(?:js|css|webp|png|jpg|jpeg|gif|svg|mp3|wav|woff2?)$/i.test(url.pathname);
}
function cacheableResponse(response) {
return response && response.status === 200 && response.type !== "opaque";
}
function putCacheSafely(request, response) {
if (!cacheableResponse(response)) return Promise.resolve(false);
return caches.open(CACHE_NAME).then(cache => cache.put(request, response.clone())).then(() => true).catch(() => false);
}
self.addEventListener("fetch", (event) => {
const request = event.request;
if (request.method !== "GET") return;
const url = new URL(request.url);
if (url.origin !== self.location.origin) return;
if (/service-worker\.js$/i.test(url.pathname)) return;
if (request.mode === "navigate" || /index\.html$/i.test(url.pathname)) {
event.respondWith(fetch(request).then(response => {
putCacheSafely(request, response);
return response;
}).catch(() => caches.match(request).then(cached => cached || caches.match("./index.html"))));
return;
}
if (isStaticAsset(request)) {
event.respondWith(caches.match(request).then(cached => cached || fetch(request).then(response => {
putCacheSafely(request, response);
return response;
})));
}
});