mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-09-11 02:32:20 +02:00
galleryEditor.js and its js/editor/ graph are 54 modules / 576 KB, and gallery.js imported them statically. Every page load paid for the whole image editor even though most sessions never open the Edit tab: 54 of the 173 JS files on a cold load, and 576 KB of the decoded JS, were for a panel that was never displayed. Add a small panel-loader registry (static/js/panels.js) that imports a panel's module on first use and memoises the promise, so a double-click cannot start two loads and a failed load can still be retried. Convert the image editor to it, and route the two existing dynamic imports in chat.js and chatRenderer.js through the same entry so all three call sites share one module instance instead of two. closeEditor() and isEditorOpen() stay synchronous: if the module was never loaded there is no edit session to close and none can be open. The service worker keeps precaching the editor, in a separate PANEL_PRECACHE list, so the panel stays available offline even though index.html no longer loads it. The two lists now serve different purposes and the header comment says so.
54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
/**
|
|
* Panel loader registry — imports the modules behind a feature panel the
|
|
* first time that panel is actually used.
|
|
*
|
|
* The panels already populate themselves on open (each fetches its own data).
|
|
* They were just not *loaded* on demand: every one of them sat on the critical
|
|
* path of every page load, opened or not.
|
|
*
|
|
* A panel only belongs here once it has been checked for import-time side
|
|
* effects that something outside the panel depends on at startup. Entries get
|
|
* added one panel at a time, not in bulk.
|
|
*
|
|
* Note the service worker still precaches these modules (PANEL_PRECACHE in
|
|
* sw.js) — they are off the critical path, not off the offline manifest.
|
|
*/
|
|
|
|
const LOADERS = {
|
|
editor: () => import('./galleryEditor.js'),
|
|
};
|
|
|
|
/**
|
|
* Build a memoising loader over a name -> import-thunk map. Exported so the
|
|
* behaviour can be tested without pulling a real panel's module graph in.
|
|
*/
|
|
export function createPanelLoader(loaders) {
|
|
const cache = new Map();
|
|
return function load(name) {
|
|
const cached = cache.get(name);
|
|
if (cached) return cached;
|
|
const loader = loaders[name];
|
|
if (!loader) throw new Error(`loadPanel: unknown panel "${name}"`);
|
|
// A failed load (offline, 404, syntax error) is not memoised — caching the
|
|
// rejection would leave the panel broken for the rest of the session even
|
|
// after the network came back.
|
|
const pending = Promise.resolve().then(loader).catch((err) => {
|
|
cache.delete(name);
|
|
throw err;
|
|
});
|
|
cache.set(name, pending);
|
|
return pending;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Load a panel's module, once. Returns the same promise on every call for the
|
|
* same panel; throws for a name that is not registered.
|
|
*/
|
|
export const loadPanel = createPanelLoader(LOADERS);
|
|
|
|
/** Registered panel names — the registry is the list, not a second copy of it. */
|
|
export function panelNames() {
|
|
return Object.keys(LOADERS);
|
|
}
|