Skip to content

ReferenceError: window is not defined during SSR for LocalSearch #154

@goshander

Description

@goshander

Describe the bug
When compile the project in a static mode with local search enable, I encounter the following error:

ReferenceError: window is not defined
    at LocalSearchProvider.get (node_modules/@diplodoc/client/build/server/app.js:462:9)
    at LocalSearchProvider.link (node_modules/@diplodoc/client/build/server/app.js:403:30)
    ...

YFM config:

allowHTML: true
staticContent: true
addSystemMeta: true
assetsPublicPath: /docs
search:
  provider: local
  confidence: phrased
  tolerance: 2

Problematic code
The error is caused by direct usage of the window object in the following lines:

Expected behavior
The code should not access the window object during SSR, as it is only available in the browser.

Possible solution
Add a check to ensure that the code referencing window only runs in the browser environment, e.g.:

if (typeof window !== 'undefined') {
  // browser-specific code
}

Reproduce at datalens docs repo:
datalens-tech/docs#13

Run command: npm run dev

Metadata

Metadata

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