Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window is not defined error #352

Closed
vojtechruz opened this issue Jun 6, 2018 · 11 comments
Closed

window is not defined error #352

vojtechruz opened this issue Jun 6, 2018 · 11 comments

Comments

@vojtechruz
Copy link

vojtechruz commented Jun 6, 2018

Do you want to request a feature or report a bug?
bug

What is the current behavior?
When I deploy my GatsbyJs application to netlify, and I import Docs Search I get:


11:47:21 AM: See our docs page on debugging HTML builds for help https://goo.gl/yL9lND
11:47:21 AM:   3 | (function(global, factory) {
11:47:21 AM:   4 |   module.exports = factory(global);
11:47:21 AM: > 5 | }(/* this ##### UPDATED: here we want to use window/global instead of this which is the current file context ##### */ window, function(window) {
11:47:21 AM:     |  ^
11:47:21 AM:   6 |   var Zepto = (function() {
11:47:21 AM:   7 |   var undefined, key, $, classList, emptyArray = [], concat = emptyArray.concat, filter = emptyArray.filter, slice = emptyArray.slice,
11:47:21 AM:   8 |     document = window.document,
11:47:21 AM: 
11:47:21 AM:   WebpackError: window is not defined
11:47:21 AM:   
11:47:21 AM:   - zepto.js:5 Object.module.exports.element
11:47:21 AM:     ~/autocomplete.js/zepto.js:5:2
11:47:21 AM:   
11:47:21 AM:   - index.js:4 Object.<anonymous>
11:47:21 AM:     ~/autocomplete.js/src/standalone/index.js:4:1
11:47:21 AM:   
11:47:21 AM:   - index.js:3 Object.<anonymous>
11:47:21 AM:     ~/autocomplete.js/index.js:3:1
11:47:21 AM:   
11:47:21 AM:   - DocSearch.js:19 Object.<anonymous>
11:47:21 AM:     ~/docsearch.js/dist/npm/src/lib/DocSearch.js:19:1
11:47:22 AM:   
11:47:22 AM:   - main.js:11 Object._bind
11:47:22 AM:     ~/docsearch.js/dist/npm/src/lib/main.js:11:1

}(/* this ##### UPDATED: here we want to use window/global instead of this which is the current file context ##### */ window, function(window) {  

My code is here:
https://github.com/vojtechruz/vojtechruzicka-blog/blob/master/src/components/SearchBox.js

Full error log here:
https://app.netlify.com/sites/vojtechruzicka/deploys/5b17acd1c965921b0450d558

@Haroenv
Copy link
Contributor

Haroenv commented Jun 6, 2018

Could you use import() in componentDidMount instead?

see https://github.com/gatsbyjs/gatsby/blob/master/www/src/components/search-form.js#L259-L289 and https://github.com/gatsbyjs/gatsby/blob/d521c19d04f8ba029f3998d104288eb92846d4ed/www/src/html.js#L77

A more easy to read example of that is the react docs:

https://github.com/reactjs/reactjs.org/blob/master/src/components/LayoutHeader/DocSearch.js in combination with https://github.com/reactjs/reactjs.org/blob/ae29fa0491e5b24b68117a45c6f19716c2f31540/src/html.js#L19

The reason is that docsearch is best lazy loaded since it's not made for a standard module system directly with SSR.

Does that make sense?

@vojtechruz
Copy link
Author

vojtechruz commented Jun 6, 2018

Thank you for the quick reply.

Since import can be only on the root level of file and not in methods, does it mean I can only use the script included from cdn as script tag in my html? That SSR and modules don't work together here? Are there plans for the future to enable this?

Imean I can call docsearch() like this only if there is window:

componentDidMount() {
        if(window) {
            docsearch({
                apiKey: '467e44d2366ac0de7f99703feb47c8b6',
                indexName: 'vojtechruzicka',
                inputSelector: '.search-box',
                debug: false,
            });
        }
    }

but looks like just importing docsearch throws the exception.

import docsearch from "docsearch.js";

@Haroenv
Copy link
Contributor

Haroenv commented Jun 6, 2018

class App extends Component {
  componentDidMount() {
    if (window) {
      import("docsearch.js").then(({ default: docsearch }) => {
        docsearch({
          apiKey: "467e44d2366ac0de7f99703feb47c8b6",
          indexName: "vojtechruzicka",
          inputSelector: ".search-box",
          debug: false
        });
      });
    }
  }
}

This is how I meant it. The other option is to use the CDN version as done by react and Gatsby themselves

@vojtechruz
Copy link
Author

Thanks, it does work now, you can close 👍

@Haroenv Haroenv closed this as completed Jun 6, 2018
@Haroenv
Copy link
Contributor

Haroenv commented Jun 6, 2018

perfect 👌

@endiliey
Copy link
Contributor

Facing this :( hahaha

@Haroenv
Copy link
Contributor

Haroenv commented Oct 28, 2018

@endiliey, is that in docusaurus? Would be nice if you showed some code which gives this error.

I think the best option for now is to always lazy-load docsearch in code bases which are server side rendered.

@endiliey
Copy link
Contributor

@Haroenv it's for Docusaurus 2, because we are creating the SSR version of the app in v2.

Since there is no official React component for DocSearch, i created our own (still simple / wip now)
https://github.com/facebook/Docusaurus/blob/master/v2/lib/theme/Search/index.js
Lazy-loaded docsearch.js

@Haroenv
Copy link
Contributor

Haroenv commented Oct 28, 2018

This seems good while looking at it from my phone, is this still throwing the error?

@endiliey
Copy link
Contributor

endiliey commented Oct 28, 2018 via email

@Haroenv
Copy link
Contributor

Haroenv commented Oct 28, 2018

No problem, feel free to get in touch if you have any more questions!

embishh added a commit to embishh/posthog.com that referenced this issue Oct 13, 2020
moved the docsearch.js import statement into the useEffect hook, per algolia/docsearch#352
yakkomajuri added a commit to PostHog/posthog.com that referenced this issue Oct 14, 2020
* initial docsearch changes

add docsearch dependency, initial styling

* functional doc search

Doc search working & styled minimally with icon

* fix build

moved the docsearch.js import statement into the useEffect hook, per algolia/docsearch#352

* change search icon image, responsiveness

- switched to using background-image for the search bar icon
- moved the search bar to Menu.js where it won't disappear on smaller screens

* delete comments

get rid of unused comments

* load search bar only on docs pages

* removed from smaller screens

Couldn't figure out a way to make it behave on screens below 1150px without refactoring the Header styling entirely, so I took it off of screens below 1150px for now

* Revert "removed from smaller screens"

This reverts commit 4b59e35.

* Revert "Revert "removed from smaller screens""

This reverts commit d876e2b.

* isDocsPage check

* fix comment

comment import statement

* move search out of header

- move DocSearch from the header into the top of Doc page template body

* move docsearch loading code

Move DocSearch loading code from Header.js to postTemplate.js

* style responsive search bar

Make search bar responsive, use antd icon as submit button

* fix dropdown size on mobile

The suggestions dropdown had been too big for mobile screens.
Override here for min- and max- widths set in external algolia css

* style adjustments and export component

Co-authored-by: Yakko Majuri <yakko.majuri@gmail.com>
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

No branches or pull requests

3 participants