Difference deploy behavior from wrangler CLI and Cloudflare website

For Workes & Pages, what is the name of the domain?

What is the error number?

500

What is the error message?

This page isn’t working right now

What is the issue or error you’re encountering

be758b82.balanced-chef.pages.dev can’t currently handle this request.

When I deploy a project across git CLI and already connect Cloudflare Works, and it builds successfully, you can view it at https://be758b82.balanced-chef.pages.dev/, it can open, but the /blog/* page doesn’t work, I check everything is OK.

After, I tried to deploy the project through npx wrangler pages deploy dist, and it worked well on every page, you can see it at https://fe5e61f6.balanced-chef.pages.dev/

What’s wrong?

You’d need to tail the deployment and see.

I tail the site, here is a partial log

{
“truncated”: false,
“outcome”: “ok”,
“scriptName”: “pages-worker–3511923-preview”,
“diagnosticsChannelEvents”: ,
“exceptions”: ,
“logs”: [
{
“message”: [
“04:48:21 [ERROR] TypeError: Cannot read properties of undefined (reading ‘replace’)\n at fetchAPI (chunks/api_kzs1suqU.mjs:58:62)\n at getPosts (chunks/api_kzs1suqU.mjs:89:22)\n at chunks/Card_DLnlouns.mjs:9:24\n at Card (chunks/astro/server_DwHsZSmE.mjs:90:12)\n at callComponentAsTemplateResultOrResponse (chunks/astro/server_DwHsZSmE.mjs:1178:31)\n at renderToReadableStream (chunks/astro/server_DwHsZSmE.mjs:1126:32)\n at renderPage (chunks/astro/server_DwHsZSmE.mjs:2258:20)\n at lastNext (chunks/server_CEC5S_rT.mjs:1266:31)\n at async callMiddleware (chunks/server_CEC5S_rT.mjs:584:10)\n at async RenderContext.render (chunks/server_CEC5S_rT.mjs:1297:22)”
],
“level”: “error”,
“timestamp”: 1724474901007
}
],
}

I found my code about the use replace method, it used to trim the string obtained from the environment variable.

Environment variables | Cloudflare Workers docs
Secrets | Cloudflare Workers docs

By looking at the document, I found that the deployment can be successful using wrangler CLI because it reads the bocal development environment .env configuration, while the integrated deployment using git CLI uses the environment variables configured by Cloudflare Dashboard, but why is it not obtained?

If you’re using wrangler to deploy the worker, you should configure non-secret environment variables in wrangler.toml, and for secrets use a .dev.vars file and also wrangler secret put xx before running wrangler deploy.

If you change your environment variables in the Cloudflare dashboard, Wrangler will override them the next time you deploy. If you want to disable this behavior, add keep_vars = true to your wrangler.toml.

Wrangler will not delete your secrets (encrypted environment variables) unless you run wrangler secret delete <key>.

So this is the issue, not sure if fetchAPI is your code or a libraries but that code is trying to do a .replace call on an undefined variable.

and you can do, wrangler pages secret put to add a secret or use the dashboard. wrangler secret is for Workers not Pages

I don’t use Wrangler, I just use git CLI to continue deploying, and config the environment via dashboard configuration.

I just use Wrangler to confirm the deployment problem, and I think that is because my project (import.meta.env.ENV) can’t obtain dashboard environment variables. And how can I obtain this variable, maybe using process.env.ENV?

This project is here: sgalcheung/balanced-chef (github.com)

In Astro they’re available under Astro.locals.runtime - @astrojs/cloudflare | Docs

If you want them available elsewhere you’d need to expose them yourself

I don’t use a wrangler.tom file, my local env is .env.* file, it can be via import.meta.env.ENV obtain variables, but I don’t find any explanation about via dashboard obtain variables both Astro or Cloudflare docs.

These are the docs, you don’t need to use Wrangler.

yes - maybe something like

const { env } = Astro.locals.runtime;
const cms = env.SQUIDEX_ENVIRONMENT;

I don’t use the wrangler and .dev.vars configuration files.

What is Cloudflare runtime?

Do you mean I should use Astro.local.runtime?

---

const { env } = Astro.locals.runtime;

---

I found the Cloudflare adapter also sets the variable to process.env.ENV, should we use motdotla/dotenv?

Yes, as the Astro docs state.

I have no idea if process.env works - what the docs state I know does work and what is officially supported

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.