fix(csp): 去掉 index.html 里的内联 style/script —— 插件样式失效的真因(同 v3.26.9)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m8s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m8s
现象:风格库 / 浏览器 / 提示词库的面板与画布上的样式节点全部丢样式(DOM 正常、宿主外链
style.css 正常,只有插件动态注入的 <style> 不生效)。
根因:Tauri 会为 index.html 里的内联块加 CSP nonce;CSP3 规定 nonce 一出现 'unsafe-inline'
即被忽略。插件样式是运行时 document.createElement('style') 注入的、没有 nonce,于是被整段丢弃。
这正是 v3.26.9 修过、v3.26.10 回退掉的那条链(当时靠给动态 style 补 nonce 解决)。
我上两版把启动屏做成了内联 <style>+<script>,等于把这个坑重新踩了一遍。
- scripts/boot-splash.js:只导出标记(复用宿主已有的 .iwb-splash* 类),不再有内联样式/脚本
- scripts/build.js:index.html 只内联启动屏标记,样式走外链 style.css
- src/ui-splash.js:React 启动屏挂载时直接移除 #iwb-boot-static 节点
- scripts/check-dist.js:script 数回到 3,并新增断言「index.html 不得含内联 <style>/<script>」
并写明原因,防止下次再踩
This commit is contained in:
1 parent
61399b1b4c
commit
9210bc3d0b
5 files changed
+18
-79
No files matched your search
+6
-71
@@ -1,73 +1,8 @@
|
||||
'use strict'
|
||||
// 静态启动屏(v3.26.109):内联进 index.html,不依赖任何脚本就能出画面。
|
||||
// 为什么:资料目录放在局域网时,app.js 要等「存储装载」完成才渲染 React,
|
||||
// 那段时间原本是纯白板(实测 15s)。
|
||||
// 静态启动屏(v3.26.109):内联进 index.html,浏览器解析完 HTML 就有画面,消掉局域网资料目录的启动白屏。
|
||||
// 只用宿主样式表里已有的 .iwb-splash* 类 —— 不能在 index.html 里写内联 <style>/<script>:
|
||||
// Tauri 会给内联块加 CSP nonce,CSP3 下 nonce 一出现 'unsafe-inline' 就失效,
|
||||
// 插件动态注入的 <style> 会被整段丢弃(v3.26.9 踩过同一个坑)。
|
||||
const markup = '<div id="iwb-boot-static" class="iwb-splash" role="status" aria-live="polite">\n <div class="iwb-splash-logo"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" width="100%" height="100%"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><circle cx="17.5" cy="17.5" r="3.5"/></svg></div>\n <div class="iwb-splash-title">生图工作台</div>\n <div class="iwb-splash-bar is-indeterminate"><div class="iwb-splash-bar-fill"></div></div>\n <div class="iwb-splash-meta"><span class="iwb-splash-sub">正在启动…</span><span class="iwb-splash-pct"></span></div>\n</div>'
|
||||
|
||||
const css = `<style>
|
||||
#iwb-boot-static{position:fixed;inset:0;z-index:2147483000;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:16px;background:#f6f6f7;color:#1c1d1f;font-family:-apple-system,BlinkMacSystemFont,"PingFang SC","Microsoft YaHei",sans-serif;transition:opacity .45s ease}
|
||||
#iwb-boot-static.iwb-boot-out{opacity:0;pointer-events:none}
|
||||
#iwb-boot-static .bs-logo{width:54px;height:54px;color:#575d69;animation:bs-bob 1.2s ease-in-out infinite}
|
||||
#iwb-boot-static .bs-title{font-size:18px;font-weight:700;letter-spacing:1px}
|
||||
#iwb-boot-static .bs-bar{width:300px;height:6px;border-radius:999px;background:#e8e9ec;border:1px solid #d8dade;overflow:hidden;position:relative}
|
||||
#iwb-boot-static .bs-fill{position:absolute;left:0;top:0;bottom:0;width:8%;border-radius:999px;background:linear-gradient(90deg,#6b7280,#575d69);transition:width .32s cubic-bezier(.2,.7,.3,1)}
|
||||
#iwb-boot-static .bs-fill::after{content:"";position:absolute;inset:0;transform:translateX(-100%);background:linear-gradient(90deg,transparent,#fff,transparent);animation:bs-sheen 1.5s ease-in-out infinite}
|
||||
#iwb-boot-static .bs-meta{width:300px;display:flex;align-items:baseline;justify-content:space-between;gap:12px;font-size:12px;color:#5c6068}
|
||||
#iwb-boot-static .bs-pct{font-variant-numeric:tabular-nums;font-weight:600;color:#1c1d1f}
|
||||
@keyframes bs-bob{0%,100%{transform:translateY(0)}50%{transform:translateY(-9px)}}
|
||||
@keyframes bs-sheen{0%{transform:translateX(-100%)}100%{transform:translateX(100%)}}
|
||||
@media (prefers-color-scheme:dark){
|
||||
#iwb-boot-static{background:#161a19;color:#e9eaea}
|
||||
#iwb-boot-static .bs-logo{color:#c9cbcd}
|
||||
#iwb-boot-static .bs-bar{background:#23282a;border-color:#2f3638}
|
||||
#iwb-boot-static .bs-fill{background:linear-gradient(90deg,#e8e8e8,#c9cbcd)}
|
||||
#iwb-boot-static .bs-meta{color:#a8adb0}
|
||||
#iwb-boot-static .bs-pct{color:#e9eaea}
|
||||
}
|
||||
@media (prefers-reduced-motion:reduce){#iwb-boot-static .bs-logo,#iwb-boot-static .bs-fill::after{animation:none}}
|
||||
</style>`
|
||||
|
||||
const markup = `<div id="iwb-boot-static" role="status" aria-live="polite">
|
||||
<svg class="bs-logo" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><circle cx="17.5" cy="17.5" r="3.5"/></svg>
|
||||
<div class="bs-title">生图工作台</div>
|
||||
<div class="bs-bar"><div class="bs-fill" id="bs-fill"></div></div>
|
||||
<div class="bs-meta"><span id="bs-sub">正在启动…</span><span class="bs-pct" id="bs-pct"></span></div>
|
||||
</div>`
|
||||
|
||||
// 轮询进度总线:真实百分比(IWBBootProgress 就绪后),之前显示已等待秒数
|
||||
const script = `<script>
|
||||
(function () {
|
||||
var t0 = Date.now()
|
||||
var box = document.getElementById('iwb-boot-static')
|
||||
var fill = document.getElementById('bs-fill')
|
||||
var sub = document.getElementById('bs-sub')
|
||||
var pct = document.getElementById('bs-pct')
|
||||
var lastPct = -1
|
||||
function hide () {
|
||||
if (!box) return
|
||||
box.className = 'iwb-boot-out'
|
||||
setTimeout(function () { if (box && box.parentNode) box.parentNode.removeChild(box) }, 500)
|
||||
}
|
||||
window.__iwbHideBootStatic = hide
|
||||
function apply () {
|
||||
if (!box) return
|
||||
var bp = window.IWBBootProgress
|
||||
var s = bp && bp.snapshot ? bp.snapshot() : null
|
||||
if (s) {
|
||||
if (!s.indeterminate && s.pct !== lastPct) {
|
||||
lastPct = s.pct
|
||||
fill.style.width = Math.max(8, s.pct) + '%'
|
||||
pct.textContent = s.pct + '%'
|
||||
}
|
||||
sub.textContent = s.detail ? (s.label + ' · ' + s.detail) : s.label
|
||||
if (s.done) { hide(); return }
|
||||
} else {
|
||||
var sec = Math.floor((Date.now() - t0) / 1000)
|
||||
sub.textContent = '正在启动…' + (sec >= 2 ? ' ' + sec + 's' : '')
|
||||
}
|
||||
requestAnimationFrame(apply)
|
||||
}
|
||||
requestAnimationFrame(apply)
|
||||
})()
|
||||
</script>`
|
||||
|
||||
module.exports = { css, markup, script }
|
||||
module.exports = { markup }
|
||||
+4
-4
@@ -240,14 +240,14 @@ const html = [
|
||||
'<title>无限画布</title>',
|
||||
'<meta name="theme-color" content="#2563eb">',
|
||||
'<link rel="stylesheet" href="style.css?v=' + version + '">',
|
||||
// v3.26.109:静态加载屏内联在 index.html 里。资料目录在局域网时,app.js 要等存储装载完
|
||||
// 才渲染 React,那段时间原本是纯白板(实测 15s);这段 HTML/CSS 不依赖任何脚本就能出画面。
|
||||
bootSplash.css,
|
||||
// v3.26.109:静态启动屏标记内联在 index.html(样式来自外链 style.css)。
|
||||
// 资料目录在局域网时 app.js 要等存储装载完才渲染 React,那段时间原本是纯白板(实测 15s)。
|
||||
// 注意:这里只能放标记,不能放内联 <style>/<script> —— Tauri 会给内联块加 nonce,
|
||||
// CSP3 下 'unsafe-inline' 随之失效,插件动态注入的样式会被整段丢弃(v3.26.9 事故)。
|
||||
'</head>',
|
||||
'<body>',
|
||||
'<div id="root"></div>',
|
||||
bootSplash.markup,
|
||||
bootSplash.script,
|
||||
'<script src="vendor/react.production.min.js"></script>',
|
||||
'<script src="vendor/react-dom.production.min.js"></script>',
|
||||
'<script src="app.js?v=' + version + '"></script>',
|
||||
|
||||
@@ -41,10 +41,15 @@ if (!fs.existsSync(htmlPath)) fail('dist/index.html 不存在')
|
||||
const html = fs.readFileSync(htmlPath, 'utf8')
|
||||
if (!html.includes('app.js?v=')) fail('dist/index.html 缺少 app.js?v=')
|
||||
const scriptCount = (html.match(/<script/g) || []).length
|
||||
if (scriptCount !== 4) fail('dist/index.html <script 数量应为 4(React/ReactDOM/app.js + 内联启动屏),实际 ' + scriptCount)
|
||||
if (scriptCount !== 3) fail('dist/index.html <script 数量应为 3,实际 ' + scriptCount)
|
||||
// v3.26.109:index.html 里不允许出现内联 <style>/<script> —— Tauri 会给它们加 CSP nonce,
|
||||
// CSP3 下 nonce 一出现 'unsafe-inline' 就失效,插件动态注入的 <style> 会被整段丢弃(v3.26.9 事故)。
|
||||
const inlineStyle = /<style[^>]*>[\s\S]*?<\/style>/i.test(html)
|
||||
const inlineScript = /<script(?![^>]*\bsrc=)[^>]*>[\s\S]*?<\/script>/i.test(html)
|
||||
if (inlineStyle || inlineScript) fail('dist/index.html 含内联 <style>/<script>:会触发 CSP nonce,插件样式将全部失效')
|
||||
if (!html.includes('iwb-boot-static')) fail('dist/index.html 缺少静态启动屏(局域网资料目录启动时的白屏兜底)')
|
||||
if (html.includes('src/')) fail('dist/index.html 仍含 src/ 引用')
|
||||
pass('dist/index.html 含 app.js?v=、静态启动屏,<script 数量为 4,无 src/ 引用')
|
||||
pass('dist/index.html 含 app.js?v=、静态启动屏标记、无内联 style/script,无 src/ 引用')
|
||||
|
||||
// 3. dist/app.js 含无引号标记子串,证明内联压缩 worker 字面量在 minify 后仍存活
|
||||
if (!app.includes('self.onmessage=async function(ev)')) {
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
const [s, setS] = React.useState(() => (bp ? bp.snapshot() : { pct: 0, label: '正在启动…', detail: '', indeterminate: true }))
|
||||
React.useEffect(() => {
|
||||
// React 启动屏一挂载就顶掉 index.html 里的静态启动屏(同款外观,切换无感)
|
||||
try { if (typeof window.__iwbHideBootStatic === 'function') window.__iwbHideBootStatic() } catch (e) { /* ignore */ }
|
||||
try { const n = document.getElementById('iwb-boot-static'); if (n && n.parentNode) n.parentNode.removeChild(n) } catch (e) { /* ignore */ }
|
||||
const un = bp ? bp.subscribe(setS) : NOOP
|
||||
return function () { try { un() } catch (e) { /* ignore */ } }
|
||||
}, [])
|
||||
|
||||
Vendored
-1
@@ -55,7 +55,6 @@ interface Window {
|
||||
IWBBootLoad?: any
|
||||
IWBBootProgress?: any // 启动加载进度总线(v3.26.109)
|
||||
IWBUISplash?: any // 启动加载屏(v3.26.109)
|
||||
__iwbHideBootStatic?: any // index.html 静态启动屏的隐藏钩子(v3.26.109)
|
||||
IWBCanvasNodes?: any
|
||||
IWBUIWorkspaceTabs?: any
|
||||
IWBUICanvasTopBar?: any
|
||||
|
||||
Reference in new issue
Block a user