@worker-tools/location-polyfill

A Location polyfill for Cloudflare Workers

Usage no npm install needed!

<script type="module">
  import workerToolsLocationPolyfill from 'https://cdn.skypack.dev/@worker-tools/location-polyfill';
</script>

README

Location Polyfill

A Location polyfill for Cloudflare Workers.

Usage

Import the polyfill in your application code. Make sure it's included at the top, before any other dependencies that register fetch event listeners.

import '@worker-tools/location-polyfill';

This will populate the global location field with the url field from incoming requests. In CF Workers, this will typically be your script's workers.dev address.

Note that the location field will be overwritten with each request! This is because I haven't been able to find a way to access the worker's URL outside a fetch event context. Let me know if there's a better way!

To avoid sniffing the url from every request, you can provide the location via a global variable called WORKER_LOCATION. In CF Workers, add the following to your wrangler.toml to define this variable.

[vars]
  WORKER_LOCATION = 'http://localhost:8787'