Skip to content

Can't use generateHtmlFromNodes correctly for SSR #3616

@eashish93

Description

@eashish93

When using generateHtmlFromNodes, I encountered an error:
To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.
which is fine as JSDOM is required, but this error doesn't mention which variables this function needs, so I dig into the code and found that we need to declare something like this first to make it work:

const { JSDOM } = jsdom;

const window = new JSDOM(`...`).window;
global.document = window.document;
// @ts-expect-error
global.window = window;

// Sometimes `global.navigator` also needs to be defined with `global.window `

// @ts-expect-error
global.DocumentFragment = function() { return JSDOM.fragment(``) }

This function is checking for both document and window. This should only check for document and DocumentFragment only, as these are the only methods generateHtmlFromNodes using.

Lexical version: 0.7.4

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions