#Server middleware not executed on Cloudflare Workers

1 messages · Page 1 of 1 (latest)

gloomy beacon
#

Hey, I have a pretty simple server middleware in server/middleware/001.app-check.global.ts:

import { sendRedirect } from '#imports'

export default defineEventHandler(async (event) => {
  const appVersion = getRequestHeader(event, 'x-app-version')
  console.log('Headers received:', getRequestHeaders(event))
  console.log('x-client-version:', appVersion)
  if (appVersion) {
    return sendRedirect(event, 'https://example.com')
  }
})

This works perfectly fine locally. However, the middleware doesn't seem to be executed on Cloudflare Workers. Is there something I'm missing? I'm using Nuxt 4.2.1.

snow pagodaBOT
#

Hi @gloomy beacon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

There’s nothing in the provided sources that directly explains server middleware not running on Cloudflare Workers for Nuxt 4.2.1, so I can’t state the exact cause with certainty.

From the available context, we do know:

  • In Nuxt 4.2, the dev/prod server architecture changed so that all requests go through Nitro first, and Vite (or other layers) are only reached if Nitro passes them on. [vite proxy change]
  • On Cloudflare Workers, there are some platform‑specific quirks around request handling (e.g. DELETE + routeRules.proxy causing hangs due to headers / body expectations), which required workarounds using custom server routes instead of routeRules. [CF DELETE issue]

However, none of the sources mention:

  • Server middleware being skipped or disabled on Cloudflare Workers.
  • Any special configuration needed to enable server/middleware/*.global.ts on that platform.
  • A known bug in 4.2.1 where server middleware doesn’t run on Workers.

Given that:

  • Your middleware is in the correct directory (server/middleware/…global.ts), which is how Nuxt server middleware is normally registered.
  • It works locally, which suggests the file name and logic are valid.

But I don’t have any source that confirms:

  • Whether Cloudflare Workers + Nuxt 4.2.1 has a regression specifically affecting server middleware.
  • Any required Cloudflare‑specific configuration to ensure those handlers run.

Because of that, I can’t reliably tell you why it’s not executing on Cloudflare Workers or propose a documented fix based on these sources alone.

If you can, I’d recommend:

#
  • Checking Nuxt’s issue tracker specifically for “server middleware Cloudflare Workers 4.2” (the provided snippets only cover routeRules/proxy and dev proxy changes, not middleware execution).
  • As a temporary workaround, you might consider implementing the logic as a dedicated server route (e.g. under server/routes) and explicitly calling it, similar to how the Cloudflare DELETE proxy issue was worked around with a custom route instead of routeRules.proxy. [CF DELETE workaround]

But to be transparent: the knowledge sources here do not contain enough information to definitively diagnose or solve your specific middleware‑not‑running issue on Cloudflare Workers.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@gloomy beacon Kapa.ai is still learning and improving, please let me know how I did by reacting below.

gloomy beacon
#

@snow pagoda yeah, already searched the GitHub issues but wasn't able to find a similar issue. Hold on, the middleware does get executed, I was just looking at the wrong thing in the Workers Observability. It's in the events, not invocations of course. However, both my console.logs don't spit out anyting... Do I need to convert them into JSON or something?

snow pagodaBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

The knowledge sources don’t mention how console.log from Nuxt/Nitro appears in Cloudflare Workers Observability, so I can’t say with certainty how/where those logs should show up.

What we do have that’s related to Cloudflare Workers is about request handling (e.g. DELETE + routeRules.proxy issues), not logging. [CF DELETE issue]

From these sources:

  • There’s no mention that you must convert logs to JSON for them to appear.
  • There’s no documented Nuxt/Nitro‑specific requirement for logging on Cloudflare Workers.
  • The Cloudflare examples shown in the issues only expose logs as plain strings/objects in Workers’ own logs, not JSON‑wrapped by the user. [CF DELETE issue]

So, based on the available information:

  • Your middleware executing but not showing console.log output in Observability is not explained in the sources.
  • There’s no indication that converting to JSON is required; plain console.log('text', someObject) should be fine, but I don’t have a Nuxt‑specific confirmation for Workers.

Given these limits, I can’t reliably tell you how to make those logs appear in Cloudflare’s UI or whether a specific format is needed. The behavior of console.log inside a Worker (and how it’s surfaced in Observability) is a Cloudflare concern and isn’t covered in the Nuxt‑focused material we have.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: