This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(legal): fix rendering of legal pages when loaded directly via url
Fixes #1372.
- Loading branch information
Showing
7 changed files
with
148 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Promise = require('bluebird'); | ||
var logger = require('intel').getLogger('legal-templates'); | ||
|
||
module.exports = function (i18n, root) { | ||
|
||
var TOS_ROOT_PATH = path.join(root, 'terms'); | ||
var PP_ROOT_PATH = path.join(root, 'privacy'); | ||
|
||
function getRoot(type) { | ||
return type === 'terms' ? TOS_ROOT_PATH : PP_ROOT_PATH; | ||
} | ||
|
||
var templateCache = {}; | ||
function getTemplate(type, lang, defaultLang) { | ||
var DEFAULT_LOCALE = i18n.localeFrom(defaultLang); | ||
|
||
// Filenames are normalized to locale, not language. | ||
var locale = i18n.localeFrom(lang); | ||
var templatePath = path.join(getRoot(type), locale + '.html'); | ||
var resolver = Promise.defer(); | ||
|
||
// cache the promises to avoid multiple concurrent checks for | ||
// the same template due to async calls to the file system. | ||
if (templateCache[templatePath]) { | ||
resolver.resolve(templateCache[templatePath]); | ||
return resolver.promise; | ||
} | ||
|
||
fs.exists(templatePath, function (exists) { | ||
if (! exists) { | ||
var bestLang = i18n.bestLanguage(i18n.parseAcceptLanguage(lang)); | ||
|
||
if (locale === DEFAULT_LOCALE) { | ||
var err = new Error(type + ' missing `' + DEFAULT_LOCALE + '` template: ' + templatePath); | ||
return resolver.reject(err); | ||
} else if (lang !== bestLang) { | ||
logger.warn('`%s` does not exist, trying next best `%s`', lang, bestLang); | ||
return resolver.resolve(getTemplate(type, bestLang, defaultLang)); | ||
} | ||
|
||
|
||
templateCache[templatePath] = null; | ||
return resolver.resolve(null); | ||
} | ||
|
||
fs.readFile(templatePath, 'utf8', function(err, data) { | ||
if (err) { | ||
return resolver.reject(err); | ||
} | ||
|
||
templateCache[templatePath] = data; | ||
resolver.resolve(data); | ||
}); | ||
}); | ||
|
||
return resolver.promise; | ||
} | ||
|
||
return getTemplate; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters