ctrl-freaks / freezeframe.js

freezeframe.js is a library that pauses animated .gifs and enables them to animate on mouse hover / mouse click / touch event, or with trigger / release functions.
http://ctrl-freaks.github.io/freezeframe.js/
MIT License
1.41k stars 110 forks source link

Error when used with Gatsby: error "window" is not available during server side rendering. #73

Closed JoshuaKGoldberg closed 1 year ago

JoshuaKGoldberg commented 4 years ago

The project isn't open source 😞 but here's a relevant snippet from a build:

5:22:47 PM: error "window" is not available during server side rendering.
5:22:47 PM: 
5:22:47 PM: > 1 | !function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.Freezeframe=n():t.Freezeframe=n()}(window,(function(){return function(t){var n={};function e(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return
...

Visually, the top of the request gatsby-log.txt

nickforddev commented 4 years ago

Sounds like this is just a general issue with SSR compatibility with libraries that use browser-only APIs.

Have you looked at https://github.com/gatsbyjs/gatsby/issues/309?

It's not specific to this library, so I'm closing for now.

JoshuaKGoldberg commented 4 years ago

@nickforddesign I have looked at that 😄 and while the general error isn't specific to this library, the issue is that freezeframe.js accesses the window object immediately - regardless of the runtime it's working in.

Generally, the solution for getting code that tries to do that is to have it either:

More recently, globalThis is also a useful strategy.

Would you be open to any of these changes, and if so, could you please reopen this issue? There is behavior done in this library that makes it difficult to use in frameworks such as Gatsby.

nickforddev commented 4 years ago

Thanks for clarifying, I will look at this

JoshuaKGoldberg commented 4 years ago

Thank you very much! I'd be happy to help / send a PR / etc., if it's useful at all.

jonrrivera commented 3 years ago

hi everybody, I'm getting the same error when using next.js. I tried defining if (typeof window !== "undefined") { new Freezeframe(); } which removed the 'window not defined error', but now the project is not compiling. Please let me know if you need any further clarification. thank you!

jonrrivera commented 3 years ago

https://www.npmjs.com/package/react-freezeframe !!

Chinomso1995 commented 2 years ago

Did anyone manage to find a way around this for gatsby projects.

nickforddev commented 1 year ago

in general I'd recommend that SSR be skipped for this components, even JSDOM doesn't fully implement the canvas APIs required. So, whatever SRR framework you are using, let it skip freezeframe components.