Skip to content
This repository has been archived by the owner on Nov 13, 2018. It is now read-only.

'window is not defined' error while SSR #1

Closed
ikappas opened this issue Dec 2, 2017 · 5 comments
Closed

'window is not defined' error while SSR #1

ikappas opened this issue Dec 2, 2017 · 5 comments

Comments

@ikappas
Copy link
Collaborator

ikappas commented Dec 2, 2017

This following error is thrown while server side rendering the react app:

/projects/testing/test-app-ts/node_modules/svg-injector/svg-injector.js:464
}(window, document));
  ^

ReferenceError: window is not defined
    at Object.<anonymous> (/projects/testing/test-app-ts/node_modules/svg-injector/svg-injector.js:464:3)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)    at require (internal/module.js:11:18)
    at Object.svg-injector (/projects/testing/test-app-ts/build/server.js:3549:18)
    at __webpack_require__ (/projects/testing/test-app-ts/build/server.js:630:30)

This can be easily fixed by changing:

import SVGInjector from 'svg-injector';

with

const isBrowser = typeof window !== 'undefined';
const SVGInjector = isBrowser ? require('svg-injector') : undefined;

See also:
webpack/react-starter#37 (comment)
tanem/react-svg#15

@lifeiscontent
Copy link
Owner

@ikappas published 1.0.1 with the fix.

@ikappas
Copy link
Collaborator Author

ikappas commented Dec 2, 2017

@lifeiscontent The fix did not make it into dist

@lifeiscontent
Copy link
Owner

@ikappas sorry about that! Just added a prepublishOnly step to the package.json so I don't forget next time.

Let me know if it works for you. Just published 1.0.2

@lifeiscontent lifeiscontent reopened this Dec 4, 2017
@ikappas
Copy link
Collaborator Author

ikappas commented Dec 4, 2017

@lifeiscontent works like a charm! Thank you! 👍

@ikappas ikappas closed this as completed Dec 4, 2017
@lifeiscontent
Copy link
Owner

@ikappas not sure if this simplifies your case, but I decided to use this instead of this lib: https://github.com/lifeiscontent/react-mutate-icon rather than dealing with loading SVGs, you can create a object with react components built from svgr and do the same manipulation from this library without waiting for the icons to load.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants