Skip to content

Commit

Permalink
[Fix] Ignore the case sensitive of Http Request (#1702)
Browse files Browse the repository at this point in the history
Change Logs:

1) Fix:Due to Http Request string shouldn't be case sensitive. So convert them in lowercase in union and do comparation to make something like `zh-CN` = `zh-cn`.

2) Fix a typo.
3) Change some "let" to "const".
  • Loading branch information
Maledong authored and fhemberger committed Jun 16, 2018
1 parent de5f7ef commit 312ea8b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ const statics = chokidar.watch(path.join(__dirname, 'static'), opts)

// Redirect mechanism meant as a fix for languages where some pages
// has not translated yet, therefore redirect to the english equivalent,
// which ofc isn't the correct language, but better than a 404-page
// which isn't the correct language, but better than a 404-page
function redirectToEnglishUrl (req, res) {
return () => {
const isAlreadyEnglish = req.url.startsWith('/en')
const urlContainsLanguage = req.url.split('/').length > 2
// Union the Url to lower case (ignore the case sensitive)
// E.g: `zh-cn` equals to `zh-CN`
const url = req.url.toLowerCase()

const isAlreadyEnglish = url.startsWith('/en')
const urlContainsLanguage = url.split('/').length > 2

if (isAlreadyEnglish || !urlContainsLanguage) {
res.writeHead(404, 'Not found')
return res.end()
}

let englishUrl = req.url.replace(/^\/\w+\//, '/en/')
const englishUrl = url.replace(/^\/\w+\//, '/en/')

res.writeHead(302, {
location: englishUrl
Expand Down

0 comments on commit 312ea8b

Please sign in to comment.