fix header logo optimize
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Has been skipped
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Has been skipped

This commit is contained in:
Mohamadzadeh 2026-10-25 18:49:40 +03:30
parent f11313909d
commit 03d7307adf
2 changed files with 69 additions and 75 deletions

File diff suppressed because one or more lines are too long

View File

@ -62,6 +62,12 @@ const STYLES = `
flex-shrink:0;
object-fit:contain;
}
/* Only opt-in optimized logos use a square, padded image box. */
.logo.logo--optimized{
aspect-ratio:1 / 1;
max-height:none;
padding:8px;
}
.brand{
display:flex;
min-width:0;
@ -243,27 +249,16 @@ class AppHeaderElement extends HTMLElement {
const update = () => {
if (!active || failed || !logo.isConnected) return;
const rect = logo.getBoundingClientRect();
const width = Math.max(0, Math.round(rect.width));
if (!width) return;
// Before an image has loaded, the browser cannot know its aspect ratio.
// Start from the available bounds (square logo is the common case); after
// load and on resizing, measure the *actual rendered element* again.
let height;
if (logo.naturalWidth && logo.naturalHeight) {
height = Math.max(0, Math.round(rect.height));
} else {
const maxHeight = Number.parseFloat(getComputedStyle(logo).maxHeight);
height = Number.isFinite(maxHeight)
? Math.min(width, Math.round(maxHeight))
: width;
}
if (!height) return;
const boxSize = Math.max(0, Math.round(rect.width));
if (!boxSize) return;
// The optimized image sits in a square box with 8px of padding
// on every side; resize the remote asset to the usable inner square.
const imageSize = Math.max(1, boxSize - 16);
const url = processImage(absoluteSource, {
w: width,
h: height,
q: 70,
w: imageSize,
h: imageSize,
q: 100,
webp: true,
});
if (url === lastRequestedUrl) return;
@ -336,6 +331,7 @@ class AppHeaderElement extends HTMLElement {
if (logoSrc) {
const logo = createElement("img", "logo");
if (logoOptimize === true) logo.classList.add("logo--optimized");
logo.alt = logoAlt || "logo";
logo.loading = logoOptimize ? "eager" : "lazy";
brandSection.appendChild(logo);