Skip to content

Bug: Hyphen in dynamic route slug causes regex syntax error #822

@tsuyuni

Description

@tsuyuni

Problem Description

When using OpenNext for Cloudflare, dynamic routes containing hyphens in the slug (e.g., [...better-auth]) are not properly escaped when converted to regular expressions, causing the following error:

Uncaught SyntaxError: Invalid regular expression: /^/(?:)?api/auth/[...better-auth](?:/)?$/: Range out of order in character class

Steps to Reproduce

  1. Create a Next.js application with the following route structure:

    src/app/api/auth/[...better-auth]/route.ts
    
  2. Build with OpenNext Cloudflare:

    opennextjs-cloudflare build
  3. Deploy to Cloudflare Workers - the above error occurs during runtime

Root Cause

In regular expressions, hyphens within character classes [] are treated as range operators (e.g., [a-z]). When [...better-auth] is converted to a regex pattern, the hyphen is not properly escaped within the character class, causing a "Range out of order" error because . and a don't form a valid range.

Expected Behavior

The hyphen should be properly escaped in the generated regex:

/^/(?:)?api/auth/[...better\-auth](?:/)?$/

Or the hyphen should be placed at the beginning or end of the character class:

/^/(?:)?api/auth/[-...betterauth](?:/)?$/

Current Workaround

Use slug names without hyphens:

src/app/api/auth/[...auth]/route.ts

Environment

  • Next.js: 15.3.4
  • @opennextjs/cloudflare: ^1.4.0
  • Node.js: v18+
  • Cloudflare Workers

Impact

This prevents using popular authentication libraries like Better Auth that use hyphened route names by convention ([...better-auth]).

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