UNPKG

3.86 kBMarkdownView Raw
1# Location Polyfill
2
3A [`Location`](https://developer.mozilla.org/docs/Web/API/Window/location) polyfill for Cloudflare Workers.
4
5## Usage
6
7Import the polyfill in your application code.
8Make sure it's included at the top, before any other dependencies that register `fetch` event listeners.
9
10```ts
11import '@worker-tools/location-polyfill';
12```
13
14This will populate the global `location` field with the `url` field from incoming requests.
15In CF Workers, this will typically be your script's `workers.dev` address.
16
17*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!
18
19To avoid sniffing the url from every request, you can provide the location via a global variable called `WORKER_LOCATION`.
20In CF Workers, add the following to your `wrangler.toml` to define this variable.
21
22```toml
23[vars]
24 WORKER_LOCATION = 'http://localhost:8787'
25```
26
27<br/>
28
29--------
30
31<br/>
32
33<p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
34<p align="center">This module is part of the Worker Tools collection<br/>⁕
35
36[Worker Tools](https://workers.tools) are a collection of TypeScript libraries for writing web servers in [Worker Runtimes](https://workers.js.org) such as Cloudflare Workers, Deno Deploy and Service Workers in the browser.
37
38If you liked this module, you might also like:
39
40- 🧭 [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
41- πŸ”‹ [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
42- πŸ“„ [__Worker HTML__][html] --- HTML templating and streaming response library
43- πŸ“¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
44- πŸ†— [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
45- 🎏 [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
46- πŸ₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
47- πŸ¦‘ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
48
49Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
50- ✏️ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
51- πŸ“ [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
52- πŸ¦• [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Deno’s native HTTP server.
53
54[router]: https://workers.tools/router
55[middleware]: https://workers.tools/middleware
56[html]: https://workers.tools/html
57[kv-storage]: https://workers.tools/kv-storage
58[cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
59[deno-kv-storage]: https://workers.tools/deno-kv-storage
60[kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
61[response-creators]: https://workers.tools/response-creators
62[stream-response]: https://workers.tools/stream-response
63[json-fetch]: https://workers.tools/json-fetch
64[json-stream]: https://workers.tools/json-stream
65[request-cookie-store]: https://workers.tools/request-cookie-store
66[extendable-promise]: https://workers.tools/extendable-promise
67[html-rewriter]: https://workers.tools/html-rewriter
68[location-polyfill]: https://workers.tools/location-polyfill
69[deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
70
71Fore more visit [workers.tools](https://workers.tools).
\No newline at end of file