feat(terminal): older TVs get a legacy build with a compatibility layer; modern output unchanged

- @vitejs/plugin-legacy: browsers without import.meta.resolve (older than Chrome 105 /
  Firefox 106 / Safari 16.4) load a second copy built for Chrome 79 / Firefox 78 / Safari 13
  with polyfills. The modern targets and the stylesheet stay on Vite's own defaults, so modern
  chunks are byte-identical to before (only plugin-legacy's 230-byte guard in the entry).
- terminal/src/compat.js (legacy copy only): swaps in <name>.compat.css (lightningcss for the
  older targets), sets container-size variables for signage slides, emulates flex gap.
- @xenia/signage cq()/CQ_BOX: container units as before where supported, calc(var()) fallback
  otherwise; EmbeddedSlide no longer uses inline `inset`.
- Shared Notice (@xenia/ui); "Devices that work" in Pair a device and Signage -> Screens (EN/EL).
- tests/e2e/compat.mjs: modern browser loads nothing extra; a simulated older TV runs the legacy
  copy and the layer, pairs, and plays a slide at the same sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 17:26:16 +02:00
co-authored by Claude Opus 5.5
parent aa8ba5d1d4
commit 14d592d963
23 changed files with 625 additions and 40 deletions
+3
View File
@@ -28,14 +28,17 @@
},
"devDependencies": {
"@eslint/js": "^9.39.5",
"@vitejs/plugin-legacy": "^8.2.4",
"@vitejs/plugin-react": "^6.3.0",
"autoprefixer": "^10.6.2",
"eslint": "^9.39.5",
"eslint-plugin-react-hooks": "^7.1.1",
"eslint-plugin-react-refresh": "^0.5.7",
"globals": "^17.14.0",
"lightningcss": "^1.33.0",
"postcss": "^8.5.33",
"tailwindcss": "^3.4.16",
"terser": "^5.51.4",
"vite": "^8.3.8"
}
}
+2 -2
View File
@@ -10,7 +10,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from '@xenia/i18n'
import client from '../../api/client'
import { useDevice, useLiveEvent, useLiveEvents } from '../../device/context'
import { boardPages, frameStyle, localNow, mediaUrls, menuMs, nextSlide, pageAt, playlistNow, SlideView, slideMs } from '@xenia/signage'
import { boardPages, CQ_BOX, frameStyle, localNow, mediaUrls, menuMs, nextSlide, pageAt, playlistNow, SlideView, slideMs } from '@xenia/signage'
const quiet = { xenia: { noOverride: true } }
const CACHE_KEY = 'xenia:signage:screen'
@@ -94,7 +94,7 @@ export default function SignageApp() {
return (
<main className="fixed inset-0 overflow-hidden select-none cursor-none" style={{ background: '#000' }}
data-testid="app-signage" data-playlist={playlist?.id ?? ''} data-orientation={screen?.orientation ?? ''} data-offline={offline ? 'true' : 'false'}>
<div style={frameStyle(screen?.rotation)} data-testid="signage-frame" data-rotation={screen?.rotation ?? 0}>
<div style={frameStyle(screen?.rotation)} {...CQ_BOX} data-testid="signage-frame" data-rotation={screen?.rotation ?? 0}>
{playlist?.slides?.length
? <Player key={playlist.id} playlist={playlist} kept={kept} tz={tz} venue={data?.venue} now={now} rotation={screen?.rotation} />
: <Empty text={data ? t('signage.nothing') : t('signage.starting')} />}
+144
View File
@@ -0,0 +1,144 @@
// The older-browser layer (2026-11-21). It is loaded only by the legacy build, which a browser runs when it
// can't run the modern one (a TV's built-in browser from 2021–2023). Modern browsers never download it.
// It does three things:
// - swaps the stylesheet for its copy rewritten for older browsers (built by vite.config.js), e.g. `inset`
// becomes top/right/bottom/left;
// - gives the signage slides their container sizes (startCqShim: the fallback of cq() in @xenia/signage);
// - spaces flex items where the browser has no flex `gap` (Chrome before 84): it adds the gap as a margin
// on each item after the first.
// It imports nothing from the apps' code, so the modern build's files stay exactly as they were.
// the same check as @xenia/signage's cq()
const CQ_NATIVE = typeof CSS === 'undefined' || typeof CSS.supports !== 'function' || CSS.supports('width', '1cqmin')
/** What runs here, for the support page and the tests: <html data-compat="legacy css cq gap">. */
const active = ['legacy']
function swapStylesheets() {
for (const link of document.querySelectorAll('link[rel="stylesheet"]')) {
const href = link.getAttribute('href') || ''
if (!/\/assets\/[^/]+\.css$/.test(href) || href.endsWith('.compat.css')) continue
const copy = link.cloneNode()
copy.setAttribute('href', href.replace(/\.css$/, '.compat.css'))
// the old sheet stays until the new one has loaded: no unstyled flash
copy.onload = () => link.remove()
copy.onerror = () => copy.remove()
link.after(copy)
if (!active.includes('css')) active.push('css')
}
}
function hasFlexGap() {
const box = document.createElement('div')
box.style.cssText = 'display:flex;flex-direction:column;row-gap:1px;position:absolute;visibility:hidden'
box.appendChild(document.createElement('div'))
box.appendChild(document.createElement('div'))
document.body.appendChild(box)
const ok = box.scrollHeight === 1
box.remove()
return ok
}
function setVars(el, size) {
if (!size || el.nodeType !== 1) return
el.style.setProperty('--xcqw', `${size.w / 100}px`)
el.style.setProperty('--xcqh', `${size.h / 100}px`)
el.style.setProperty('--xcqmin', `${Math.min(size.w, size.h) / 100}px`)
}
/**
* Older browsers only: give the children of every container its size as --xcqw / --xcqh / --xcqmin.
* The variables are on the children, not on the container: the container's own sizes refer to the
* container around it, as with the real units. Nothing happens where the browser has container units.
* Returns a stop function.
*/
function startCqShim(root = document.body) {
if ( !root || typeof ResizeObserver === 'undefined' || typeof MutationObserver === 'undefined') return () => {}
const sizes = new WeakMap()
const watched = new WeakSet()
const ro = new ResizeObserver(entries => {
for (const e of entries) {
const size = { w: e.contentRect.width, h: e.contentRect.height }
sizes.set(e.target, size)
for (const child of e.target.children) setVars(child, size)
}
})
const watch = el => {
if (watched.has(el)) return
watched.add(el)
ro.observe(el)
}
const scan = node => {
if (node.nodeType !== 1) return
const parent = node.parentElement
if (parent && parent.hasAttribute('data-cq')) setVars(node, sizes.get(parent))
if (node.hasAttribute('data-cq')) watch(node)
for (const el of node.querySelectorAll('[data-cq]')) watch(el)
}
const mo = new MutationObserver(list => {
for (const m of list) for (const n of m.addedNodes) scan(n)
})
mo.observe(root, { childList: true, subtree: true })
scan(root)
return () => { mo.disconnect(); ro.disconnect() }
}
const SIDE = { row: 'marginLeft', 'row-reverse': 'marginRight', column: 'marginTop', 'column-reverse': 'marginBottom' }
function gapItems(el) {
const cs = getComputedStyle(el)
if (cs.display !== 'flex' && cs.display !== 'inline-flex') return
const dir = cs.flexDirection || 'row'
const gap = dir.startsWith('row') ? cs.columnGap : cs.rowGap
const side = SIDE[dir]
let first = true
for (const child of el.children) {
const ccs = getComputedStyle(child)
if (ccs.display === 'none' || ccs.position === 'absolute' || ccs.position === 'fixed') continue
const want = !first && gap && gap !== 'normal' && gap !== '0px' ? gap : ''
// an item's own margin stays (a zero one doesn't count); a write only when it changes, as a
// write fires the observer again
if (want && (child.dataset.xgap || !parseFloat(child.style[side]))) {
if (child.style[side] !== want) child.style[side] = want
child.dataset.xgap = '1'
} else if (!want && child.dataset.xgap) { child.style[side] = ''; delete child.dataset.xgap }
first = false
}
}
function startGapShim() {
const dirty = new Set()
let queued = false
const flush = () => {
queued = false
const els = [...dirty]
dirty.clear()
for (const el of els) if (el.isConnected) gapItems(el)
}
const mark = el => {
if (!el || el.nodeType !== 1) return
dirty.add(el)
if (!queued) { queued = true; requestAnimationFrame(flush) }
}
const markTree = el => {
if (el.nodeType !== 1) return
mark(el)
for (const d of el.querySelectorAll('*')) mark(d)
}
new MutationObserver(list => {
for (const m of list) {
if (m.type === 'attributes') { mark(m.target); mark(m.target.parentElement) }
else { mark(m.target); for (const n of m.addedNodes) markTree(n) }
}
}).observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['class', 'style'] })
markTree(document.body)
}
export function startCompat() {
// the tests force a part on a modern browser: window.__XENIA_COMPAT_FORCE = ['gap']
const force = window.__XENIA_COMPAT_FORCE || []
swapStylesheets()
if (!CQ_NATIVE) { startCqShim(); active.push('cq') }
if (force.includes('gap') || !hasFlexGap()) { startGapShim(); active.push('gap') }
document.documentElement.setAttribute('data-compat', active.join(' '))
}
+3
View File
@@ -8,6 +8,9 @@ import { applyTheme, getStoredTheme } from '@xenia/ui'
// New code uses the tokens only, so every theme works; a per-screen choice comes with each app.
applyTheme(getStoredTheme())
// The legacy build only (a TV's older browser): the compatibility layer. Modern browsers never load it.
if (import.meta.env.LEGACY) import('./compat.js').then(m => m.startCompat())
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
+33 -1
View File
@@ -1,10 +1,42 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import legacy from '@vitejs/plugin-legacy'
import { transform } from 'lightningcss'
const BACKEND = process.env.XENIA_BACKEND || 'http://localhost:8000'
// Older TVs (2026-11-21). Modern browsers get exactly the build they always had: Vite's own targets
// (Baseline widely available) for the code and the stylesheet. A browser that can't run it (the
// check is plugin-legacy's: import.meta.resolve, Chrome 105 / Firefox 106 / Safari 16.4) gets a second
// copy of the code, made for LEGACY_TARGETS with the missing pieces added. That copy also loads
// src/compat.js. Modern browsers never download any of it.
const MODERN = ['chrome111', 'edge111', 'firefox114', 'safari16.4', 'ios16.4']
const LEGACY_TARGETS = ['chrome >= 79', 'firefox >= 78', 'safari >= 13']
const COMPAT_CSS = { chrome: 79 << 16, firefox: 78 << 16, safari: 13 << 16 }
/** Each stylesheet again as <name>.compat.css, rewritten for the older browsers (compat.js swaps it in). */
function compatCss() {
return {
name: 'xenia:compat-css',
apply: 'build',
generateBundle(_opts, bundle) {
for (const [name, file] of Object.entries(bundle)) {
if (file.type !== 'asset' || !name.endsWith('.css') || name.endsWith('.compat.css') || name.includes('-legacy')) continue
const { code } = transform({ filename: name, code: Buffer.from(String(file.source)), minify: true, targets: COMPAT_CSS })
this.emitFile({ type: 'asset', fileName: name.replace(/\.css$/, '.compat.css'), source: code })
}
},
}
}
export default defineConfig({
plugins: [react()],
plugins: [
react(),
legacy({ targets: LEGACY_TARGETS, modernTargets: MODERN.map(t => t.replace(/(\D)(\d)/, '$1>=$2')).join(', ') }),
compatCss(),
],
// plugin-legacy would lower the shared stylesheet for everyone; it keeps Vite's own target instead
build: { cssTarget: MODERN },
// npm workspace (D-15): shared packages are imported from source; one React for all.
resolve: {
dedupe: ['react', 'react-dom'],