diplodoc-platform / diplodoc-platform/client

ReferenceError: window is not defined during SSR for LocalSearch

Open
#154 0 comments 0 reactions 1 assignee Claimed by @martyanovandrey View on GitHub
Dominant language
TypeScript
Stars
3
Forks
7
Avg merge
21h 21m
Merged PRs (30d)
17

Description

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

```js
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:

```yaml
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:
- https://github.com/diplodoc-platform/client/blame/master/src/components/Search/provider/index.ts#L52
- https://github.com/diplodoc-platform/client/blame/master/src/components/Search/provider/index.ts#L56

**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.:

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

Reproduce at datalens docs repo:
https://github.com/datalens-tech/docs/pull/13

Run command: `npm run dev`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.