How to detect the Cloudflare Worker runtime?

Hello,

I’m wondering how I can detect if a JS script is working within a Cloudflare Worker runtime? This would be similar to detecting if JS is in Node. I stumbled onto a situation where an SSR dependency was using the following function to check for Node and block browser-specific logic:

var isNode = typeof global !== 'undefined' && {}.toString.call(global) === '[object global]';

I’m looking for a way to reasonably and safely do the same for CFWs so that I can correctly patch the issue for other CF users.

David

If it were me, I’d look for the presence of non-standard globals specific to the cloudflare runtime, like caches.default or WebSocketPair

I see, ok that’s helpful for me to move ahead with. Does Cloudflare have an official method or answer to this though? I suspect lots of other libraries will have a similar issue.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.