My CF Worker needs to load a large array containing a search term index. This takes around 80 - 100 milliseconds to load. It should be a one-off operation on Worker start.
If I put the array load before the “async function handleRequest(request)” then I get the following error when running “wrangler publish”:
"code": 10021, "message": "Script startup timed out.\n"
However if I put the array load within the “async function handleRequest(request)” then the Worker publishes and works just fine. The problem with this approach is that the array is re-loaded every time the Worker handlers a new request, which adds an unnecessary CPU overhead to each request (~80-100ms) instead of just the initial load at startup.
I just want the array to be loaded once by the Worker, before the async function, but without the script startup timing out on publish.
The array does not need to be available for the first request to the worker (i.e. HTML page load) but does need to present for subsequent requests which may come several seconds later when the visitor starts entering content into a web form.
Is there any way of doing an asynchronous loading of the array, so it doesn’t block the initial script startup, but has loaded by the time the second or subsequent request is made to the Worker?