Fixing Chinese Short Links in Cloudflare Worker

Requirement

A friend reported that if a custom short link uses Chinese characters, it doesn't work.

Let me test it. Add a "Chinese short link" in the management page.

LocalStorage List on the admin page, with load, clear and import KV buttons plus a list of short links

Query the KV store, added correctly.

Search Chinese short links by prefix, list shows the added short links

Tested in the browser, got an error saying it doesn't exist.

Browser visiting a Chinese short link returns 404 Not Found


========

Analysis

Go into the Cloudflare worker editor page.

Add console log statements in the controller.

Cloudflare Worker edit page with console.log(path) debug statement highlighted in orange

Test.

workers.dev test panel GET on a Chinese short link, returns a 302 redirect

Found the cause. The Chinese characters were encoded.

Console showing percent-encoded Chinese characters and a warning that the event handler returned a Promise

Just find a way to decode it back.


========

Practice

Asked GPT: https://devv.ai/search?threadId=dhrz3tf3gidc

devv.ai code decoding a Chinese short link with decodeURIComponent

Add a conversion

path = decodeURIComponent(path);

Line 290 highlighted in orange: path = decodeURIComponent(path)

Tested, the function works normally.

Console showing Chinese short link output by Worker correctly decoded

Thinking more broadly, I tested emoji too, and it also works normally.

Console showing percent-encoded emoji short link F0 9F 8C B9, test passed


========

The End

Github

https://github.com/crazypeace/url-Shorten-Worker/

Demo site

https://urlsrv.crazypeace.workers.dev/bodongshouqulveweifengci



Comments