rdutton
CDCloudflare Developers
•Created by rdutton on 8/30/2024 in #workers-help
Alternative to Fetch API (can't add body to GET)
Is there an alternative to the Fetch API within a Cloudflare worker? Axios does not work (as it relies on Node libs) .. I assume other packages will have a similar problem. Reason is because a vendor API requires a body on a GET request and that produces a "TypeError: Request with a GET or HEAD method cannot have a body". At this point I am considering proxying the request via an AWS Lambda but would love a workaround within a the Cloudflare environment.
3 replies
CDCloudflare Developers
•Created by rdutton on 2/13/2023 in #workers-help
Worker Using StreamWriter produces error
I am developing a worker that uses streams and am encountering some issues. I have reduced the code down to the following simple example to reproduce (one) of my problems. I have based it loosely off this : https://developers.cloudflare.com/workers/learning/using-streams/ with the difference being I want to write my own information to the response. Am I using streams correctly? I suspect I am not..
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
let { readable, writable } = new TransformStream();
const writer = writable.getWriter();
await writer.write((new TextEncoder()).encode("Hello"));
return new Response(readable, {headers: {"content-type":"application/json"}});
}
The error I am getting is:
A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
17:37:07 POST / 200
✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.2 replies
CDCloudflare Developers
•Created by rdutton on 1/22/2023 in #pages-help
Issue setting up ideal development environment for Cloudflare Pages react app with _worker.js
I have created a webpack-based react app that places the output from npm build in /dist. I have also added a boilerplate _worker.js in to /dist .
When I run wrangler pages dev dist I can access both the react app and the worker in the browser, which is good, however I will have to run npm build each time the React app is updated (changes to _worker.js do seem to be watched).
Running wrangler pages dev -- npm start does work, except that _worker.js is ignored and only the React part of the application is visible in the browser.
My question is essentially, how do I get a development environment setup where changes to both the worker code and the react code are watched and rebuilt when changed? If someone can point me in the right direction I would greatly appreciate it.
2 replies