Fastly
Edgemesh Client to a Fastly Compute at Edge enabled site is super easy. To start, add the following line as early as possible to the <head> tag of your index.html:
index.html
1
<script
2
async
3
type="text/javascript"
4
data-cfasync="false"
5
src="https://static.edgeme.sh/client.js"
6
onload="window.edgemesh = new Edgemesh()">
7
</script>
Copied!
You can pass any of the Client Config options to the Edgemesh class constructor:
index.html
1
<script
2
async
3
type="text/javascript"
4
data-cfasync="false"
5
src="https://static.edgeme.sh/client.js"
6
onload="window.edgemesh = new Edgemesh({
7
swUrl: '/my-sw.js',
8
scope: '/assets'
9
})">
10
<script/>
Copied!
Next up, just enable a Compute at Edge worker to listen for the Edgemesh Service worker path and return the code. A functional example is show below:
1
import { Request, Response, URL, Fastly, Headers } from '@fastly/as-compute'
2
3
const backend = 'your-backend'
4
const text = "importScripts('https://static.edgeme.sh/edgemesh-sw.js')"
5
6
function fetch (request: Request): Response {
7
const url = new URL(request.url)
8
9
if (request.method === 'GET' && url.pathname === '/sw.js') {
10
const headers = new Headers()
11
headers.set('Content-Type', 'application/javascript')
12
13
return new Response(String.UTF8.encode(text), {
14
url: url.toString(),
15
status: 200,
16
headers
17
})
18
}
19
20
const cacheOverride = new Fastly.CacheOverride()
21
// Set your default cache settings here
22
// cacheOverride.setSWR(60 * 60 * 24)
23
24
return Fastly.fetch(request, { backend, cacheOverride }).wait()
25
}
26
27
Fastly.respondWith(fetch(Fastly.getClientRequest()))typ
Copied!
Lastly, create an account on the Edgemesh Portal to validate the installation. Data should begin flowing into the system within 15 minutes!
Copy link