pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber
https://cannon.pmnd.rs
2.79k stars 156 forks source link

Blob is not defined #22

Closed gtolarc closed 4 years ago

gtolarc commented 4 years ago

When using SSR, 'Blob is not defined' error occurs. Perhaps you use blobs in the packaging, can you remove them for SSR?

drcmda commented 4 years ago

could you look into the source? im not experience enough to deal with ssr, no idea what must be done. it's most likely in src/index.tsx

stockhuman commented 4 years ago

@gtolarc Are you using Gatsby, by any chance?

gtolarc commented 4 years ago

Here is sample source using nextjs. https://codesandbox.io/s/awesome-bardeen-pxhs8 @drcmda

drcmda commented 4 years ago

The webworker is inlined, most likely as a blob, by a roll-up plugin. It's happening in a setState. So, should I simply not do it on the server? And how do I know that I'm on the server?

gtolarc commented 4 years ago

When converting a web worker to the es module, should the code be blobs unconditionally? I'm asking because I don't have any insight about it. @drcmda

gtolarc commented 4 years ago
const isServer = typeof window === 'undefined';

I don't know if you want this. I'm using this if I want to only work on client in nextjs. @drcmda

drcmda commented 4 years ago

can you try? i test against window now before creating the worker

drcmda commented 4 years ago

ok looks like the rollup plugin adds that blob globally. the check i've added doesn't seem to do anything. probably needs a different solution.

gtolarc commented 4 years ago

The same problem still occurs. I think the error occurs right away when worker.js is imported. So can we solve it fundamentally? like this https://www.npmjs.com/package/typed-web-workers @drcmda

drcmda commented 4 years ago

the problem is, that thanks to web standards web workers are downright stupid, they don't know what an import statement is, nor do they have access to npm. the package that im using is "rollup-plugin-web-worker-loader" - it packs the worker (resolves imports, bablifies, etc), then makes an inline blob. the problem seems to be that it does that in the global space.

so a solution could be dynamic imports. index.tsx would import the blob via code splitting IF it's not running ssr. but creates a slight delay until physics become usable.

drcmda commented 4 years ago

im trying, but codesplitting in rollup is something i have never done. will keep you posted. if you find an easier solution, please save me from wasting my time :-)

drcmda commented 4 years ago

you could try again, i think it should work now

gtolarc commented 4 years ago

It works!! BTW, I searched for related content. In the process of bundling web workers into a single file, blobs are inevitable. Additionally, I've verified that next.js's dynamic import feature works by wrapping components that contain physics implementations. For general use, it is good to check whether it is a server in the use-cannon library itself and decide whether to import or not. So the way you fixed it seems best :) @drcmda

codynova commented 4 years ago

This looks to be resolved, closing