@xingwangzhe/stalux 1.15.0 → 1.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xingwangzhe/stalux",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "description": "A powerful, modern Astro blog theme — use as template or install as plugin",
5
5
  "keywords": [
6
6
  "astro",
@@ -45,7 +45,10 @@ const bgUrls = Object.values(bgModules)
45
45
  const nb = parseInt(b.match(/pattern-(\d+)/)?.[1] || "0", 10);
46
46
  return na - nb;
47
47
  });
48
- const randomIndex = bgUrls.length > 0 ? Math.floor(Math.random() * bgUrls.length) : 0;
48
+ // 背景索引固定为 0(确定性):保证 define:vars 内联脚本在构建间稳定,
49
+ // 使 Astro 增量构建的依赖图 hash 可复用。真正的随机背景由客户端
50
+ // background.ts 在运行时为每个访问者挑选。
51
+ const randomIndex = 0;
49
52
  const initialBg = bgUrls[randomIndex] || "";
50
53
  const llmPromoteRaw = promoteConfig?.llm_promote ?? "";
51
54
  const llmPromote = llmPromoteRaw
@@ -72,9 +75,8 @@ const llmPromote = llmPromoteRaw
72
75
  <!-- Per-route font subset (generated at build time) -->
73
76
  <link rel="stylesheet" href="/fonts/common.css" />
74
77
  {fontSubsetUrl && <link rel="stylesheet" href={fontSubsetUrl} />}
75
- <script define:vars={{ bgUrls, randomIndex }}>
78
+ <script define:vars={{ bgUrls }}>
76
79
  window.__STALUX_BG_URLS__ = bgUrls;
77
- window.__STALUX_BG_INDEX__ = randomIndex;
78
80
  </script>
79
81
  <script>
80
82
  import "../scripts/background.ts";
@@ -1,17 +1,16 @@
1
1
  /**
2
- * 双层随机背景 — 每页服务端随机选图直接渲染,不做 VT crossfade
2
+ * 双层随机背景 — 客户端运行时随机选图,不做 VT crossfade
3
3
  * (去掉 view-transition-name 避免 SVG 全屏瓦片在过渡期间双倍 CPU 光栅化)
4
+ * 背景随机从服务端移到客户端,保证构建产物确定性,
5
+ * 使 Astro 增量构建的依赖图 hash 可跨构建复用。
4
6
  */
5
7
  declare global {
6
8
  interface Window {
7
9
  __STALUX_BG_URLS__?: string[];
8
- __STALUX_BG_INDEX__?: number;
9
10
  }
10
11
  }
11
12
 
12
13
  const backgroundImages: string[] = window.__STALUX_BG_URLS__ || [];
13
- const initialIndexFromSSR =
14
- typeof window.__STALUX_BG_INDEX__ === "number" ? window.__STALUX_BG_INDEX__ : -1;
15
14
 
16
15
  function getLayerEl(layer: "a" | "b"): HTMLElement | null {
17
16
  return document.querySelector(`.bg-layer.bg-${layer}`);
@@ -23,25 +22,18 @@ function setLayerOpacity(layer: "a" | "b", opacity: number): void {
23
22
  }
24
23
 
25
24
  function initBackground(): void {
26
- if (initialIndexFromSSR >= 0 && backgroundImages.length > 0) {
27
- // 服务端已预填充 bg-a,只需确保状态一致
28
- document.body.dataset.staluxBgIndex = String(initialIndexFromSSR);
29
- document.body.dataset.staluxBgLayer = "a";
30
- setLayerOpacity("a", 1);
31
- setLayerOpacity("b", 0);
32
- } else {
33
- // Fallback:客户端自行初始化
34
- const index = Math.floor(Math.random() * backgroundImages.length);
35
- const layer: "a" | "b" = "a";
36
- if (backgroundImages.length > 0) {
37
- const el = getLayerEl(layer);
38
- if (el) el.style.backgroundImage = `url('${backgroundImages[index]}')`;
39
- }
40
- setLayerOpacity(layer, 1);
41
- setLayerOpacity("b", 0);
42
- document.body.dataset.staluxBgIndex = String(index);
43
- document.body.dataset.staluxBgLayer = layer;
25
+ // 背景随机在客户端运行时执行,保证构建产物确定性
26
+ // (SSR 固定索引 → 内联脚本稳定 → Astro 增量构建依赖图可复用)
27
+ const index = Math.floor(Math.random() * backgroundImages.length);
28
+ const layer: "a" | "b" = "a";
29
+ if (backgroundImages.length > 0) {
30
+ const el = getLayerEl(layer);
31
+ if (el) el.style.backgroundImage = `url('${backgroundImages[index]}')`;
44
32
  }
33
+ setLayerOpacity(layer, 1);
34
+ setLayerOpacity("b", 0);
35
+ document.body.dataset.staluxBgIndex = String(index);
36
+ document.body.dataset.staluxBgLayer = layer;
45
37
  }
46
38
 
47
39
  // 初始化(首次加载 + VT 软导航都走这里)