diff --git a/services/web/app/src/infrastructure/ExpressLocals.js b/services/web/app/src/infrastructure/ExpressLocals.js index 7a89154e49..87f6d16532 100644 --- a/services/web/app/src/infrastructure/ExpressLocals.js +++ b/services/web/app/src/infrastructure/ExpressLocals.js @@ -78,6 +78,12 @@ module.exports = function(webRouter, privateApiRouter, publicApiRouter) { staticFilesBase = '' } + res.locals.buildBaseAssetPath = function() { + // Return the base asset path (including the CDN url) so that webpack can + // use this to dynamically fetch scripts (e.g. PDFjs worker) + return Url.resolve(staticFilesBase, '/') + } + res.locals.buildJsPath = function(jsFile) { let path if (IS_DEV_ENV) { @@ -89,7 +95,7 @@ module.exports = function(webRouter, privateApiRouter, publicApiRouter) { // In production: resolve path from webpack manifest file // We are guaranteed to have a manifest file since webpack compiles in // the build - path = webpackManifest[jsFile] + path = `/${webpackManifest[jsFile]}` } return Url.resolve(staticFilesBase, path) @@ -97,7 +103,7 @@ module.exports = function(webRouter, privateApiRouter, publicApiRouter) { // Temporary hack while jQuery/Angular dependencies are *not* bundled, // instead copied into output directory - res.locals.buildCopiedJsAssetPath = function(jsFile, opts = {}) { + res.locals.buildCopiedJsAssetPath = function(jsFile) { let path if (IS_DEV_ENV) { // In dev: resolve path to root directory @@ -108,27 +114,17 @@ module.exports = function(webRouter, privateApiRouter, publicApiRouter) { // In production: resolve path from webpack manifest file // We are guaranteed to have a manifest file since webpack compiles in // the build - path = webpackManifest[jsFile] + path = `/${webpackManifest[jsFile]}` } - if (opts.cdn !== false) { - path = Url.resolve(staticFilesBase, path) - } - - if (opts.qs) { - path = path + '?' + querystring.stringify(opts.qs) - } - - return path + return Url.resolve(staticFilesBase, path) } - res.locals.mathJaxPath = res.locals.buildCopiedJsAssetPath( - 'js/libs/mathjax/MathJax.js', + res.locals.mathJaxPath = `/js/libs/mathjax/MathJax.js?${querystring.stringify( { - cdn: false, - qs: { config: 'TeX-AMS_HTML,Safe' } + config: 'TeX-AMS_HTML,Safe' } - ) + )}` res.locals.lib = PackageVersions.lib @@ -169,7 +165,7 @@ module.exports = function(webRouter, privateApiRouter, publicApiRouter) { // In production: resolve path from webpack manifest file // We are guaranteed to have a manifest file since webpack compiles in // the build - path = webpackManifest[cssFileName] + path = `/${webpackManifest[cssFileName]}` } return Url.resolve(staticFilesBase, path) diff --git a/services/web/app/views/layout.pug b/services/web/app/views/layout.pug index 054c5693ef..8c489c1dba 100644 --- a/services/web/app/views/layout.pug +++ b/services/web/app/views/layout.pug @@ -47,6 +47,9 @@ html( script(type="text/javascript"). window.csrfToken = "#{csrfToken}"; + //- Configure dynamically loaded assets (via webpack) to be downloaded from CDN + //- See: https://webpack.js.org/guides/public-path/#on-the-fly + window.baseAssetPath = "#{buildBaseAssetPath()}" script(src=buildCopiedJsAssetPath('js/libs/jquery-1.11.1.min.js')) script(type="text/javascript"). diff --git a/services/web/frontend/js/base.js b/services/web/frontend/js/base.js index a319f7881f..a06f41ac7f 100644 --- a/services/web/frontend/js/base.js +++ b/services/web/frontend/js/base.js @@ -14,6 +14,11 @@ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ + +// Configure dynamically loaded assets (via webpack) to be downloaded from CDN +// See: https://webpack.js.org/guides/public-path/#on-the-fly +__webpack_public_path__ = window.baseAssetPath + define([ 'libraries', 'libs/polyfills/symbol', diff --git a/services/web/webpack.config.js b/services/web/webpack.config.js index 110c6356f0..5c78d51c22 100644 --- a/services/web/webpack.config.js +++ b/services/web/webpack.config.js @@ -41,9 +41,6 @@ module.exports = { output: { path: path.join(__dirname, '/public'), - // Serve from the root of public directory - publicPath: '/', - // By default write into js directory filename: 'js/[name].js',