Skip to content

Conversation

@yrom
Copy link
Contributor

@yrom yrom commented Dec 27, 2025

Add SSR mode support (global flag __ANTV_INFOGRAPHIC_SSR__).

  • Use LinkeDOM for mocking DOM.
  • Add a cli package for render syntax to svg

Test:

npm run build:esm
cd cli
npm install
npm run example
# view output/02-list-with-icons.svg

Related #68

Non-Browser Rendering

import { renderToSVG } from '@antv/infographic/ssr';

const result = await renderToSVG({
  input: `
infographic list-row-simple-horizontal-arrow
data
  items:
    - label: Step 1
      desc: Start
    - label: Step 2
      desc: In Progress
    - label: Step 3
      desc: Complete
`,
});

console.log(result.svg);
console.log('Errors:', result.errors);
console.log('Warnings:', result.warnings);

With custom ResourceLoader:

import { renderToSVG } from '@antv/infographic/ssr';
import { registerResourceLoader, loadSVGResource, type ResourceLoader, type ResourceConfig } from '@antv/infographic';

const customLoader: ResourceLoader =  (async (config: ResourceConfig) => {
  const {data, scene} = config;
  //... build url
  const url = ...;
  return loadSVGResource(url);
}

registerResourceLoader(customLoader);

const result = await renderToSVG({ input: syntax });

CLI Package documents

cli/README.md

npx @antv/infographic-cli input.txt -o output.svg ( available on @antv/infographic-cli published)

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yrom, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the AntV Infographic library by introducing Server-Side Rendering (SSR) capabilities. This allows for the generation of SVG infographics in a Node.js environment, eliminating the need for a browser. A new command-line interface (CLI) tool is also provided, simplifying the process of converting infographic syntax into SVG files. The changes include robust DOM emulation using jsdom, intelligent resource preloading, and specific optimizations for server-side execution to ensure accurate and efficient rendering.

Highlights

  • Server-Side Rendering (SSR) Pipeline: Introduced a comprehensive SSR pipeline, allowing AntV Infographic to render SVG outputs in a Node.js environment without a browser, leveraging jsdom for DOM mocking.
  • New CLI Package: Added a new command-line interface (CLI) package, @antv/infographic-cli, which enables users to convert infographic syntax files directly into SVG images from the terminal.
  • Resource Preloading for SSR: Implemented a resource preloading mechanism for icons and illustrations, ensuring all necessary assets are fetched before rendering in the SSR environment, crucial for generating complete SVG outputs.
  • SSR-Specific Optimizations: Adjusted font loading, text measurement, and text content handling (innerText vs. textContent) to accommodate jsdom's limitations and optimize performance in a server-side context, controlled by a global __ANTV_INFOGRAPHIC_SSR__ flag.
  • ESM Import Fixes: Added a post-build script to automatically append .js extensions to relative imports in ESM build outputs, resolving module resolution issues in Node.js environments.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant new feature: Server-Side Rendering (SSR) capabilities and a command-line interface (CLI) for rendering infographics. The changes are extensive, including a new cli package, DOM mocking with jsdom, and adjustments to the rendering pipeline to support a non-browser environment. The implementation is generally solid, with good use of jsdom for environment setup and a well-structured CLI.

I've identified a critical issue in the new SSR tests where asynchronous functions are not being awaited, which will cause tests to pass incorrectly. I've also found several medium-severity issues, including dead code, unnecessary package contents, and opportunities to improve type safety and test quality. Addressing these points will improve the robustness and maintainability of this new functionality.

@codecov-commenter
Copy link

codecov-commenter commented Dec 27, 2025

Codecov Report

❌ Patch coverage is 64.77612% with 118 lines in your changes missing coverage. Please review.
✅ Project coverage is 35.71%. Comparing base (6b6b431) to head (bd5539f).

Files with missing lines Patch % Lines
src/ssr/renderer.ts 68.29% 39 Missing ⚠️
src/utils/text.ts 34.61% 34 Missing ⚠️
src/renderer/renderer.ts 30.43% 16 Missing ⚠️
src/ssr/dom-shim.ts 85.45% 16 Missing ⚠️
src/resource/loader.ts 0.00% 13 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #104      +/-   ##
==========================================
+ Coverage   31.37%   35.71%   +4.33%     
==========================================
  Files         314      317       +3     
  Lines       23149    23454     +305     
  Branches     1257     1495     +238     
==========================================
+ Hits         7264     8377    +1113     
+ Misses      15882    15073     -809     
- Partials        3        4       +1     
Flag Coverage Δ
infographic 35.71% <64.77%> (+4.33%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/index.ts 100.00% <100.00%> (ø)
src/renderer/fonts/loader.ts 20.63% <100.00%> (+6.84%) ⬆️
src/renderer/fonts/registry.ts 44.82% <ø> (ø)
src/runtime/Infographic.tsx 65.58% <100.00%> (+47.40%) ⬆️
src/ssr/index.ts 100.00% <100.00%> (ø)
src/utils/data.ts 100.00% <100.00%> (ø)
src/utils/measure-text.ts 32.75% <100.00%> (ø)
src/utils/recognizer.ts 73.80% <100.00%> (+1.19%) ⬆️
src/utils/svg.ts 100.00% <ø> (ø)
src/resource/loader.ts 6.06% <0.00%> (-0.84%) ⬇️
... and 4 more

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Aarebecca
Copy link
Contributor

@yrom Thanks for your contribution. We’ll review this PR asap on workdays.

@Aarebecca
Copy link
Contributor

@yrom Before initiating the human code review, it is recommended to first resolve the issues raised by Gemini.

@@ -0,0 +1,11 @@
infographic list-row-simple-horizontal-arrow
Copy link
Member

@hustcc hustcc Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这些作为 infographic 的单元测试的输入,测试 ssr 的 api,然后把输出的 svg 也存到 git commit 中,单测断言做 svg string 的匹配。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If do that, the unit tests will be unstable, since it affected by rendering logic changes.

@@ -0,0 +1,70 @@
#!/usr/bin/env node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放到单测中,就不需要这个文件了。

@Aarebecca
Copy link
Contributor

@copilot Code Review

@hustcc
Copy link
Member

hustcc commented Dec 29, 2025

@yrom 挺牛的 PR,增加了两个大能力,可以在 PR 描述中加上描述:

  1. ssr 的 API 使用,便于后续的功能封装,比如 MCP Server 可以用得上
  2. cli 的大概使用文档

这两个功能,后续都需要在官网文档、Readme 中展示出来。

@Aarebecca Aarebecca changed the title Implement SSR rendering pipeline feat: Implement SSR rendering pipeline Dec 29, 2025
@yrom

This comment was marked as off-topic.

@gemini-code-assist

This comment was marked as off-topic.

@yrom
Copy link
Contributor Author

yrom commented Dec 31, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant new feature: Server-Side Rendering (SSR) for infographics. This is achieved by adding a new ssr module that uses linkedom to create a mock DOM environment in Node.js. A key part of this is a sophisticated DOM shim that correctly handles SVG serialization and text measurement on the server. The changes also include a new @antv/infographic-cli package for command-line rendering, complete with its own tests and documentation. The core library is updated to support the SSR mode, for instance by skipping font loading on the server and using textContent instead of innerText. Overall, this is a well-structured and comprehensive implementation of SSR support. My review includes a few minor suggestions for improving code clarity and maintainability.

Comment on lines +183 to +185
const factor = parseFloat(lineHeightStyle);
lineHeight = isNaN(factor) ? fontSize * 1.4 : (factor < 4 ? fontSize * factor : factor);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for calculating lineHeight uses magic numbers (1.4 and 4). It appears to differentiate between a unitless multiplier and an absolute pixel value. To improve readability and maintainability, please add a comment explaining this heuristic.

Comment on lines +9 to +13
export interface SSRRenderOptions {
/** Input: Antv Infographic Syntax string */
input: string;
options?: Partial<InfographicOptions>;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 renderToSVG API 参数可以好好看看 @Aarebecca

  • 参数名称合理不
  • 后面再扩展支持加哪里,比如为了更加的精度,node 环境用 canvas 测评文本宽度支持,怎么传下去

@@ -0,0 +1,3 @@
// Re-export SSR functionality from main package
export { renderToSVG, setupDOM, teardownDOM, isSSR } from '@antv/infographic/ssr';
export type { SSRRenderOptions, SSRRenderResult } from '@antv/infographic/ssr';
Copy link
Member

@lvisei lvisei Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抛一下我的想法吧,保持架构上和合理性。

  • @antv/infographic 代码可以在浏览器环境和 node 环境跑起来(出 svg 的代码),核心代码兼容,可两端跑。
  • @antv/infographic 里不应该依赖 jsdom 和 linkdom svgo 这些,这些是为了服务端场景用的。
  • cli 就是为 node 环境用的,定位不只是 cli,更像是 @antv/infographic-srr,里面可以包node环境的布丁(linkdom,svgo等)。
  • 用户可以用 @antv/infographic 封装等轻量 renderSVG API,在浏览器环境直接可运行。如果在 node 环境用这个,需要主动给 global 加 dom 环境变量。
  • 用户也可以用我们在 node 环境封装好的 cli 这个包,直接就可以用。

建议 cli 这个包改名字,叫 @antv/infographic-srr,提供以下能力

  • 有 cli 命令可用
  • 用户可直接导入 renderSvg 方法,在 node 环境直接可以用跑起来
  • 可提供 svg 压缩,可以提供渲染成图片等,
  • 也可以提供一些语法糖 exportToFile、toBuffer,这样在服务端场景更加实用一些,才很贴合
  • 风格,可以参考 @antv/gpt-vis-ssr@antv/g6-ssr

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得 G2、G6 单独一个 ssr 包不是因为 G2、G6 不支持 ssr,而是用起来麻烦。

yrom added 2 commits December 31, 2025 14:11
remove custom XMLSerializer implementation in favor of native outerHTML
move svgo optimization to CLI level with option
- Add fast fail for syntax errors
- Validate template existence
@Aarebecca
Copy link
Contributor

这个 PR 的设计还存在诸多存在争议的点,为了避免后续对用户造成疑惑,所以我们需要花费更多的时间进行讨论和明确设计

@Aarebecca Aarebecca marked this pull request as draft December 31, 2025 07:16
@yrom
Copy link
Contributor Author

yrom commented Dec 31, 2025

To keep things manageable, I intentionally started simple: SSR just works out of the box, backed by a minimal CLI. IMHO, we can gradually add more advanced features later as needed.

@hustcc
Copy link
Member

hustcc commented Dec 31, 2025

cli 其实是 ssr 能力的一种透出形态。

我觉得可以先提供 ssr 的 api,再来做 cli 相关的,而且我更建议 cli 作为项目周边,不放在这个仓库中,可以放到个人仓库,然后官网收录,因为这类封装可以有更多的形态。

然后 ssr 的 api 有多种设计方式。

  1. 在现有 Infographic 类上增加 renderToSVGString 的类方法
  2. 按照现在的方式,npm 新开一个目录来做 api

我其实更建议第一种,可以类似于 ReactDOM,这个可以讨论下。@Aarebecca @yrom

@hustcc
Copy link
Member

hustcc commented Dec 31, 2025

对于单测,现在 cli 中的单测应该放到 infographic 主包中,测试 ssr 的能力。

cli 测试可以简化一下。

@hustcc
Copy link
Member

hustcc commented Jan 7, 2026

@yrom 有啥进展不,要不加一个微信聊一下,我的微信在我 GitHub 主页有。

@yrom
Copy link
Contributor Author

yrom commented Jan 8, 2026

@hustcc 🈚️ 我没明白你们具体改进方向,另外这几天也没时间😂

@Aarebecca
Copy link
Contributor

@yrom 我把 PR 先合并到开发分支了,后续我会进行处理

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.

5 participants