StreamSaver.js
StreamSaver.js copied to clipboard
npm i streamsaver -> how to use in react ?
Hi, I tried it in react using
import streamSaver from 'streamsaver'
And it shows error : window is not defined
How to use it with npm ?
Thanks
React.js or react native?
Hi @jimmywarting thanks for quick reply, its react js, my current workaround is using
<script src="https://cdn.jsdelivr.net/npm/[email protected]/StreamSaver.min.js"></script>
and put it in root html
I just remembered maybe its because of ssr, this was in my next js project, when it is importing, no window object available
@x4080 : you import it with: import * as streamsaver from 'streamsaver'
You can see an example here:
@dreamofi Thanks, I'll try it
@dreamofi Thanks it works !
@dreamofi No, I make mistake, it works when the link is in the client, but when rendered from server its not working
@x4080 can you post your webpack configuration file?
@dreamofi , I'm using next js and my next.config.js seems not using custom webpack config so it goes like this
const withSass = require('@zeit/next-sass');
const withOffline = require('next-offline')
module.exports = withSass(withOffline());
Thanks for your help
@x4080 I have yet used nextjs, but what is the error when you render from the server, and can you post the code where you use streamsaver? Do you add ponyfill?
@dreamofi It's simple actually, 'window is not defined' when it rendered on server
@dreamofi you should be able to load the dependencies on componentDidMount
and set it on the component, since this life cycle is guaranteed to only run on the client.
componentDidMount() {
// HACK: streamsaver references window which is undefined on SSR. Ensure library is only loaded on client
try {
this.streamSaver = require('streamsaver');
if (!this.streamSaver.WritableStream) {
this.streamSaver.WritableStream = require('web-streams-polyfill/ponyfill').WritableStream;
}
} catch (e) {
console.error(e);
}
}
someFunction = () => {
const fileStream = this.streamSaver.createWriteStream('hi-there.mp4');
...
}
@jimmywarting any chance we can prevent such errors by checking against window
(const isSSR = typeof window === "undefined"
)? Current calls to window
will keep crashing SSR builds on Next.js, Gatsby, etc. https://github.com/jimmywarting/StreamSaver.js/blob/1bce493b16fd51cf313570445e2b9f96045393cb/StreamSaver.js#L15
maybe should use globalThis
instead?
@jimmywarting yeah I think that would work!
any updates on this?
if window is not defined then you probably are using streamsaver in a wrong context like some server side render like next or in a web worker.
only include streamsaver on the client side (on the main thread)
I'm using dynamic imports on my code for now to work around this. was just asking because there was a suggestion of using globalThis
in place of window
.
I guess if StreamSaver.js is supposed to be a browser-only module and window
is supposed always work, then this issue should be closed to avoid confusion.
Webpack 5 might be able to solve this issue with its recently added native Worker support.