Skip to content

X6并不支持服务端渲染 #4095

@kingyue737

Description

@kingyue737

Describe the bug

初次尝试X6,在SSR项目中引入X6,会报错,如:

Element is not defined
document is not defined

目前我发现的在@antv/x6-commonpolyfill.tsprefix.ts中,使用了很多documentElement,这俩只有在browser端才有,服务端Node.js是没有的,所以就出错了。
比较奇怪的是js的polyfill和css的prefix通常应该由下游用户自主决定,比如根据想要兼容的浏览器版本使用webpack/vite来transpile code, 然后用babel来加垫片,postcss来处理css兼容问题。直接在库里处理浏览器兼容问题不是良好的实践,会引入很多不需要代码增加体积。

另外一个比较大的问题是没有有效的ESM入口,也和服务端渲染有关,Node.js不认package.json中的module,必须要用exports表明ESM的入口。参考这两个网站
https://publint.dev/@antv/x6@2.16.1
https://arethetypeswrong.github.io/?p=%40antv%2Fx6%402.16.1
可以看到X6只有CJS入口,没有能被识别的ESM入口。

正确的写法应当如下:

{
  "name":"@antv/x6",
  "exports": {
    ".": {
      "import": "./es/index.mjs",   // IMPORTANT, use ".mjs" instead of ".js"
      "require": "./lib/index.js"
    }
  },
}

或指定type: "module"

{
  "name":"@antv/x6",
  "type":"module",
  "exports": {
    ".": {
      "import": "./es/index.js",
      "require": "./lib/index.cjs"  // IMPORTANT, use ".cjs" instead of ".js"
    }
  },
}

参考:
https://publint.dev/rules#has_module_but_no_exports
https://nuxt.com/docs/guide/concepts/esm#what-are-valid-imports-in-a-nodejs-context

Your Example Website or App

https://stackblitz.com/edit/github-h7fojp

Steps to Reproduce the Bug or Issue

在SSR项目中引入X6

Expected behavior

根据README,X6应该是支持服务端渲染的,不应该有这样的问题。

X6/README.md

Line 32 in f2cae96

- 支持服务端渲染。

Screenshots or Videos

No response

Platform

  • OS: Windows, Linux
  • Browser: Chrome
  • Version: 2.16.1
  • Node: 20.10.0

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions