-
Notifications
You must be signed in to change notification settings - Fork 390
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
Comments
Could you use 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? |
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:
but looks like just importing docsearch throws the exception.
|
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 |
Thanks, it does work now, you can close 👍 |
perfect 👌 |
Facing this :( hahaha |
@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. |
@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) |
This seems good while looking at it from my phone, is this still throwing the error? |
It worked fine. Sorry for the trouble
Sent from Mail for Windows 10
From: Haroen Viaene
Sent: Sunday, 28 October 2018 10:38 PM
To: algolia/docsearch
Cc: Endilie Yacop Sucipto; Mention
Subject: Re: [algolia/docsearch] window is not defined error (#352)
This seems good while looking at it from my phone, is this still throwing the error?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
No problem, feel free to get in touch if you have any more questions! |
moved the docsearch.js import statement into the useEffect hook, per algolia/docsearch#352
* 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>
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:
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
The text was updated successfully, but these errors were encountered: