Skip to content

Latest commit

 

History

History
49 lines (37 loc) · 1.71 KB

location-polyfill.md

File metadata and controls

49 lines (37 loc) · 1.71 KB
layout description
page
A Location polyfill for Cloudflare Workers.

Location Polyfill

A Location polyfill for Cloudflare Workers.

  • Table of Contents {:toc .large-only}

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'

{:style="margin: 2rem 0"}

Links: GitHub | ghuc.cc · NPM | Browse Package · deno.land | Docs {:.faded}