Skip to content

fix(analytics): SSR 页面百度统计注入时序修复#55

Merged
calderbuild merged 1 commit intomainfrom
fix/baidu-tongji-ssr-injection
Feb 18, 2026
Merged

fix(analytics): SSR 页面百度统计注入时序修复#55
calderbuild merged 1 commit intomainfrom
fix/baidu-tongji-ssr-injection

Conversation

@calderbuild
Copy link
Owner

Summary

  • SSR 页面(首页、城市页、FAQ 等)百度统计脚本未注入的 bug 修复
  • 根因:templates.env.globals["baidu_tongji_id"] 在模块导入时一次性读取 os.getenv(),如果此时环境变量未就绪则永远为空字符串
  • 修复:改为每次请求时通过 _common_context() 动态读取,与 /api/config/analytics 端点行为一致

验证

  • 工具页(通过 API fetch 动态加载)百度统计已正常工作
  • SSR 页面部署后需用 Playwright 验证百度统计脚本出现在 HTML 中

… time

The Jinja2 global was set once at module import time via
templates.env.globals, which captured an empty string if the
env var wasn't available during import. Changed to pass
baidu_tongji_id through template context at request time,
matching how the /api/config/analytics endpoint works.
@calderbuild calderbuild merged commit e6d982d into main Feb 18, 2026
7 of 9 checks passed
@calderbuild calderbuild deleted the fix/baidu-tongji-ssr-injection branch February 18, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant