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

gatsby: window is not defined #57

Closed
bfine9618 opened this issue Mar 6, 2019 · 4 comments
Closed

gatsby: window is not defined #57

bfine9618 opened this issue Mar 6, 2019 · 4 comments

Comments

@bfine9618
Copy link

bfine9618 commented Mar 6, 2019

Rendering filestack using gatsby throws the following error. Please help.

WebpackError: ReferenceError: window is not defined

  - filestack-react.js:1 Object../node_modules/filestack-react/dist/filestack-react.js
    [lib]/[filestack-react]/dist/filestack-react.js:1:452
@AndrzejSala
Copy link
Contributor

@bfine9618
Can you provide version of filestack-react which are you using?

@MatthewGreenberg
Copy link

getting the same thing. i am using Next.js and the newest version of filestack-react

@MatthewGreenberg
Copy link

Ive partially figured this out. the 'window' object is not available on SSR react projects, so you need to dynamically import the module on componentDidMount or with a library. Next.js has dynamic imports-- im sure gatsby has something similar. This would be nice if it were mentioned in the docs

@bfine9618
Copy link
Author

I found a solution without dynamic imports:

use the Client import from filestack-js. You have to import the js script into the gatsby-ssr file. I do it in the onRenderBody

export const onRenderBody = ({ setHeadComponents }) => { return setHeadComponents([ <script key="filestack" src="//static.filestackapi.com/filestack-js/2.x.x/filestack.min.js"/> ]) }

Then in your file you can import the filestack client in the component like this:
const client = filestack.init('API_KEY')

then you can use a normal html file import with the onChange function that looks like:
onChange={ (e) => client.upload(e.target.files[0]).then(data => console.log(data.url))}

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