From 0276e52829f1e17d9422551e8c85eba70b327d23 Mon Sep 17 00:00:00 2001 From: MaleDong Date: Sun, 1 Jul 2018 09:38:52 +0800 Subject: [PATCH] [Fix] Make the `redirectToEnglishUrl` behaves properly for local debugging According to the normal webside, we have 4 kinds of url forms: 1) https://localhost:8080/: Access the route, the default lang is `en`. 2) https://localhost:8080/zh-cn/docs/: If we don't have the proper translations, just direct you to `en`. 3) https://localhost:8080/zh-nc/docs/: If we have a wrong language, 404 will be returned. 4) https://localhost:8080/blog: When directly accessing a folder, just add `en` as the default lang. For other page routes, `404` will be redirected to you. So the original codes don't meet all these cases. Here's the fix for that. --- server.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/server.js b/server.js index e8ca140916ad4..043ff2cdd23b0 100644 --- a/server.js +++ b/server.js @@ -34,28 +34,34 @@ const opts = { const locales = chokidar.watch(path.join(__dirname, 'locale'), opts) const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts) const statics = chokidar.watch(path.join(__dirname, 'static'), opts) +const fs = require('fs') +// Read all the langs under `locale` +const SUPPORTED_LANGUAGES = new Set(fs.readdirSync(path.join(__dirname, 'locale'))) // Redirect mechanism meant as a fix for languages where some pages // have not been translated yet, therefore redirect to the english equivalent, // which isn't the correct language, but better than a 404-page function redirectToEnglishUrl (req, res) { return () => { - // 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() + // Url should be case insensitive.(e.g: zh-CN = zh-cn), + // So we should make a convert to the lower case and check the route values. + let url = req.url.toLowerCase() + const splitedValues = url.split('/') + // For urls like `/blog`, add `en` before that + if (splitedValues.length === 2) { + splitedValues[0] = 'en' + url = splitedValues.join('/').trim() + } else if (splitedValues[1] !== 'en' && SUPPORTED_LANGUAGES.has(splitedValues[1])) { + // For urls like `/lang/docs/`. + // If we found the lang in our set, this means the specific lang + // doesn't have proper translated pages yet, so force the default + // lang to `en`. + splitedValues[1] = 'en' + url = splitedValues.join('/').trim() } - const englishUrl = url.replace(/^\/\w+\//, '/en/') - res.writeHead(302, { - location: englishUrl + location: url }) res.end() } @@ -93,6 +99,11 @@ statics.on('add', (filePath) => { // Initializes the server and mounts it in the generated build directory. http.createServer((req, res) => { + // If we are accessing the root, it should be redirected to `/en` instead. + // We shouldn't get a 404 page. + if (req.url === '/') { + req.url = '/en' + } mount(req, res, redirectToEnglishUrl(req, res)) }).listen(port, () => console.log(`http://localhost:${port}/en/`))