Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache brand images #17840

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/components/entity/state-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ export class StateBadge extends LitElement {
const stateObj = this.stateObj;

const iconStyle: { [name: string]: string } = {};
const hostStyle: Partial<CSSStyleDeclaration> = {
backgroundImage: "",
};
let backgroundImage = "";

this._showIcon = true;

Expand All @@ -135,13 +133,12 @@ export class StateBadge extends LitElement {
if (domain === "camera") {
imageUrl = cameraUrlWithWidthHeight(imageUrl, 80, 80);
}
hostStyle.backgroundImage = `url(${imageUrl})`;
backgroundImage = `url(${imageUrl})`;
this._showIcon = false;
if (domain === "update") {
hostStyle.borderRadius = "0";
}
if (domain === "media_player") {
hostStyle.borderRadius = "8%";
this.style.borderRadius = "0";
} else if (domain === "media_player") {
this.style.borderRadius = "8%";
}
} else if (this.color) {
// Externally provided overriding color wins over state color
Expand Down Expand Up @@ -182,12 +179,12 @@ export class StateBadge extends LitElement {
if (this.hass) {
imageUrl = this.hass.hassUrl(imageUrl);
}
hostStyle.backgroundImage = `url(${imageUrl})`;
backgroundImage = `url(${imageUrl})`;
this._showIcon = false;
}

this._iconStyle = iconStyle;
Object.assign(this.style, hostStyle);
this.style.backgroundImage = backgroundImage;
}

static get styles(): CSSResultGroup {
Expand Down
1 change: 1 addition & 0 deletions src/components/ha-config-entry-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class HaConfigEntryPicker extends LitElement {
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
@error=${this._onImageError}
@load=${this._onImageLoad}
Expand Down
2 changes: 2 additions & 0 deletions src/components/ha-related-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export class HaRelatedItems extends LitElement {
useFallback: true,
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
alt=${entry.domain}
slot="graphic"
/>
Expand Down
65 changes: 41 additions & 24 deletions src/entrypoints/service_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// eslint-disable-next-line spaced-comment
/// <reference path="../types/service-worker.d.ts" />
/* eslint-env serviceworker */
import { CacheableResponsePlugin } from "workbox-cacheable-response";
import { cacheNames, RouteHandler } from "workbox-core";
import { CacheableResponsePlugin } from "workbox-cacheable-response";
import { ExpirationPlugin } from "workbox-expiration";
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching";
import { registerRoute, setCatchHandler } from "workbox-routing";
Expand All @@ -15,11 +15,8 @@ import {

const noFallBackRegEx =
/\/(api|static|auth|frontend_latest|frontend_es5|local)\/.*/;
// Clean up caches from older workboxes and old service workers.
// Will help with cleaning up Workbox v4 stuff
cleanupOutdatedCaches();

function initRouting() {
const initRouting = () => {
precacheAndRoute(
// @ts-ignore
WB_MANIFEST,
Expand All @@ -35,6 +32,27 @@ function initRouting() {
new CacheFirst({ matchOptions: { ignoreSearch: true } })
);

// Cache any brand images used for 30 days
// Use revalidation so cache is always available during an extended outage
registerRoute(
({ url, request }) =>
url.origin === "https://brands.home-assistant.io" &&
request.destination === "image",
new StaleWhileRevalidate({
cacheName: "brands",
// CORS must be forced to work for CSS images
fetchOptions: { mode: "cors", credentials: "omit" },
plugins: [
// Add 404 so we quicly respond to domains with missing images
new CacheableResponsePlugin({ statuses: [0, 200, 404] }),
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 30,
purgeOnQuotaError: true,
}),
],
})
);

// Get api from network.
registerRoute(/\/(api|auth)\/.*/, new NetworkOnly());

Expand All @@ -59,18 +77,16 @@ function initRouting() {
new StaleWhileRevalidate({
cacheName: "file-cache",
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200],
}),
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24,
purgeOnQuotaError: true,
}),
],
})
);
}
};

function initPushNotifications() {
const initPushNotifications = () => {
// HTML5 Push Notifications
function firePushCallback(payload, jwt) {
// Don't send the JWT in the payload.data
Expand Down Expand Up @@ -176,7 +192,20 @@ function initPushNotifications() {
self.addEventListener("notificationclose", (event) => {
notificationEventCallback("closed", event);
});
}
};

const catchHandler: RouteHandler = async (options) => {
const dest = (options.request as Request).destination;
const url = (options.request as Request).url;

if (dest !== "document" || noFallBackRegEx.test(url)) {
return Response.error();
}
// eslint-disable-next-line no-console
console.log("Using fallback for:", url);

return (await caches.match("/", { ignoreSearch: true })) || Response.error();
};

self.addEventListener("install", (event) => {
// Delete all runtime caching, so that index.html has to be refetched.
Expand Down Expand Up @@ -206,19 +235,7 @@ self.addEventListener("message", (message) => {
}
});

const catchHandler: RouteHandler = async (options) => {
const dest = (options.request as Request).destination;
const url = (options.request as Request).url;

if (dest !== "document" || noFallBackRegEx.test(url)) {
return Response.error();
}
// eslint-disable-next-line no-console
console.log("Using fallback for:", url);

return (await caches.match("/", { ignoreSearch: true })) || Response.error();
};

cleanupOutdatedCaches();
initRouting();
setCatchHandler(catchHandler);
initPushNotifications();
1 change: 1 addition & 0 deletions src/onboarding/integration-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class IntegrationBadge extends LitElement {
type: "icon",
darkOptimized: this.darkOptimizedIcon,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
</div>
Expand Down
13 changes: 9 additions & 4 deletions src/panels/config/dashboard/ha-config-updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "@material/mwc-list/mwc-list";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/entity/state-badge";
Expand Down Expand Up @@ -87,7 +88,9 @@ class HaConfigUpdates extends SubscribeMixin(LitElement) {
<ha-list-item
twoline
graphic="medium"
class=${entity.attributes.skipped_version ? "skipped" : ""}
class=${ifDefined(
entity.attributes.skipped_version ? "skipped" : undefined
)}
.entity_id=${entity.entity_id}
.hasMeta=${!this.narrow}
@click=${this._openMoreInfo}
Expand All @@ -97,9 +100,11 @@ class HaConfigUpdates extends SubscribeMixin(LitElement) {
.title=${entity.attributes.title ||
entity.attributes.friendly_name}
.stateObj=${entity}
class=${this.narrow && entity.attributes.in_progress
? "updating"
: ""}
class=${ifDefined(
this.narrow && entity.attributes.in_progress
? "updating"
: undefined
)}
></state-badge>
${this.narrow && entity.attributes.in_progress
? html`<ha-circular-progress
Expand Down
2 changes: 2 additions & 0 deletions src/panels/config/devices/ha-config-device-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export class HaConfigDevicePage extends LitElement {
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>

Expand Down Expand Up @@ -741,6 +742,7 @@ export class HaConfigDevicePage extends LitElement {
type: "logo",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
@load=${this._onImageLoad}
@error=${this._onImageError}
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/devices/ha-config-devices-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ export class HaConfigDeviceDashboard extends LitElement {
device.domains.length
? html`<img
alt=""
crossorigin="anonymous"
referrerpolicy="no-referrer"
src=${brandsUrl({
domain: device.domains[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export class EnergyGridSettings extends LitElement {
? html`<div class="row" .entry=${this._co2ConfigEntry}>
<img
alt=""
crossorigin="anonymous"
referrerpolicy="no-referrer"
src=${brandsUrl({
domain: "co2signal",
Expand All @@ -246,6 +247,7 @@ export class EnergyGridSettings extends LitElement {
<div class="row border-bottom">
<img
alt=""
crossorigin="anonymous"
referrerpolicy="no-referrer"
src=${brandsUrl({
domain: "co2signal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export class DialogEnergySolarSettings
>
<img
alt=""
crossorigin="anonymous"
referrerpolicy="no-referrer"
style="height: 24px; margin-right: 16px;"
src=${brandsUrl({
Expand Down
9 changes: 8 additions & 1 deletion src/panels/config/hardware/ha-config-hardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,14 @@ class HaConfigHardware extends SubscribeMixin(LitElement) {
? html`
<ha-card outlined>
<div class="card-content">
${imageURL ? html`<img alt="" src=${imageURL} />` : ""}
${imageURL
? html`<img
alt=""
src=${imageURL}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>`
: ""}
<div class="board-info">
<p class="primary-text">
${boardName ||
Expand Down
3 changes: 2 additions & 1 deletion src/panels/config/helpers/dialog-helper-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,14 @@ export class DialogHelperDetail extends LitElement {
<img
slot="graphic"
loading="lazy"
alt=""
src=${brandsUrl({
domain,
type: "icon",
useFallback: true,
darkOptimized: this.hass.themes?.darkMode,
})}
aria-hidden="true"
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<span class="item-text"> ${label} </span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class HaConfigIntegrationPage extends SubscribeMixin(LitElement) {
type: "logo",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
@load=${this._onImageLoad}
@error=${this._onImageError}
Expand Down
3 changes: 3 additions & 0 deletions src/panels/config/integrations/ha-domain-integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class HaDomainIntegrations extends LitElement {
useFallback: true,
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<span
Expand Down Expand Up @@ -106,6 +107,7 @@ class HaDomainIntegrations extends LitElement {
useFallback: true,
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<span
Expand Down Expand Up @@ -168,6 +170,7 @@ class HaDomainIntegrations extends LitElement {
useFallback: true,
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class HaIntegrationActionCard extends LitElement {
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
@error=${this._onImageError}
@load=${this._onImageLoad}
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/integrations/ha-integration-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class HaIntegrationHeader extends LitElement {
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
@error=${this._onImageError}
@load=${this._onImageLoad}
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/integrations/ha-integration-list-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class HaIntegrationListItem extends ListItemBase {
darkOptimized: this.hass.themes?.darkMode,
brand: this.brand,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
</span>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export class ThreadConfigPanel extends SubscribeMixin(LitElement) {
darkOptimized: this.hass.themes?.darkMode,
})}
alt=${router.brand}
crossorigin="anonymous"
referrerpolicy="no-referrer"
@error=${this._onImageError}
@load=${this._onImageLoad}
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/repairs/ha-config-repairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class HaConfigRepairs extends LitElement {
darkOptimized: this.hass.themes?.darkMode,
})}
.title=${domainToName(this.hass.localize, issue.domain)}
crossorigin="anonymous"
referrerpolicy="no-referrer"
slot="graphic"
/>
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/repairs/integrations-startup-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class IntegrationsStartupTime extends LitElement {
useFallback: true,
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
slot="graphic"
/>
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/voice-assistants/assist-pref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class AssistPref extends LitElement {
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>Assist
</h1>
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/voice-assistants/cloud-alexa-pref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class CloudAlexaPref extends LitElement {
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>${this.hass.localize("ui.panel.config.cloud.account.alexa.title")}
</h1>
Expand Down
Loading
Loading