Skip to content

req.query is now a getter and can't use as custom server in front of Next.js #6633

@ale-grosselle

Description

@ale-grosselle

When using Express as a custom server in front of Next.js, accessing req.query directly no longer returns a mutable plain object. Instead, it is now a getter, and attempts to modify it or use it as a plain object lead to unexpected behavior or errors.

This change affects routes where the server modifies or depends on the mutability of req.query before passing it to Next.js, breaking compatibility with existing implementations.

Next.js Issue:
vercel/next.js#79158


Context

We applied the following patch, but we do not believe it is the best solution:

// Express 5 makes the 'query' property read-only, but Next.js needs to write to it
try {
  Object.defineProperty(req, 'query', descriptor);
} catch (error) {
  // If property already exists and is configurable, delete and recreate it
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    delete (req as any).query;
    Object.defineProperty(req, 'query', descriptor);
    logger.logInfo(`query property fixed ${error}`, requestID);
  } catch (error) {
    logger.logWarn(
      `Could not fix query property for Next.js compatibility: ${error}`,
      requestID
    );
  }
}

Is it possible to know why Express decided to change? This would help us understand where we need to make adjustments (on the Next.js side or the Express.js side).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions