Skip to content

headers is undefined in route-level beforeHandle when using plugin composition (AOT mode) #1712

@qprqpr

Description

@qprqpr

What version of Elysia is running?

1.4.22

What platform is your computer?

Darwin 25.2.0 arm64 arm

What environment are you using

Bun 1.3.7

Are you using dynamic mode?

No (using default AOT mode)

What steps can reproduce the bug?

When using a composable plugin pattern with beforeHandle on a specific route, the headers parameter is undefined instead of an object.

import { Elysia } from "elysia";

// Setup plugin with services
const setup = new Elysia({ name: "setup" })
  .decorate("myService", { foo: "bar" });

// Controller using setup
const controller = setup.post(
  "/webhook",
  ({ body }) => {
    return { ok: true };
  },
  {
    beforeHandle({ headers }) {
      console.log("headers:", headers); // undefined
      console.log("typeof headers:", typeof headers); // "undefined"

      // This throws: "undefined is not an object"
      if (headers["x-secret-token"] !== "secret") {
        return new Response("Forbidden", { status: 403 });
      }
    },
  }
);

const app = new Elysia()
  .use(controller)
  .listen(3000);

Send a request:

curl -X POST http://localhost:3000/webhook \
  -H "Content-Type: application/json" \
  -H "x-secret-token: secret" \
  -d '{}'

What is the expected behavior?

headers should be an object containing request headers (or at least an empty object {}), allowing access like headers["x-secret-token"].

What do you see instead?

headers is undefined. Accessing any property throws:

undefined is not an object (evaluating 'headers["x-secret-token"]')

The error in minified production code appears as:

undefined is not an object (evaluating 'A["x-secret-token"]')

Additional information

Workaround

Using request.headers.get() instead of destructured headers works correctly:

beforeHandle({ request }) {
  const token = request.headers.get("x-secret-token");
  if (token !== "secret") {
    return new Response("Forbidden", { status: 403 });
  }
}

Potentially related to #1617

Have you try removing the node_modules and bun.lockb and try again yet?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions