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.

Query the KV store, added correctly.

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


========

Analysis

Go into the Cloudflare worker editor page.

Add console log statements in the controller.

Test.

Found the cause. The Chinese characters were encoded.

Just find a way to decode it back.


========

Practice

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

Add a conversion

path = decodeURIComponent(path);

Tested, the function works normally.

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


========

The End

Github

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

Demo site

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



Comments