From f74641a5488a8a0d759dd56390d5f525630515bb Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 14 Mar 2018 20:24:26 +0100 Subject: [PATCH] Cache busting --- .../components/QuestionBoxBuilder.js | 4 +- middleware/staticTemplate.js | 37 +++ package.json | 3 +- views/admin.ejs | 4 +- views/article.ejs | 2 +- views/auth-callback.ejs | 2 +- views/embed/stream.ejs | 6 +- views/login.ejs | 4 +- webpack.config.js | 185 ++++++++------ yarn.lock | 235 ++++++++++-------- 10 files changed, 296 insertions(+), 186 deletions(-) diff --git a/client/coral-embed-stream/src/tabs/configure/components/QuestionBoxBuilder.js b/client/coral-embed-stream/src/tabs/configure/components/QuestionBoxBuilder.js index 30fd8842c..604eb1179 100644 --- a/client/coral-embed-stream/src/tabs/configure/components/QuestionBoxBuilder.js +++ b/client/coral-embed-stream/src/tabs/configure/components/QuestionBoxBuilder.js @@ -25,7 +25,9 @@ class QuestionBoxBuilder extends React.Component { async loadEditor() { const { default: MarkdownEditor, - } = await import('coral-framework/components/MarkdownEditor'); + } = await import(/* webpackChunkName: "markdownEditor" */ + + 'coral-framework/components/MarkdownEditor'); return this.setState({ loading: false, diff --git a/middleware/staticTemplate.js b/middleware/staticTemplate.js index 5a7c34b60..61c9ef59f 100644 --- a/middleware/staticTemplate.js +++ b/middleware/staticTemplate.js @@ -1,4 +1,8 @@ const SettingsService = require('../services/settings'); +const fs = require('fs'); +const path = require('path'); +const merge = require('lodash/merge'); +const memoize = require('lodash/memoize'); const { BASE_URL, @@ -34,6 +38,37 @@ const attachStaticLocals = locals => { } }; +function getManifest() { + return merge( + {}, + ...['../dist/manifest.json', '../dist/manifest.embed.json'] + .map(f => fs.readFileSync(path.resolve(__dirname, f), 'utf8')) + .map(JSON.parse) + ); +} + +const getManifestMemoized = memoize(getManifest); + +if (process.env.NODE_ENV === 'production') { + // Crash early if file does not exists. + getManifestMemoized(); +} + +function resolve(key) { + if (process.env.NODE_ENV === 'production') { + return `${STATIC_URL}static/${getManifestMemoized()[key]}`; + } else { + // In dev mode, we are more forgiving and we always load the + // newest version of the manifest. + try { + return `${STATIC_URL}static/${getManifest()[key]}`; + } catch (err) { + console.warn(err); + return ''; + } + } +} + module.exports = async (req, res, next) => { try { // Attach the custom css url. @@ -46,6 +81,8 @@ module.exports = async (req, res, next) => { // Always attach the locals. attachStaticLocals(res.locals); + res.locals.resolve = resolve; + // Forward the request. next(); }; diff --git a/package.json b/package.json index 29becde3e..0aa4f247b 100644 --- a/package.json +++ b/package.json @@ -116,7 +116,6 @@ "graphql-tag": "^1.2.3", "graphql-tools": "^0.10.1", "hammerjs": "^2.0.8", - "hard-source-webpack-plugin": "^0.6.0", "helmet": "3.8.2", "history": "^3.0.0", "hjson": "^3.1.1", @@ -222,11 +221,13 @@ "lint-staged": "^7.0.0", "mocha": "^3.1.2", "mocha-junit-reporter": "^1.12.1", + "name-all-modules-plugin": "^1.0.1", "nightwatch": "^0.9.16", "nodemon": "^1.11.0", "selenium-standalone": "^6.11.0", "sinon": "^3.2.1", "sinon-chai": "^2.13.0", + "webpack-manifest-plugin": "^2.0.0-rc.2", "yaml-lint": "^1.0.0" }, "engines": { diff --git a/views/admin.ejs b/views/admin.ejs index 36356f797..c3ccf5aad 100644 --- a/views/admin.ejs +++ b/views/admin.ejs @@ -5,7 +5,7 @@ Talk - Coral Admin - +