-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_worker.js
More file actions
34 lines (29 loc) · 1.14 KB
/
_worker.js
File metadata and controls
34 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler';
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const url = new URL(request.url);
// Handle static assets
if (url.pathname.startsWith('/_next/static/') ||
url.pathname.startsWith('/favicon') ||
url.pathname.endsWith('.svg') ||
url.pathname.endsWith('.ico') ||
url.pathname.endsWith('.png') ||
url.pathname.endsWith('.jpg') ||
url.pathname.endsWith('.jpeg') ||
url.pathname.endsWith('.gif') ||
url.pathname.endsWith('.webp')) {
return getAssetFromKV(request, {
mapRequestToAsset: req => new Request(`${req.url.replace(/\/$/, '')}/index.html`, req)
});
}
// For all other requests, serve the index.html (client-side routing)
return getAssetFromKV(request, {
mapRequestToAsset: req => new Request(`${req.url.replace(/\/$/, '').replace(/\/[^\/]*$/, '')}/index.html`, req)
});
} catch (e) {
return new Response('Error: ' + e.message, { status: 500 });
}
}