-
-
Save ThisIsMissEm/83da279659c3d0a4e4144ce6d83ba371 to your computer and use it in GitHub Desktop.
Despite the code being identical other than the ordering of the "addEventListener" calls, the results change when run on the Cloudflare Worker platform.
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
addEventListener('fetch', event => { | |
console.log("fetch handler 2", event.request) | |
}) | |
addEventListener('fetch', event => { | |
console.log("fetch handler 1") | |
event.respondWith(handleRequest(event.request)) | |
}) | |
/** | |
* Fetch and log a given request object | |
* @param {Request} request | |
*/ | |
async function handleRequest(request) { | |
console.log('Got request', request) | |
const response = await fetch(request) | |
console.log('Got response', response) | |
return response | |
} | |
/** | |
Outputs the following: | |
worker.js:2 fetch handler 2 Request {…} | |
worker.js:6 fetch handler 1 | |
worker.js:16 Got request Request {…} | |
worker.js:18 Got response Response {…} | |
**/ |
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
addEventListener('fetch', event => { | |
console.log("fetch handler 1") | |
event.respondWith(handleRequest(event.request)) | |
}) | |
addEventListener('fetch', event => { | |
console.log("fetch handler 2", event.request) | |
}) | |
/** | |
* Fetch and log a given request object | |
* @param {Request} request | |
*/ | |
async function handleRequest(request) { | |
console.log('Got request', request) | |
const response = await fetch(request) | |
console.log('Got response', response) | |
return response | |
} | |
/** | |
Outputs the following: | |
worker.js:6 fetch handler 1 | |
worker.js:15 Got request Request {…} | |
worker.js:17 Got response Response {…} | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apparently the cloudflare worker platform only executes 'fetch' event handlers until event.respondWith is called