-
Notifications
You must be signed in to change notification settings - Fork 61
Description
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
-
Create a Next.js application with the following route structure:
src/app/api/auth/[...better-auth]/route.ts
-
Build with OpenNext Cloudflare:
opennextjs-cloudflare build
-
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]
).