Polka does not support unicode in routing patterns.
aral opened this issue · 2 comments
The problem
Polka does not support unicode in routing patterns.
(Routing fails when unicode – e.g., emoji or an ü, etc. – is included in the routing pattern.)
Basic reproduction
import polka from 'polka'
polka()
.get('/kitten', (_request, response) => response.end('Meow!'))
.get('/😸', (_request, response) => response.end('Meow!'))
.listen(3000, _ => {
console.log('Visit http://localhost:3000/kitten (works)')
console.log('and http://localhost:3000/😸 (doesn’t work).')
})
I’m assuming this is actually an issue in trouter, so going to try and reproduce there now but opening the issue here since it’s the main project that’s impacted.
Nope, the bug is in Polka, not trouter:
import Trouter from 'trouter'
const router = new Trouter()
router
.get('/kitten', _ => {
console.log('Meow from plain text route path!')
})
.get('/😸', _ => {
console.log('Meow from emoji route path!')
})
// Find a route definition
const kittenText = router.find('GET', '/kitten')
const kittenEmoji = router.find('GET', '/😸')
// Both handlers execute as expected.
kittenText.handlers[0]()
kittenEmoji.handlers[0]()
You can replace the Kitten emoji with the letter ü
and observe the same behaviour.
Workaround
You must manually URI encode every component of the file path before adding your route to the router. Unless I’m mistaken, this is basically what SvelteKit does also (see sveltejs/kit#2171).
So here’s the above example, with the workaround implemented:
import path from 'node:path'
import polka from 'polka'
const encodeFilePath = filePath => filePath
.split(path.sep)
.map(value => encodeURIComponent(value))
.join(path.sep)
polka()
.get(encodeFilePath('/kitten'), (_request, response) => response.end('Meow!'))
.get(encodeFilePath('/😸'), (_request, response) => response.end('Meow!'))
.listen(3000, _ => {
console.log('Visit http://localhost:3000/kitten (works)')
console.log('and http://localhost:3000/😸 (now also works).')
})
Suggested solution
Based on the above workaround, Polka itself should apply the equivalent of the encodeFilePath()
function internally and transparently when .get()
, etc., is called.
Please let me know if you’d like a pull request for this; I’d be happy to prepare one.
Possibly the same issue as sveltejs/kit#2166
(I’m seeing this on Polka 1.0.0-next.22.)
More observations:
If I add the route like this, it works:
.get('/%F0%9F%98%B8', (_request, response) => response.end('Meow!'))
Even if I use middleware to rewrite manually decode the URI component, it doesn’t work.
e.g.,
.use((request, _response, next) => {
console.log(request.path)
request.path = decodeURIComponent(request.path)
next()
})
You also cannot just encodeUriComponent()
the path you’re adding to the router as that will encode the slashes too and it still doesn’t work.