Skip to content

Commit

Permalink
Fixing infinite loop case to fix issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsah committed Apr 3, 2018
1 parent d4cbf50 commit c1d9c84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
33 changes: 26 additions & 7 deletions cmscontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const read = require('node-readability');
const winston = require('winston');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const uiLangs = require('./configs/uiLangs.json');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;

Expand All @@ -26,10 +27,26 @@ const getContentOutputFile = (pageName, lang) => path.join(
`${pageName}_${lang}.html`
);

const getErrorFile = (pageName, lang) => path.join(
appconstants.CONTENT_CACHE, "error",
`${pageName}_${lang}.html`
);
const getErrorFile = (lang) => {
var langCode = lang.split("_");
var availableLangs = uiLangs.uiLanguages;
// check if the current language language is there in the list
// of ui languages otherwise return english
if (availableLangs.indexOf(langCode[0]) > -1) {
return
path.join(
appconstants.CONTENT_CACHE, "error",
`_error_${langCode[0]}.html`
);
} else {
return
path.join(
appconstants.CONTENT_CACHE, "error",
"_error_en.html"
);
}
}
;

/**
* Reads pages.json and generates static content for each of the listed content pages
Expand Down Expand Up @@ -123,7 +140,7 @@ const pageCleanup = (pageHtml) => {
* @param {string} lang
*/
function serveFile(req, res, next, page, lang) {
let cmsPage = page.startsWith('_error') ? getErrorFile(page, lang) : getContentOutputFile(page, lang);
let cmsPage = page.startsWith('_error') ? getErrorFile(lang) : getContentOutputFile(page, lang);
fs.readFileAsync(cmsPage, 'utf8')
.then(
(content) => {
Expand All @@ -136,8 +153,10 @@ function serveFile(req, res, next, page, lang) {
(err) => {
winston.log('error', "error while serving file " + page, err );
let errLang = lang || "en" ;
console.log(" ERR LANG ", errLang);
serveFile(req, res, next, '_error', errLang);
// prevent infinite loops
if (!page.startsWith('_error')) {
serveFile(req, res, next, '_error', errLang);
}
}
);
}
Expand Down
1 change: 1 addition & 0 deletions configs/uiLangs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"uiLanguages": ["en", "es", "fr"]}

0 comments on commit c1d9c84

Please sign in to comment.