Use `/assets` for client assets

- Route `/assets` -> `build/public`, falling back on `public`
- Moved destinations for `nodebb.min.js`, `acp.min.js`, `admin.css`, `stylesheet.css`, `templates`, and `sounds` to `build/public`
- r.js modules previously routed through express are now symlinked into `public/build/src/modules`
- minfiles no longer served from a memory cache
- use config `views_dir` setting everywhere template directory is used
- fix cache buster `v=v=`
v1.18.x
Peter Jaszkowiak 8 years ago
parent e1a29f4aa8
commit 05b68391dd

@ -18,3 +18,4 @@ logs/
*.ipr
*.iws
/coverage
/build

@ -99,7 +99,7 @@ function loadConfig(callback) {
nconf.defaults({
base_dir: __dirname,
themes_path: path.join(__dirname, 'node_modules'),
views_dir: path.join(__dirname, 'public/templates'),
views_dir: path.join(__dirname, 'build/public/templates'),
version: pkg.version
});

@ -57,6 +57,7 @@ exports.buildTargets = function (targets, callback) {
winston.info('[build] Building javascript');
var startTime = Date.now();
async.series([
meta.js.linkModules,
async.apply(meta.js.minify, 'nodebb.min.js'),
async.apply(meta.js.minify, 'acp.min.js')
], step.bind(this, startTime, 'js', next));

@ -338,7 +338,7 @@ $(document).ready(function () {
callback(templates.cache[template]);
} else {
$.ajax({
url: RELATIVE_PATH + '/templates/' + template + '.tpl' + (config['cache-buster'] ? '?v=' + config['cache-buster'] : ''),
url: config.relative_path + '/assets/templates/' + template + '.tpl' + '?' + config['cache-buster'],
type: 'GET',
success: function (data) {
callback(data.toString());

@ -558,7 +558,7 @@ app.cacheBuster = null;
var scriptEl = document.createElement('script');
scriptEl.type = 'text/javascript';
scriptEl.src = config.relative_path + '/vendor/jquery/js/jquery-ui.js' + (app.cacheBuster ? '?v=' + app.cacheBuster : '');
scriptEl.src = config.relative_path + '/vendor/jquery/js/jquery-ui.js' + '?' + config['cache-buster'];
scriptEl.onload = callback;
document.head.appendChild(scriptEl);
};
@ -625,7 +625,7 @@ app.cacheBuster = null;
app.loadProgressiveStylesheet = function () {
var linkEl = document.createElement('link');
linkEl.rel = 'stylesheet';
linkEl.href = config.relative_path + '/js-enabled.css';
linkEl.href = config.relative_path + '/assets/js-enabled.css';
document.head.appendChild(linkEl);
};

@ -84,7 +84,7 @@ define('forum/account/header', [
params: {uid: ajaxify.data.uid },
accept: '.png,.jpg,.bmp'
}, function (imageUrlOnServer) {
components.get('account/cover').css('background-image', 'url(' + imageUrlOnServer + '?v=' + Date.now() + ')');
components.get('account/cover').css('background-image', 'url(' + imageUrlOnServer + '?' + config['cache-buster'] + ')');
});
},
removeCover

@ -513,12 +513,12 @@
break;
}
jQuery.getScript(config.relative_path + '/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '.js').done(function () {
jQuery.getScript(config.relative_path + '/assets/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '.js').done(function () {
jQuery('.timeago').timeago();
adaptor.timeagoShort = assign({}, jQuery.timeago.settings.strings);
// Retrieve the shorthand timeago values as well
jQuery.getScript(config.relative_path + '/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '-short.js').done(function () {
jQuery.getScript(config.relative_path + '/assets/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '-short.js').done(function () {
// Switch back to long-form
adaptor.toggleTimeagoShorthand();
});

@ -1,5 +1,5 @@
require.config({
baseUrl: config.relative_path + "/src/modules",
baseUrl: config.relative_path + '/assets/src/modules',
waitSeconds: 7,
urlArgs: config['cache-buster'],
paths: {

@ -29,7 +29,7 @@
}
});
$.get(config.relative_path + '/api/widgets/render' + (config['cache-buster'] ? '?v=' + config['cache-buster'] : ''), {
$.get(config.relative_path + '/api/widgets/render' + '?' + config['cache-buster'], {
locations: widgetLocations,
template: template + '.tpl',
url: url,

@ -4,6 +4,7 @@ var fs = require('fs');
var path = require('path');
var async = require('async');
var sanitizeHTML = require('sanitize-html');
var nconf = require('nconf');
var utils = require('../../public/src/utils');
var Translator = require('../../public/src/modules/translator').Translator;
@ -23,7 +24,7 @@ function filterDirectories(directories) {
}
function getAdminNamespaces(callback) {
utils.walk(path.resolve(__dirname, '../../public/templates/admin'), function (err, directories) {
utils.walk(path.resolve(nconf.get('views_dir'), 'admin'), function (err, directories) {
if (err) {
return callback(err);
}
@ -60,7 +61,7 @@ var fallbackCacheInProgress = {};
var fallbackCache = {};
function initFallback(namespace, callback) {
fs.readFile(path.resolve(__dirname, '../../public/templates/', namespace + '.tpl'), function (err, file) {
fs.readFile(path.resolve(nconf.get('views_dir'), namespace + '.tpl'), function (err, file) {
if (err) {
return callback(err);
}

@ -2,6 +2,7 @@
var async = require('async');
var nconf = require('nconf');
var meta = require('../../meta');
var settingsController = module.exports;
@ -25,7 +26,7 @@ function renderEmail(req, res, next) {
var path = require('path');
var utils = require('../../../public/src/utils');
var emailsPath = path.join(__dirname, '../../../public/templates/emails');
var emailsPath = path.join(nconf.get('views_dir'), 'emails');
async.waterfall([
function (next) {

@ -105,7 +105,7 @@ uploadsController.uploadSound = function (req, res, next) {
return next(err);
}
var soundsPath = path.join(__dirname, '../../../public/sounds'),
var soundsPath = path.join(__dirname, '../../../build/public/sounds'),
filePath = path.join(__dirname, '../../../public/uploads/sounds', uploadedFile.name);
if (process.platform === 'win32') {

@ -26,7 +26,7 @@ function getCover(type, id) {
return covers[id];
}
return nconf.get('relative_path') + '/images/cover-default.png';
return nconf.get('relative_path') + '/assets/images/cover-default.png';
}
module.exports = coverPhoto;

@ -86,15 +86,6 @@ module.exports = function (Meta) {
});
};
Meta.css.getFromFile = function (callback) {
async.series([
async.apply(Meta.css.loadFile, path.join(__dirname, '../../public/stylesheet.css'), 'cache'),
async.apply(Meta.css.loadFile, path.join(__dirname, '../../public/admin.css'), 'acpCache')
], function (err) {
callback(err);
});
};
function getStyleSource(files, prefix, extension, callback) {
var pluginDirectories = [],
source = '';
@ -127,7 +118,7 @@ module.exports = function (Meta) {
Meta.css.commitToFile = function (filename, callback) {
var file = (filename === 'acpCache' ? 'admin' : 'stylesheet') + '.css';
fs.writeFile(path.join(__dirname, '../../public/' + file), Meta.css[filename], function (err) {
fs.writeFile(path.join(__dirname, '../../build/public/' + file), Meta.css[filename], function (err) {
if (!err) {
winston.verbose('[meta/css] ' + file + ' committed to disk.');
} else {
@ -139,19 +130,6 @@ module.exports = function (Meta) {
});
};
Meta.css.loadFile = function (filePath, filename, callback) {
winston.verbose('[meta/css] Reading stylesheet ' + filePath.split('/').pop() + ' from file');
fs.readFile(filePath, function (err, file) {
if (err) {
return callback(err);
}
Meta.css[filename] = file;
callback();
});
};
function minify(source, paths, destination, callback) {
callback = callback || function () {};
less.render(source, {

@ -6,6 +6,7 @@ var path = require('path');
var async = require('async');
var nconf = require('nconf');
var fs = require('fs');
var mkdirp = require('mkdirp');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
@ -85,30 +86,34 @@ module.exports = function (Meta) {
}
}
};
Meta.js.bridgeModules = function (app, callback) {
// Add routes for AMD-type modules to serve those files
function addRoute(relPath) {
var relativePath = nconf.get('relative_path');
app.get(relativePath + '/src/modules/' + relPath, function (req, res) {
return res.sendFile(path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]), {
maxAge: app.enabled('cache') ? 5184000000 : 0
});
});
Meta.js.linkModules = function (callback) {
function link(filePath, destPath, cb) {
if (process.platform === 'win32') {
fs.link(filePath, destPath, cb);
} else {
fs.symlink(filePath, destPath, 'file', cb);
}
}
var numBridged = 0;
for(var relPath in Meta.js.scripts.modules) {
if (Meta.js.scripts.modules.hasOwnProperty(relPath)) {
addRoute(relPath);
++numBridged;
plugins.reload(function (err) {
if (err) {
return callback(err);
}
}
async.each(Object.keys(Meta.js.scripts.modules), function (relPath, next) {
var filePath = path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]);
var destPath = path.join(__dirname, '../../build/public/src/modules', relPath);
mkdirp(path.dirname(destPath), function (err) {
if (err) {
return next(err);
}
winston.verbose('[meta/js] ' + numBridged + ' of ' + Object.keys(Meta.js.scripts.modules).length + ' modules bridged');
callback();
link(filePath, destPath, next);
});
}, callback);
});
};
Meta.js.minify = function (target, callback) {
@ -202,48 +207,11 @@ module.exports = function (Meta) {
};
Meta.js.commitToFile = function (target, callback) {
fs.writeFile(path.join(__dirname, '../../public/' + target), Meta.js.target[target].cache, function (err) {
fs.writeFile(path.join(__dirname, '../../build/public/' + target), Meta.js.target[target].cache, function (err) {
callback(err);
});
};
Meta.js.getFromFile = function (target, callback) {
function readFile(filePath, next) {
fs.readFile(filePath, function (err, contents) {
if (err) {
if (err.code === 'ENOENT') {
if (!filePath.endsWith('.map')) {
winston.warn('[meta/js] ' + filePath + ' not found on disk, did you run ./nodebb build?');
}
return next(null, '');
}
}
next(err, contents);
});
}
var scriptPath = path.join(nconf.get('base_dir'), 'public/' + target);
var mapPath = path.join(nconf.get('base_dir'), 'public/' + target + '.map');
async.parallel({
script: function (next) {
readFile(scriptPath, next);
},
map: function (next) {
readFile(mapPath, next);
}
}, function (err, results) {
if (err) {
return callback(err);
}
Meta.js.target[target] = {
cache: results.script,
map: results.map
};
callback();
});
};
function setupDebugging() {
/**
* Check if the parent process is running with the debug option --debug (or --debug-brk)

@ -28,7 +28,7 @@ module.exports = function (Meta) {
Meta.sounds.getFiles = function (callback) {
async.waterfall([
function (next) {
fs.readdir(path.join(__dirname, '../../public/sounds'), next);
fs.readdir(path.join(__dirname, '../../build/public/sounds'), next);
},
function (sounds, next) {
fs.readdir(path.join(__dirname, '../../public/uploads/sounds'), function (err, uploaded) {
@ -88,7 +88,7 @@ module.exports = function (Meta) {
};
function setupSounds(callback) {
var soundsPath = path.join(__dirname, '../../public/sounds');
var soundsPath = path.join(__dirname, '../../build/public/sounds');
async.waterfall([
function (next) {

@ -48,7 +48,7 @@ module.exports = function (Meta) {
if (configObj.screenshot) {
configObj.screenshot_url = nconf.get('relative_path') + '/css/previews/' + configObj.id;
} else {
configObj.screenshot_url = nconf.get('relative_path') + '/images/themes/default.png';
configObj.screenshot_url = nconf.get('relative_path') + '/assets/images/themes/default.png';
}
next(null, configObj);
} catch (err) {

@ -101,7 +101,7 @@ module.exports = function (middleware) {
plugins: results.custom_header.plugins,
authentication: results.custom_header.authentication,
scripts: results.scripts,
'cache-buster': meta.config['cache-buster'] ? 'v=' + meta.config['cache-buster'] : '',
'cache-buster': meta.config['cache-buster'] || '',
env: process.env.NODE_ENV ? true : false,
title: (acpPath || 'Dashboard') + ' | NodeBB Admin Control Panel',
bodyClass: data.bodyClass

@ -46,7 +46,7 @@ module.exports = function (middleware) {
bootswatchCSS: meta.config['theme:src'],
title: meta.config.title || '',
description: meta.config.description || '',
'cache-buster': meta.config['cache-buster'] ? 'v=' + meta.config['cache-buster'] : '',
'cache-buster': meta.config['cache-buster'] || '',
'brand:logo': meta.config['brand:logo'] || '',
'brand:logo:url': meta.config['brand:logo:url'] || '',
'brand:logo:alt': meta.config['brand:logo:alt'] || '',

@ -145,21 +145,47 @@ module.exports = function (app, middleware, hotswapIds) {
}
app.use(middleware.privateUploads);
app.use(relativePath + '/assets', express.static(path.join(__dirname, '../../', 'build/public'), {
app.use(relativePath + '/assets', express.static(path.join(__dirname, '../../build/public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
app.use(relativePath + '/assets', express.static(path.join(__dirname, '../../public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
// DEPRECATED
var deprecatedPaths = [
'/nodebb.min.js',
'/acp.min.js',
'/stylesheet.css',
'/js-enabled.css',
'/admin.css',
'/logo.png',
'/favicon.ico',
'/vendor/',
'/uploads/',
'/templates/',
'/src/',
'/images/',
// '/sounds/',
];
app.use(relativePath, function (req, res, next) {
if (deprecatedPaths.some(function (path) { return req.path.startsWith(path); })) {
winston.warn('[deprecated] Accessing `' + req.path.slice(1) + '` from `/` is deprecated. ' +
'Use `/assets' + req.path + '` to access this file.');
res.redirect(relativePath + '/assets' + req.path + '?' + meta.config['cache-buster']);
} else {
next();
}
});
// DEPRECATED
app.use(relativePath + '/api/language', function (req, res) {
winston.warn('[deprecated] Accessing language files from `/api/language` is deprecated. ' +
'Use `/assets/language/[langCode]/[namespace].json` for prefetch paths.');
'Use `/assets/language' + req.path + '.json` for prefetch paths.');
res.redirect(relativePath + '/assets/language' + req.path + '.json?' + meta.config['cache-buster']);
});
app.use(relativePath, express.static(path.join(__dirname, '../../', 'public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
app.use(relativePath + '/vendor/jquery/timeago/locales', middleware.processTimeagoLocales);
app.use(relativePath + '/assets/vendor/jquery/timeago/locales', middleware.processTimeagoLocales);
app.use(controllers.handle404);
app.use(controllers.handleURIErrors);
app.use(controllers.handleErrors);

@ -5,31 +5,6 @@ var nconf = require('nconf');
var meta = require('../meta');
function sendMinifiedJS(req, res) {
var target = path.basename(req.path);
var cache = meta.js.target[target] ? meta.js.target[target].cache : '';
res.type('text/javascript').send(cache);
}
// The portions of code involving the source map are commented out as they're broken in UglifyJS2
// Follow along here: https://github.com/mishoo/UglifyJS2/issues/700
// function sendJSSourceMap(req, res) {
// if (meta.js.hasOwnProperty('map')) {
// res.type('application/json').send(meta.js.map);
// } else {
// res.redirect(404);
// }
// };
function sendStylesheet(req, res) {
res.type('text/css').status(200).send(meta.css.cache);
}
function sendACPStylesheet(req, res) {
res.type('text/css').status(200).send(meta.css.acpCache);
}
function sendSoundFile(req, res, next) {
var resolved = meta.sounds._filePathHash[path.basename(req.path)];
@ -41,11 +16,6 @@ function sendSoundFile(req, res, next) {
}
module.exports = function (app, middleware, controllers) {
app.get('/stylesheet.css', middleware.addExpiresHeaders, sendStylesheet);
app.get('/admin.css', middleware.addExpiresHeaders, sendACPStylesheet);
app.get('/nodebb.min.js', middleware.addExpiresHeaders, sendMinifiedJS);
app.get('/acp.min.js', middleware.addExpiresHeaders, sendMinifiedJS);
// app.get('/nodebb.min.js.map', middleware.addExpiresHeaders, sendJSSourceMap);
app.get('/sitemap.xml', controllers.sitemap.render);
app.get('/sitemap/pages.xml', controllers.sitemap.getPages);
app.get('/sitemap/categories.xml', controllers.sitemap.getCategories);

@ -4,8 +4,8 @@
<title>{title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{relative_path}/admin.css?{cache-buster}" />
<link rel="stylesheet" type="text/css" href="{relative_path}/vendor/mdl/mdl.min.css?{cache-buster}" />
<link rel="stylesheet" type="text/css" href="{relative_path}/assets/admin.css?{cache-buster}" />
<link rel="stylesheet" type="text/css" href="{relative_path}/assets/vendor/mdl/mdl.min.css?{cache-buster}" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<script>
@ -21,17 +21,17 @@
</script>
<script src="https://storage.googleapis.com/code.getmdl.io/1.0.3/material.min.js"></script>
<script type="text/javascript" src="{relative_path}/vendor/jquery/sortable/Sortable.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/acp.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/colorpicker/colorpicker.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/src/admin/admin.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/ace/ace.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/semver/semver.browser.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/jquery/serializeObject/jquery.ba-serializeobject.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/jquery/deserialize/jquery.deserialize.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/snackbar/snackbar.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/slideout/slideout.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/vendor/nprogress.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/jquery/sortable/Sortable.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/acp.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/colorpicker/colorpicker.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/src/admin/admin.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/ace/ace.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/semver/semver.browser.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/jquery/serializeObject/jquery.ba-serializeobject.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/jquery/deserialize/jquery.deserialize.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/snackbar/snackbar.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/slideout/slideout.min.js?{cache-buster}"></script>
<script type="text/javascript" src="{relative_path}/assets/vendor/nprogress.min.js?{cache-buster}"></script>
<!-- BEGIN scripts -->
<script type="text/javascript" src="{scripts.src}"></script>

@ -43,7 +43,7 @@
<p class="help-block">
[[admin/settings/group:default-cover-help]]
</p>
<input type="text" class="form-control input-lg" id="groups:defaultCovers" data-field="groups:defaultCovers" value="{config.relative_path}/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" /><br />
<input type="text" class="form-control input-lg" id="groups:defaultCovers" data-field="groups:defaultCovers" value="{config.relative_path}/assets/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" /><br />
</form>
</div>
</div>

@ -131,7 +131,7 @@
<p class="help-block">
[[admin/settings/uploads:default-covers-help]]
</p>
<input type="text" class="form-control input-lg" id="profile:defaultCovers" data-field="profile:defaultCovers" value="{config.relative_path}/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" />
<input type="text" class="form-control input-lg" id="profile:defaultCovers" data-field="profile:defaultCovers" value="{config.relative_path}/assets/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" />
</form>
</div>
</div>

@ -88,7 +88,6 @@ function initializeNodeBB(callback) {
plugins.init(app, middleware, next);
},
async.apply(plugins.fireHook, 'static:assets.prepare', {}),
async.apply(meta.js.bridgeModules, app),
function (next) {
plugins.fireHook('static:app.preload', {
app: app,
@ -104,9 +103,6 @@ function initializeNodeBB(callback) {
},
function (next) {
async.series([
async.apply(meta.js.getFromFile, 'nodebb.min.js'),
async.apply(meta.js.getFromFile, 'acp.min.js'),
async.apply(meta.css.getFromFile),
async.apply(meta.sounds.init),
async.apply(languages.init),
async.apply(meta.blacklist.load)

@ -3,6 +3,7 @@
var fs = require('fs');
var path = require('path');
var async = require('async');
var nconf = require('nconf');
var plugins = require('../plugins');
var admin = {};
@ -25,7 +26,7 @@ admin.get = function (callback) {
plugins.fireHook('filter:widgets.getWidgets', [], next);
},
adminTemplate: function (next) {
fs.readFile(path.resolve(__dirname, '../../public/templates/admin/partials/widget-settings.tpl'), 'utf8', next);
fs.readFile(path.resolve(nconf.get('views_dir'), 'admin/partials/widget-settings.tpl'), 'utf8', next);
}
}, function (err, widgetData) {
if (err) {

@ -20,7 +20,7 @@
base_dir: path.join(__dirname,'../..'),
themes_path: path.join(__dirname, '../../node_modules'),
upload_url: path.join(path.sep, '../../uploads', path.sep),
views_dir: path.join(__dirname, '../../public/templates'),
views_dir: path.join(__dirname, '../../build/public/templates'),
relative_path: ''
});

Loading…
Cancel
Save