@profitlich/template-toolkit 7.1.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CLAUDE.project.md CHANGED
@@ -178,3 +178,11 @@ Einen neuen Entry in `rollupOptions.input` eintragen **nur wenn** das Script per
178
178
  ### Bilder
179
179
 
180
180
  Kein `lazysizes`. Ausschliesslich natives `loading="lazy"`.
181
+
182
+ ### Kein Ausblenden gegen FOUC
183
+
184
+ Den Body **nie** bis `DOMContentLoaded` per `visibility: hidden` verstecken, auch nicht nur in Dev. Der verbreitete Trick ist dasselbe Muster wie die Anti-Flicker-Snippets von A/B-Test-Tools: Solange der Body unsichtbar ist, gilt nichts als gemalt, und `DOMContentLoaded` wartet auf alle Modul-Skripte. FCP und LCP hängen damit am langsamsten Skript der Seite. Gegen FOUC schützt in Produktion schon das CSS, das im `<head>` blockiert. Dass der Vite-Dev-Server CSS per JS nachlädt und dort kurz flackert, wird in Kauf genommen.
185
+
186
+ Was bleibt: `body[data-preloading="true"]` gegen Transitions beim Laden und das Firefox-`<script>0</script>`.
187
+
188
+ Grosse Bibliotheken, die nur ein Teil der Seiten braucht, per dynamischem `import()` laden, nicht statisch – wie `MuxPlayer` mit `@mux/mux-player`.
@@ -1,4 +1,3 @@
1
- import '@mux/mux-player';
2
1
  import './mux-player.scss';
3
2
 
4
3
  /**
@@ -16,8 +15,10 @@ import './mux-player.scss';
16
15
  *
17
16
  * Ladereihenfolge:
18
17
  * 1. observeLazyElements() beobachtet alle .mux-player-Container.
19
- * 2. Betritt ein Container den Viewport, ersetzt #handleLazyLoad ihn durch
20
- * ein <mux-player>-Element und ruft handleVisibilityChange auf.
18
+ * 2. Betritt ein Container den Viewport, lädt #handleLazyLoad die Bibliothek
19
+ * `@mux/mux-player` (einmalig, per dynamischem Import), ersetzt den
20
+ * Container durch ein <mux-player>-Element und ruft handleVisibilityChange
21
+ * auf. Seiten ohne sichtbares Video laden die Bibliothek nie.
21
22
  * 3. handleVisibilityChange → #setup für alle sichtbaren Videos:
22
23
  * - Autoplay: muted, Controls versteckt, #playPauseObserver startet
23
24
  * - Kein Autoplay: keine Änderungen, User steuert Wiedergabe selbst
@@ -49,6 +50,8 @@ export class MuxPlayer {
49
50
  #disableTracking;
50
51
  #envKey;
51
52
  #metadata;
53
+ /** @type {Promise<unknown> | null} */
54
+ #library = null;
52
55
 
53
56
  /**
54
57
  * @param {MuxPlayerOptions} [options]
@@ -125,89 +128,108 @@ export class MuxPlayer {
125
128
  #handleLazyLoad(entries) {
126
129
  for (const entry of entries) {
127
130
  if (entry.isIntersecting) {
131
+ // Sofort, nicht erst nach dem Import: Sonst meldet der Observer
132
+ // denselben Container erneut, solange die Bibliothek lädt.
128
133
  this.#lazyLoadObserver.unobserve(entry.target);
129
- const container = entry.target;
130
- const playbackId = container.dataset.playbackId;
131
- const aspectRatio = container.dataset.aspectRatio || '16 / 9';
132
- const autoplay = container.dataset.autoplay === "true";
133
-
134
- const player = document.createElement('mux-player');
135
- player.playbackId = playbackId;
136
- player.streamType = 'on-demand';
137
- player.style.aspectRatio = aspectRatio;
138
- player.thumbnailTime = 0;
139
- if (this.#noLowRes) {
140
- const cssWidth = container.getBoundingClientRect().width
141
- || container.parentElement?.getBoundingClientRect().width;
142
- const physicalWidth = cssWidth * window.devicePixelRatio;
143
- // aspectRatio funktioniert mit Zahl und mit Verhältnis (x/y)
144
- const arRatio = aspectRatio.includes('/')
145
- ? (([w, h]) => parseFloat(w) / parseFloat(h))(aspectRatio.split('/'))
146
- : parseFloat(aspectRatio);
147
- const physicalHeight = physicalWidth / arRatio;
148
- const tiers = [
149
- [270, '270p'], [360, '360p'], [480, '480p'], [540, '540p'],
150
- [720, '720p'], [1080, '1080p'], [1440, '1440p'], [2160, '2160p'],
151
- ];
152
- player.minResolution = (tiers.find(([px]) => physicalHeight <= px) ?? [, '2160p'])[1];
153
- }
154
-
155
- if (container.dataset.accentColor) {
156
- player.accentColor = container.dataset.accentColor;
157
- }
158
-
159
- if (container.dataset.poster === 'false') {
160
- player.setAttribute('poster', '');
161
- } else if (container.dataset.poster) {
162
- player.poster = container.dataset.poster;
163
- }
164
-
165
- if (autoplay) {
166
- player.setAttribute('data-autoplay', 'true');
167
- } else {
168
- player.setAttribute('data-autoplay', 'false');
169
- }
170
-
171
- // Der aktuelle Wert, nicht der aus dem HTML: Ein Skript kann
172
- // ihn am Container schon vor dem Lazy-Load umgeschaltet haben.
173
- if (container.dataset.autoplayEnabled !== undefined) {
174
- player.setAttribute('data-autoplay-enabled', container.dataset.autoplayEnabled);
175
- }
176
-
177
- if (this.#disableTracking) {
178
- player.setAttribute('disable-tracking', '');
179
- } else if (this.#envKey) {
180
- player.setAttribute('env-key', this.#envKey);
181
- if (this.#metadata) {
182
- player.metadata = this.#metadata;
183
- }
184
- }
185
-
186
- const setStatus = value => player.setAttribute('data-status', value);
187
-
188
- player.addEventListener('loadstart', () => setStatus('loadstart'));
189
- player.addEventListener('playing', () => setStatus('playing'));
190
- player.addEventListener('pause', () => setStatus('pause'));
191
- player.addEventListener('ended', () => setStatus('ended'));
192
-
193
- console.log(
194
- 'Toolkit Video',
195
- 'width:', container.getBoundingClientRect().width,
196
- 'height:', container.getBoundingClientRect().height,
197
- 'aspectRatio:', aspectRatio
198
- );
199
-
200
- container.replaceWith(player);
201
- setStatus('false');
202
-
203
- this.handleVisibilityChange(player);
204
- if (this.#onPlayerCreated) {
205
- this.#onPlayerCreated(player, container);
206
- }
134
+ this.#loadLibrary()
135
+ .then(() => this.#createPlayer(entry.target))
136
+ .catch(e => console.error('Mux-Player konnte nicht geladen werden:', e));
207
137
  }
208
138
  }
209
139
  }
210
140
 
141
+ /**
142
+ * Die Bibliothek ist rund 1 MB gross. Statisch importiert, läge sie auf
143
+ * jeder Seite im kritischen Pfad, auch wenn kein Video sichtbar wird.
144
+ */
145
+ #loadLibrary() {
146
+ this.#library ??= import('@mux/mux-player');
147
+ return this.#library;
148
+ }
149
+
150
+ #createPlayer(container) {
151
+ // Erst nach dem Import: Eigenschaften, die vor der Definition des
152
+ // Custom Elements gesetzt werden, landen als Own-Properties auf dem
153
+ // Element und verdecken nach dem Upgrade die Setter des Players.
154
+ const playbackId = container.dataset.playbackId;
155
+ const aspectRatio = container.dataset.aspectRatio || '16 / 9';
156
+ const autoplay = container.dataset.autoplay === "true";
157
+
158
+ const player = document.createElement('mux-player');
159
+ player.playbackId = playbackId;
160
+ player.streamType = 'on-demand';
161
+ player.style.aspectRatio = aspectRatio;
162
+ player.thumbnailTime = 0;
163
+ if (this.#noLowRes) {
164
+ const cssWidth = container.getBoundingClientRect().width
165
+ || container.parentElement?.getBoundingClientRect().width;
166
+ const physicalWidth = cssWidth * window.devicePixelRatio;
167
+ // aspectRatio funktioniert mit Zahl und mit Verhältnis (x/y)
168
+ const arRatio = aspectRatio.includes('/')
169
+ ? (([w, h]) => parseFloat(w) / parseFloat(h))(aspectRatio.split('/'))
170
+ : parseFloat(aspectRatio);
171
+ const physicalHeight = physicalWidth / arRatio;
172
+ const tiers = [
173
+ [270, '270p'], [360, '360p'], [480, '480p'], [540, '540p'],
174
+ [720, '720p'], [1080, '1080p'], [1440, '1440p'], [2160, '2160p'],
175
+ ];
176
+ player.minResolution = (tiers.find(([px]) => physicalHeight <= px) ?? [, '2160p'])[1];
177
+ }
178
+
179
+ if (container.dataset.accentColor) {
180
+ player.accentColor = container.dataset.accentColor;
181
+ }
182
+
183
+ if (container.dataset.poster === 'false') {
184
+ player.setAttribute('poster', '');
185
+ } else if (container.dataset.poster) {
186
+ player.poster = container.dataset.poster;
187
+ }
188
+
189
+ if (autoplay) {
190
+ player.setAttribute('data-autoplay', 'true');
191
+ } else {
192
+ player.setAttribute('data-autoplay', 'false');
193
+ }
194
+
195
+ // Der aktuelle Wert, nicht der aus dem HTML: Ein Skript kann
196
+ // ihn am Container schon vor dem Lazy-Load umgeschaltet haben.
197
+ if (container.dataset.autoplayEnabled !== undefined) {
198
+ player.setAttribute('data-autoplay-enabled', container.dataset.autoplayEnabled);
199
+ }
200
+
201
+ if (this.#disableTracking) {
202
+ player.setAttribute('disable-tracking', '');
203
+ } else if (this.#envKey) {
204
+ player.setAttribute('env-key', this.#envKey);
205
+ if (this.#metadata) {
206
+ player.metadata = this.#metadata;
207
+ }
208
+ }
209
+
210
+ const setStatus = value => player.setAttribute('data-status', value);
211
+
212
+ player.addEventListener('loadstart', () => setStatus('loadstart'));
213
+ player.addEventListener('playing', () => setStatus('playing'));
214
+ player.addEventListener('pause', () => setStatus('pause'));
215
+ player.addEventListener('ended', () => setStatus('ended'));
216
+
217
+ console.log(
218
+ 'Toolkit Video',
219
+ 'width:', container.getBoundingClientRect().width,
220
+ 'height:', container.getBoundingClientRect().height,
221
+ 'aspectRatio:', aspectRatio
222
+ );
223
+
224
+ container.replaceWith(player);
225
+ setStatus('false');
226
+
227
+ this.handleVisibilityChange(player);
228
+ if (this.#onPlayerCreated) {
229
+ this.#onPlayerCreated(player, container);
230
+ }
231
+ }
232
+
211
233
  // Für autoplay Videos: play und pause
212
234
  #handlePlayPause(entries) {
213
235
  for (const entry of entries) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@profitlich/template-toolkit",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "description": "Shared SCSS layout system, JS utilities, components and build scripts for profitlich template repos",
5
5
  "type": "module",
6
6
  "sass": "scss/forward.scss",