Skip to content

Commit 1cc579f

Browse files
committed
Add custom worker with API proxy and deploy scripts
- Custom worker.ts with /api proxy to OPENWORKERS_API service binding - SPA fallback to index.html for Angular routing - Add compile:worker, predeploy/deploy scripts for dev and main
1 parent 4d26411 commit 1cc579f

5 files changed

Lines changed: 127 additions & 4 deletions

File tree

bun.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openworkers-dash",
3-
"version": "1.4.2",
3+
"version": "1.4.3",
44
"license": "MIT",
55
"scripts": {
66
"ng": "ng",
@@ -12,8 +12,12 @@
1212
"test": "echo \"Error: no test specified\" && exit 1",
1313
"format": "bun x prettier --write .",
1414
"check": "tsc --noEmit --project tsconfig.app.json",
15-
"predeploy:dev": "adapter-static ./dist/browser -o ./dist/openworkers",
16-
"deploy:dev": "ow dev workers upload dash-preview ./dist/openworkers"
15+
"compile:worker": "bun build worker.ts --outfile ./dist/openworkers/worker.js --format esm --target browser",
16+
"predeploy:dev": "adapter-static ./dist/browser -o ./dist/openworkers && bun compile:worker",
17+
"deploy:dev": "ow dev workers upload dash-preview ./dist/openworkers",
18+
"predeploy:main": "adapter-static ./dist/browser -o ./dist/openworkers && bun compile:worker",
19+
"deploy:main": "ow main workers upload openworkers-dash ./dist/openworkers",
20+
"clean": "rimraf dist node_modules .angular"
1721
},
1822
"private": true,
1923
"dependencies": {
@@ -47,6 +51,7 @@
4751
"ansi_up": "^6.0.6",
4852
"autoprefixer": "^10.4.23",
4953
"postcss": "^8.5.6",
54+
"rimraf": "^6.1.2",
5055
"tailwindcss": "4.1.17",
5156
"typescript": "5.9.3"
5257
}

src/app/modules/login/login.page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="flex xs:h-1/6 md:h-1/4 lg:h-full lg:w-2/3 lg:mx-0">
44
<div class="flex my-8 flex-row items-center justify-center lg:justify-end w-full">
55
<div class="xs:w-72 lg:w-96">
6-
<img class="h-8 xs:h-12" src="https://arq.pw/public/long-black-2.svg" alt="logo" />
6+
<img class="h-8 xs:h-12" src="/assets/long-black-2.svg" alt="logo" />
77
</div>
88
</div>
99
</div>

src/assets/long-black-2.svg

Lines changed: 33 additions & 0 deletions
Loading

worker.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/// <reference types="@openworkers/workers-types" />
2+
3+
interface Env {
4+
ASSETS: BindingAssets;
5+
OPENWORKERS_API?: BindingWorker;
6+
}
7+
8+
export default {
9+
async fetch(req: Request, env: Env): Promise<Response> {
10+
const url = new URL(req.url);
11+
let { pathname } = url;
12+
13+
try {
14+
pathname = decodeURIComponent(pathname);
15+
} catch {
16+
// ignore invalid URI
17+
}
18+
19+
// Proxy /api requests to API worker
20+
if (pathname.startsWith('/api')) {
21+
if (!env.OPENWORKERS_API) {
22+
return new Response('API worker not configured', { status: 500 });
23+
}
24+
25+
return env.OPENWORKERS_API.fetch(req);
26+
}
27+
28+
// Remove trailing slash (except for root)
29+
if (pathname !== '/' && pathname.endsWith('/')) {
30+
pathname = pathname.slice(0, -1);
31+
}
32+
33+
// Try to serve the file directly
34+
let response = await tryServeFile(env, pathname);
35+
36+
if (response) {
37+
return addHeaders(response, pathname);
38+
}
39+
40+
// SPA fallback to index.html
41+
response = await tryServeFile(env, '/index.html');
42+
43+
if (response) {
44+
return addHeaders(response, '/index.html');
45+
}
46+
47+
return new Response('Not Found', { status: 404 });
48+
}
49+
} satisfies ExportedHandler<Env>;
50+
51+
async function tryServeFile(env: Env, pathname: string): Promise<Response | null> {
52+
try {
53+
const response = await env.ASSETS.fetch(pathname);
54+
55+
if (response.ok) {
56+
return response;
57+
}
58+
59+
return null;
60+
} catch {
61+
return null;
62+
}
63+
}
64+
65+
function addHeaders(response: Response, pathname: string): Response {
66+
const headers = new Headers(response.headers);
67+
68+
if (pathname.startsWith('/assets/')) {
69+
headers.set('cache-control', 'public, max-age=31536000, immutable');
70+
} else if (pathname.endsWith('.html')) {
71+
headers.set('cache-control', 'no-cache');
72+
} else {
73+
headers.set('cache-control', 'public, max-age=3600');
74+
}
75+
76+
return new Response(response.body, {
77+
status: response.status,
78+
headers
79+
});
80+
}

0 commit comments

Comments
 (0)