From 1e152e5743117a1534da3ae9b191253b385f70e7 Mon Sep 17 00:00:00 2001 From: aki-0421 <118268728+aki-0421@users.noreply.github.com> Date: Thu, 20 Apr 2023 11:53:07 +0900 Subject: [PATCH] F: update readme --- README.md | 5 +---- examples/fetch-event/README.md | 36 +++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 37818c1..8c5e128 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,7 @@ - [x] Calling stubs * [x] D1 (alpha) * [x] Environment variables -* FetchEvent's [lifecycle methods](https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#lifecycle-methods) - - [x] waitUntil - - [ ] respondWith - - [ ] passThroughOnException +* [x] FetchEvent ## Installation diff --git a/examples/fetch-event/README.md b/examples/fetch-event/README.md index 861afef..40923eb 100644 --- a/examples/fetch-event/README.md +++ b/examples/fetch-event/README.md @@ -1,30 +1,48 @@ -# fetch-event +# [FetchEvent](https://developers.cloudflare.com/workers/runtime-apis/fetch-event/) -## Document +Normally, workers are designed to return some kind of HTTP Response and exit immediately upon receiving an HTTP request. `FetchEvent` can extend these life cycles. -* https://developers.cloudflare.com/workers/runtime-apis/fetch-event/ +#### WaitUntil -## Setup +`WaitUntil` extends the lifetime of the "fetch" event. It accepts an asynchronous task which the Workers runtime will execute without blocking the response. The worker will not be terminated until those tasks are completed. + +#### PassThroughOnException + +`PassThroughOnException` prevents a runtime error response when the Worker script throws an unhandled exception. Instead, the request forwards to the origin server as if it had not gone through the worker. + +## Example + +### Usecase + +You have decided to implement a log stream API to capture all access logs. You must edit the Headers so that the user's API token is not logged. If an unknown error occurs during this process, the entire service will be down, which must be avoided. +In such cases, declare PassThroughOnException first and use WaitUntil for logging. + +### Setup + +This example worker is triggered by [Routes](https://developers.cloudflare.com/workers/platform/triggers/routes/). To try this example, add your site to cloudflare and add some records(A and CNAME, etc.) so that you can actually access the website. +If your domain is `sub.example.com`, edit `wrangler.toml` as following: ```toml routes = [ - { pattern = "example.com/*", zone_name = "example.com" } + { pattern = "sub.example.com/*", zone_name = "example.com" } ] ``` -## Development +The workers is executed if the URL matches `sub.example.com/*`. -### Requirements +### Development + +#### Requirements This project requires these tools to be installed globally. * wrangler * tinygo -### Commands +#### Commands ``` make dev # run dev server make build # build Go Wasm binary make publish # publish worker -``` +``` \ No newline at end of file