Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers library running unexpectedly within Wrangler context #27

Open
niconiahi opened this issue Oct 30, 2024 · 3 comments
Open

Headers library running unexpectedly within Wrangler context #27

niconiahi opened this issue Oct 30, 2024 · 3 comments

Comments

@niconiahi
Copy link

niconiahi commented Oct 30, 2024

context: I'm running a Remix application deployed to Cloudflare Pages, using official plugins and CLIs. No weird stuff

when using the Headers library like this

import SuperHeaders from "@mjackson/headers"

async function invalidateCache(urls: string[], env: Env) {
  const headers = new SuperHeaders()
  headers.contentType = "application/json"
  headers.set("Authorization", `Bearer ${env.AUTHORIZATION}`)
  headers.set("X-Auth-Email", env.X_AUTH_EMAIL)
  headers.set("X-Auth-Key", env.X_AUTH_KEY)
  return fetch(`https://api.cloudflare.com/client/v4/zones/${env.ZONE_ID}/purge_cache`, {
    headers,
    method: "POST",
    body: JSON.stringify({
      files: urls,
    }),
  })
    .then((response) => {
      return response.json()
    })
    .catch((error) => {
      console.error("error", error)
    })
}

when this runs locally with pnpm run dev (remix vite:dev), the invalidation gets executed correctly

when running this, either deployed with pnpm run deploy (wrangler pages deploy ./build/client) or running the preview locally with pnpm run start (wrangler pages dev ./build/client), the headers don't get applied correctly. How do I know this? I get a Response letting me know there was an error (code 9106) which says: "Missing X-Auth-Key, X-Auth-Email or Authorization headers"

when refactoring the code to disregard using the Headers library, it works

async function invalidateCache(urls: string[], env: Env) {
  return fetch(`https://api.cloudflare.com/client/v4/zones/${env.ZONE_ID}/purge_cache`, {
    headers: {
      "Authorization": `Bearer ${env.AUTHORIZATION}`,
      "X-Auth-Email": env.X_AUTH_EMAIL,
      "X-Auth-Key": env.X_AUTH_KEY,
      "Content-Type": "application/json",
    },
    method: "POST",
    body: JSON.stringify({
      files: urls,
    }),
  })
    .then((response) => {
      return response.json()
    })
    .catch((error) => {
      console.error("error", error)
    })
}

let me know if I could provide any further information

@mjackson
Copy link
Owner

mjackson commented Nov 9, 2024

Are you using Remix's installGlobals function in production? If so, stop. People have reported various issues using it with the headers library.

@niconiahi
Copy link
Author

I'm not actually. I just checked all over my codebase for installGlobals and didn't find any match

@mjackson
Copy link
Owner

It won't be in your codebase. It will be in remix-serve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants