Skip to main content

2026-11-28 · 4 min read

A Chinese-character URL passed build and tests, then 404'd in production

Next.jsDebugging

Translated from the Chinese original: read it in Chinese

I added "posts by tag" pages to the blog: /blog/tags/devops, /blog/tags/next-js, and so on. The URL is made from the tag name, lowercased, with spaces and dots replaced by -:

export function tagSlug(tag: string) {
  return tag.toLowerCase().replace(/[\s.]+/g, "-");
}

One of the eight posts has a Chinese tag, "privacy" (two Chinese characters), so its URL was /blog/tags/ followed by those two characters.

First failure: the build

Error occurred prerendering page "/md/blog/tags/<the Chinese tag>".
TypeError: Cannot convert argument to a ByteString because the character
at index 31 has a value of 38577 which is greater than 255.

Every page on this site has a Markdown version for AI tools to read, and its response carries a Link HTTP header pointing back to the original page:

Link: `<${SITE_URL}/${path}>; rel="canonical"`

HTTP headers can only hold ASCII, so the Chinese characters blew it up. I changed it to encodeURI(path) and the build passed.

Second failure: tests passed, production returned 404

Unit tests passed, the build passed, and I opened /blog/tags/next-js locally and it looked fine, but that was the only page I tried, an English tag. After pushing, while tidying my to-do list, I requested every URL in the sitemap:

404 /blog/tags/<the Chinese tag>

The other 42 were 200. Only that one failed.

Next.js pre-renders /blog/tags/<the Chinese tag> as a page at build time. But when a browser sends the request, the Chinese in the URL is percent-encoded as /blog/tags/%E9%9A%B1%E7%A7%81. Somewhere along the way these two did not match up, the server could not find the pre-rendered page, and since I had set dynamicParams = false (anything not in the list is a 404), the result was a 404.

Why did I not notice locally? Because I only clicked the English tag page and never made "request every URL" part of the tests. After the fix, I ran the new test against the old version locally and it got the same 404: the problem had always been there, nobody was looking.

The fix: only letters and digits in URLs

Rather than dig into which layer of Next.js encodes Chinese URLs how, I just made URLs free of Chinese:

const TAG_SLUGS: Record<string, string> = { "\u96b1\u79c1": "privacy" }; // the Chinese tag for "privacy"

export function tagSlug(tag: string) {
  return TAG_SLUGS[tag] ?? tag.toLowerCase().replace(/[\s.]+/g, "-");
}

(The key is written here as unicode escapes; in the real file it is the Chinese word itself.)

Two more lines of defense:

  1. A unit test: every tag's URL must match /^[a-z0-9-]+$/, so adding a new Chinese tag and forgetting the mapping fails immediately
  2. An E2E test: fetch the sitemap, and every URL in it must return 200

I ran the second test against the version from before the fix to confirm it really catches this bug:

-   Array []
+   Array [
+     "404 /blog/tags/<the Chinese tag>",
+   ]

It has to fail there; that is what shows the test is actually doing something.

What I learned

  • "The build passed" only means the code compiles, not that every URL opens. The sitemap is the site's own declaration that "these URLs exist", and checking each one is cheap.
  • Run a new test against the broken version first. If a test always passes, you cannot tell whether the code is fine or the test is not testing anything.
  • The Chinese tag still shows in Chinese on screen; only the URL is English. Users cannot tell the difference, but a whole class of encoding problems is gone.

Later (added 2026-11-29)

This bug was fixed on November 9. After that the tag pages changed twice, and each change affected one of the two lines of defense above.

November 12: tag pages left the sitemap. Looking at Search Console, a large share of the 27 "Crawled – currently not indexed" pages were tag pages. A tag page is only a subset of the article list and duplicates /blog, so I made them noindex (links still followed) and dropped them from the sitemap. The side effect: the second line of defense (every sitemap URL must return 200) no longer checks tag pages. What guards "no Chinese in URLs" now is only the first one, the unit test that every tag's URL must match /^[a-z0-9-]+$/.

November 25: Cache Components was turned on, and dynamicParams = false can no longer be written. With it on, route settings like that in a page make the build fail. I replaced it with export const ensureStatic = "navigation": the page must be fully static, and a URL outside the list is only answered after the page has been generated, so that notFound() is a real 404. Without it the server streams the page shell first, the status is already 200, and "a nonexistent URL returns 200", the exact opposite of this post's bug (details in the Cache Components post).

Checked against the live site today:

200 /blog/tags/privacy
200 /blog/tags/next-js
404 /blog/tags/%E9%9A%B1%E7%A7%81   ← the old Chinese URL, no longer in the list
404 /blog/tags/no-such-tag          ← a tag that does not exist, a real 404

The posts now have 45 tags; the only Chinese one is still the single privacy tag. The sitemap has 129 URLs, 0 of them tag pages.

One small footnote: CI runs E2E against next dev, which compiles each page the first time it is requested, so the sitemap test that requests every URL took longer than the default 30 seconds. I raised its timeout to 10 minutes (November 28).

Type to search projects and posts too