AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Next.Js 16 Regression: Non-Latin-1 Route Group/Segment Names Crash With InvalidCharacterError Due To Btoa In Segment-Value-Encoding

In Next.js 16, route groups or segments with non-ASCII names (e.g. (안녕), (日本), (café), emoji) cause dev server 500, build failure, and prerender error because encodeToFilesystemAndURLSafeString calls btoa directly on the raw Unicode string, which only accepts Latin-1 characters. The segment cache is enabled by default in Next 16, so every such segment hits this path. A UTF-8 safe encoding is required.

highConfidence 95%Next.jsAffected V16.3.3

Origin Analysis

encodeToFilesystemAndURLSafeString in packages/next/src/shared/lib/segment-cache/segment-value-encoding.ts uses btoa(value) without first converting the string to a UTF-8 byte sequence. btoa only supports characters in the Latin-1 range (0-255), so any non-Latin-1 character (CJK, Cyrillic, emoji, accented Latin-1 supplement) throws DOMException: InvalidCharacterError. This function is used for the segment cache key which is now always enabled in Next 16, causing build/prerender and dev server crashes for affected routes.
1. npx create-next-app@latest repro --app --ts --no-tailwind --no-eslint 2. Create route group with non-ASCII name and page: app/(안녕)/hello/page.tsx 3. Run next dev and open /hello -> observe 500 and InvalidCharacterError in terminal 4. Run next build -> observe prerender failure and export error

Fixing Code Block

Edge Case Audit

This changes the encoded representation for non-ASCII segment names. If any cached segment cache entries (.next/cache) were generated with the old encoding (though they would have crashed, so unlikely), they may need invalidation. After applying the fix, delete the .next directory to avoid stale key mismatches. Ensure the corresponding decode function (if any) also uses TextDecoder or equivalent to convert the binary string back to UTF-8 bytes to maintain round-trip consistency. For rollback, restore the original btoa(value) call but note that non-ASCII segment names will again crash. No cross-platform issues expected since TextEncoder is globally available in Node.js (>=11) and all modern browsers.

Ecosystem Topology