As luck would have it, Cloudflare’s very own @zproser just published a blog post which shows how to do exactly what you’re attempting. You could probably use his script as a template for your own.
To more fully answer your question: we don’t currently support the
importScripts() function. Supporting it would significantly complicate script security auditing, and could be a source of nondeterminism if the script fetched by
importScripts() is not pinned to a specific version. But, if scripts imported by
importScripts() are pinned to a specific version, then using a tool like webpack, rollup, or browserify to bundle your dependencies with the main worker script is a better option, as this ensures that script cold starts take much less time – this is important because when scripts are cold started there is already a client request waiting for it.
However, for the specific use case of logging to Sentry, I don’t recommend bundling RavenJS with a tool like webpack. The problem, which @zproser mentions in the blog post, is that
RavenJS.captureException() uses a
fetch() call under the hood to report the error to Sentry, but there is no way to access the promise returned by that
fetch(). Without that promise, there is no way for the Cloudflare Workers runtime to know that it should wait for that outstanding Sentry request to complete before considering the overall FetchEvent lifetime complete. Instead, it assumes that as soon as the worker has finished sending the main Response back to the client, the FetchEvent lifetime is complete, and all outstanding asynchronous tasks (such as the Sentry request) are canceled. Since the code path which calls
RavenJS.captureException() fulfills that main Response with
undefined, the Sentry request is bound to be immediately canceled before it goes anywhere, and you won’t see your Sentry logs.
The solution is to use
event.waitUntil(promise) to instruct the Workers runtime to wait for the Sentry request to complete before tearing down the FetchEvent. See the blog post linked above for details.