javascriptcontinuous-deploymentcloudflare-workersesbuildwrangler

Why is the script in the Cloudflare worker's online editor not updated after automatic deployment?


When using git to push a commit to online repo (Gitlab|Github) and have CI/CD deploy the index.js file to the Cloudflare Worker automatically, after a few automatic deployments and manual edits in the Online Editor that provides live feedback to code-changes, eventually, the editor starts showing and older version of the code. (seemingly.)

  1. Update version number in comment.
  2. Save file. Commit File on proper branch linked to your CD.
  3. Push file.
  4. Wait 'build'/deploy step at Cloudflare worker's interface \ Deployments is complete.
  5. Open Online Editor.
  6. Review code and find something else than the code you JUST committed in git. (Unexpected)

So the code in git after deployment is not the same as the code marked 'latest' in the online editor. It looks like an older version.

This Q&A is added to prevent other users from running in the same circles. Even LLMs will send you in the wrong direction because so much could be configured incorrect in the pipeline. And LLMs are not great if the user does not realize yet, they are asking the wrong questions. The LLM was convinced there were two versions: a production environment version and a development environment version or convinced there was a button that was not there.

Rephrased related questions that suffer a bit from not-yet-seeing-the-problem:

  1. How to make sure the Cloudflare worker script is also updated after deployment via git? (npx wrangler deploy ?)
  2. Why isn't the script online identical to the one in git after deployment?
  3. Why is the Cloudflare worker updated, but the online editor still showing an older version?
  4. Why am I not seeing the updated version number in comment in the online editor code after deployment via git?
  5. How to deploy to Cloudflare's online editor as well as production?

Solution

  • When npx wrangler deploy is run after being triggered by a code push on a git branch, Wrangler will alter your index.js (or worker.js) file before uploading. Even though simple Cloudflare workers or Azure functions do not have a need for a build step, this deploy step alters the payload.

    This underlying issue was/is having a different expectations on what deployment in Cloudflare context entails and what a deployment step is 'allowed' to do.

    Contrary to the expectation in the question, a deployment via git to a Cloudflare worker, does not result in uploading the identical code to the online editor due to the bundling step by Wrangler.

    You can disable this behaviour by adding a no-bundle flag to your deploy step. (Alt->[2]):

    npx wrangler deploy --no-bundle

    or

    npx wrangler deploy --no-bundle {your other flags}

    Depending on your set-up, one can update this command in the Cloudflare worker's online settings, in Build \ Build Configuration \ Deploy. This will result in having the index.js file in git be identical with the online editor in Cloudflare after the next deployment. (You may opt to 'Retry build' button online with the safe effect, without a new push.) Screenshot Cloudflare worker deploy command setting.

    One ought to review the pros and cons of using the no-bundle feature.

    Consider that code of the type that technically only requires a deployment and no technical build or compile step, could probably still benefit from optimization and minimize operations, especially in context of browsers.

    It remains unclear why the changes via git or edits via the online editor result in a different after-save/deployment experience in the editor.

    If file-edits online are bundled out-of-sight, one could benefit from having the source file uploaded to the editor after git push while a bundled output is deployed to the worker.)

    Regardless, both ways of working have their own benefit: in case of Cloudflare worker's online editor:

    versus


    [1] https://developers.cloudflare.com/workers/wrangler/bundling/

    [2] Alternatives like altering *.toml with [build.upload.format] turned out to be ancient and deprecated. https://developers.cloudflare.com/workers/wrangler/deprecations/.