perf(relay): cache macro images instead of re-fetching every render
The relay web client re-fetched every macro image through the relay->desktop proxy on each render (tab switch, WS update) and revoked the object URL on load, so nothing was reused and fetches were serial — slow to display. - Client: cache image_path -> Promise<objectURL> and reuse across renders (macro images are content-addressed by uuid, so immutable); fetch in parallel and de-duplicate concurrent requests. - Proxy: send Cache-Control: private, max-age=31536000, immutable on image responses so the browser also disk-caches them across reloads. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,11 @@ class MacroPadApp {
|
|||||||
this.tabs = [];
|
this.tabs = [];
|
||||||
this.currentTab = 'All';
|
this.currentTab = 'All';
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
|
// Cache of image_path -> Promise<objectURL>. Macro images are
|
||||||
|
// content-addressed by uuid filename, so they are immutable: once
|
||||||
|
// fetched, an image never changes for a given path. Caching here avoids
|
||||||
|
// re-fetching every image through the relay on each re-render.
|
||||||
|
this._imageCache = new Map();
|
||||||
this.desktopConnected = false;
|
this.desktopConnected = false;
|
||||||
this.wsAuthenticated = false;
|
this.wsAuthenticated = false;
|
||||||
|
|
||||||
@@ -249,35 +254,55 @@ class MacroPadApp {
|
|||||||
this.loadMacroImages();
|
this.loadMacroImages();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch each macro image with the password header and display it
|
// Fetch each macro image with the password header and display it as a blob
|
||||||
// as a blob object URL so the credential never appears in a URL.
|
// object URL so the credential never appears in a URL. Images are fetched
|
||||||
async loadMacroImages() {
|
// in parallel and cached (see _imageCache) so re-renders reuse them instead
|
||||||
|
// of re-fetching through the relay each time.
|
||||||
|
loadMacroImages() {
|
||||||
for (const [id, macro] of Object.entries(this.macros)) {
|
for (const [id, macro] of Object.entries(this.macros)) {
|
||||||
if (!macro.image_path) continue;
|
if (!macro.image_path) continue;
|
||||||
const card = document.querySelector(`[data-macro-id="${id}"]`);
|
const card = document.querySelector(`[data-macro-id="${id}"]`);
|
||||||
if (!card) continue;
|
if (!card) continue;
|
||||||
const img = card.querySelector('.macro-image');
|
const img = card.querySelector('.macro-image');
|
||||||
if (!img) continue;
|
if (!img) continue;
|
||||||
|
this._applyMacroImage(img, macro.image_path);
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`/${this.sessionId}/api/image/${macro.image_path}`,
|
|
||||||
{ headers: this.getApiHeaders() }
|
|
||||||
);
|
|
||||||
if (!response.ok) continue; // keep placeholder
|
|
||||||
const blob = await response.blob();
|
|
||||||
const objectUrl = URL.createObjectURL(blob);
|
|
||||||
img.onload = () => URL.revokeObjectURL(objectUrl);
|
|
||||||
img.src = objectUrl;
|
|
||||||
img.style.display = '';
|
|
||||||
const placeholder = img.nextElementSibling;
|
|
||||||
if (placeholder) placeholder.style.display = 'none';
|
|
||||||
} catch (error) {
|
|
||||||
// Leave the placeholder visible on failure.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _applyMacroImage(img, imagePath) {
|
||||||
|
try {
|
||||||
|
const objectUrl = await this._getImageUrl(imagePath);
|
||||||
|
img.src = objectUrl;
|
||||||
|
img.style.display = '';
|
||||||
|
const placeholder = img.nextElementSibling;
|
||||||
|
if (placeholder) placeholder.style.display = 'none';
|
||||||
|
} catch (error) {
|
||||||
|
// Leave the placeholder visible on failure.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a cached Promise<objectURL> for an image path, fetching once.
|
||||||
|
// The object URL is retained for the page's lifetime (images are immutable
|
||||||
|
// per path, so there is nothing to invalidate); this de-duplicates
|
||||||
|
// concurrent requests and eliminates re-fetching on re-render.
|
||||||
|
_getImageUrl(imagePath) {
|
||||||
|
let entry = this._imageCache.get(imagePath);
|
||||||
|
if (entry) return entry;
|
||||||
|
entry = (async () => {
|
||||||
|
const response = await fetch(
|
||||||
|
`/${this.sessionId}/api/image/${imagePath}`,
|
||||||
|
{ headers: this.getApiHeaders() }
|
||||||
|
);
|
||||||
|
if (!response.ok) throw new Error(`image ${response.status}`);
|
||||||
|
const blob = await response.blob();
|
||||||
|
return URL.createObjectURL(blob);
|
||||||
|
})();
|
||||||
|
// Drop failed fetches from the cache so a later render can retry.
|
||||||
|
entry.catch(() => this._imageCache.delete(imagePath));
|
||||||
|
this._imageCache.set(imagePath, entry);
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
setupEventListeners() {
|
setupEventListeners() {
|
||||||
document.getElementById('tabs-container').addEventListener('click', (e) => {
|
document.getElementById('tabs-container').addEventListener('click', (e) => {
|
||||||
if (e.target.classList.contains('tab')) {
|
if (e.target.classList.contains('tab')) {
|
||||||
|
|||||||
@@ -49,6 +49,10 @@ export function createApiProxy(
|
|||||||
if (response.body?.base64 && response.body?.contentType) {
|
if (response.body?.base64 && response.body?.contentType) {
|
||||||
const buffer = Buffer.from(response.body.base64, 'base64');
|
const buffer = Buffer.from(response.body.base64, 'base64');
|
||||||
res.set('Content-Type', response.body.contentType);
|
res.set('Content-Type', response.body.contentType);
|
||||||
|
// Macro images are content-addressed (uuid filenames) and therefore
|
||||||
|
// immutable, so let the browser cache them aggressively. 'private'
|
||||||
|
// keeps them out of shared proxy caches since they sit behind auth.
|
||||||
|
res.set('Cache-Control', 'private, max-age=31536000, immutable');
|
||||||
res.send(buffer);
|
res.send(buffer);
|
||||||
} else {
|
} else {
|
||||||
res.status(response.status).json(response.body);
|
res.status(response.status).json(response.body);
|
||||||
|
|||||||
Reference in New Issue
Block a user