-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements support for `[text_blobs]` as defined by cloudflare/wrangler-legacy#1677. Text blobs can be defined in service-worker format with configuration in `wrangler.toml` as - ``` [text_blobs] MYTEXT = "./path/to/my-text.file" ``` The content of the file will then be available as the global `MYTEXT` inside your code. Note that this ONLY makes sense in service-worker format workers (for now). Workers Sites now uses `[text_blobs]` internally. Previously, we were inlining the asset manifest into the worker itself, but we now attach the asset manifest to the uploaded worker. I also added an additional example of Workers Sites with a modules format worker.
- Loading branch information
1 parent
a283836
commit 72f035e
Showing
11 changed files
with
342 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
feat: implement `[text_blobs]` | ||
|
||
This implements support for `[text_blobs]` as defined by https://github.com/cloudflare/wrangler/pull/1677. | ||
|
||
Text blobs can be defined in service-worker format with configuration in `wrangler.toml` as - | ||
|
||
``` | ||
[text_blobs] | ||
MYTEXT = "./path/to/my-text.file" | ||
``` | ||
|
||
The content of the file will then be available as the global `MYTEXT` inside your code. Note that this ONLY makes sense in service-worker format workers (for now). | ||
|
||
Workers Sites now uses `[text_blobs]` internally. Previously, we were inlining the asset manifest into the worker itself, but we now attach the asset manifest to the uploaded worker. I also added an additional example of Workers Sites with a modules format worker. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
getAssetFromKV, | ||
mapRequestToAsset, | ||
} from "@cloudflare/kv-asset-handler"; | ||
|
||
import manifestJSON from "__STATIC_CONTENT_MANIFEST"; | ||
const assetManifest = JSON.parse(manifestJSON); | ||
|
||
/** | ||
* The DEBUG flag will do two things that help during development: | ||
* 1. we will skip caching on the edge, which makes it easier to | ||
* debug. | ||
* 2. we will return an error message on exception in your Response rather | ||
* than the default 404.html page. | ||
*/ | ||
const DEBUG = false; | ||
|
||
export default { | ||
async fetch(request, env, ctx) { | ||
let options = { | ||
ASSET_NAMESPACE: env.__STATIC_CONTENT, | ||
ASSET_MANIFEST: assetManifest, | ||
}; | ||
|
||
/** | ||
* You can add custom logic to how we fetch your assets | ||
* by configuring the function `mapRequestToAsset` | ||
*/ | ||
// options.mapRequestToAsset = handlePrefix(/^\/docs/) | ||
|
||
try { | ||
if (DEBUG) { | ||
// customize caching | ||
options.cacheControl = { | ||
bypassCache: true, | ||
}; | ||
} | ||
|
||
const page = await getAssetFromKV( | ||
{ | ||
request, | ||
waitUntil(promise) { | ||
return ctx.waitUntil(promise); | ||
}, | ||
}, | ||
options | ||
); | ||
|
||
// allow headers to be altered | ||
const response = new Response(page.body, page); | ||
|
||
response.headers.set("X-XSS-Protection", "1; mode=block"); | ||
response.headers.set("X-Content-Type-Options", "nosniff"); | ||
response.headers.set("X-Frame-Options", "DENY"); | ||
response.headers.set("Referrer-Policy", "unsafe-url"); | ||
response.headers.set("Feature-Policy", "none"); | ||
|
||
return response; | ||
} catch (e) { | ||
// if an error is thrown try to serve the asset at 404.html | ||
if (!DEBUG) { | ||
try { | ||
let notFoundResponse = await getAssetFromKV( | ||
{ | ||
request, | ||
waitUntil(promise) { | ||
return ctx.waitUntil(promise); | ||
}, | ||
}, | ||
{ | ||
ASSET_NAMESPACE: env.__STATIC_CONTENT, | ||
ASSET_MANIFEST: assetManifest, | ||
mapRequestToAsset: (req) => | ||
new Request(`${new URL(req.url).origin}/404.html`, req), | ||
} | ||
); | ||
|
||
return new Response(notFoundResponse.body, { | ||
...notFoundResponse, | ||
status: 404, | ||
}); | ||
} catch (e) {} | ||
} | ||
|
||
return new Response(e.message || e.toString(), { status: 500 }); | ||
} | ||
}, | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.