Skip to content

Commit

Permalink
fix: workaround assets being unable to run a worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Cherry committed Nov 10, 2024
1 parent 955c78c commit 87edddc
Show file tree
Hide file tree
Showing 17 changed files with 522 additions and 421 deletions.
903 changes: 490 additions & 413 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@
"@adaptivelink/pops": "0.5.8",
"@cloudflare/kv-asset-handler": "0.3.4",
"@cloudflare/vitest-pool-workers": "https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/11180182432/npm-package-cloudflare-vitest-pool-workers-6835",
"@cloudflare/workers-types": "4.20241004.0",
"@cloudflare/workers-types": "4.20241106.0",
"@nodecraft/eslint-config": "44.0.0",
"@types/sanitize-html": "2.13.0",
"@typescript-eslint/eslint-plugin": "8.8.0",
"@typescript-eslint/eslint-plugin": "8.13.0",
"eslint": "8.57.1",
"eslint-plugin-json": "4.0.1",
"eslint-plugin-unicorn": "56.0.0",
"sanitize-html": "2.13.1",
"typescript": "5.6.2",
"typescript": "5.6.3",
"validate-color": "2.2.4",
"vitest": "2.1.2",
"wrangler": "3.80.0"
"vitest": "2.1.4",
"wrangler": "3.86.0"
},
"engines": {
"node": ">=18"
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
30 changes: 27 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ async function handleEvent(request: Request, env: Env, ctx: ExecutionContext) {
return response;
}

// else get the assets from KV
// else get the assets from Workers Assets/
// We need to rewrite the path to `/cdn-cgi/assets/...`
const options = {
cacheControl: {
edgeTTL: 60 * 60 * 1, // 1 hour
Expand All @@ -96,14 +97,37 @@ async function handleEvent(request: Request, env: Env, ctx: ExecutionContext) {

let asset = null;
try {
asset = await env.ASSETS.fetch(request);
const fixedUrl = new URL(request.url);
fixedUrl.pathname = `/cdn-cgi/assets${fixedUrl.pathname}`;
if (fixedUrl.pathname.endsWith('/')) {
fixedUrl.pathname = fixedUrl.pathname.slice(0, -1);
}
const fixedRequest = new Request(fixedUrl.toString(), request);
asset = await env.ASSETS.fetch(fixedRequest);
} catch (err) {
const probableError = err as Error;
return new Response(probableError?.message || probableError.toString(), { status: 500 });
}

// rewrite location redirects to remove the `/cdn-cgi/assets` prefix
const assetHeaders = new Headers(asset.headers);
if (assetHeaders.has('location')) {
const location = assetHeaders.get('location');
if (location && location.includes('/cdn-cgi/assets')) {
assetHeaders.set('location', location.replace('/cdn-cgi/assets', ''));

// if empty string, set to root so that we actually redirect
if (assetHeaders.get('location') === '') {
assetHeaders.set('location', '/');
}
}
}
// recreate response so that headers are mutable
asset = new Response(asset.body, asset);
asset = new Response(asset.body, {
status: asset.status,
statusText: asset.statusText,
headers: assetHeaders,
});
// set cache headers
asset.headers.set('Cache-Control', `public, max-age=${options.cacheControl.browserTTL}`);

Expand Down

0 comments on commit 87edddc

Please sign in to comment.