From c7d98c1f5a6f383aa71995ad395b0e634ce358ba Mon Sep 17 00:00:00 2001 From: okihouse Date: Fri, 21 Sep 2018 18:48:44 +0900 Subject: [PATCH] Add manifest for assets caching --- package.json | 3 +- pom.xml | 19 +++++----- .../java/com/okihouse/config/MvcConfig.java | 14 ++++++++ .../okihouse/controller/IndexController.java | 11 +----- .../com/okihouse/interceptor/Interceptor.java | 36 +++++++++++++++++++ src/main/resources/templates/index.hbs | 6 +--- .../resources/templates/shared/header.hbs | 6 +--- webpack.config.js | 16 +++++---- 8 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/okihouse/interceptor/Interceptor.java diff --git a/package.json b/package.json index 668681b..814fef0 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "typescript": "^2.9.2", "webpack": "^4.16.1", "webpack-cli": "^3.1.0", - "webpack-dev-server": "^3.1.4" + "webpack-dev-server": "^3.1.4", + "webpack-manifest-plugin": "^2.0.4" } } diff --git a/pom.xml b/pom.xml index 4cddc4d..8a264fa 100644 --- a/pom.xml +++ b/pom.xml @@ -48,22 +48,23 @@ lombok true - - - org.apache.commons - commons-lang3 - 3.4 - + + + com.fasterxml.jackson.core + jackson-core + 2.8.6 + - com.fasterxml.uuid - java-uuid-generator - 3.1.3 + com.fasterxml.jackson.core + jackson-databind + 2.8.6 org.springframework.boot spring-boot-devtools + provided diff --git a/src/main/java/com/okihouse/config/MvcConfig.java b/src/main/java/com/okihouse/config/MvcConfig.java index 7773c0b..2980d8a 100644 --- a/src/main/java/com/okihouse/config/MvcConfig.java +++ b/src/main/java/com/okihouse/config/MvcConfig.java @@ -1,7 +1,11 @@ package com.okihouse.config; +import com.okihouse.interceptor.Interceptor; + import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -18,5 +22,15 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) { .addResourceHandler("/**") .addResourceLocations("classpath:/"); } + + @Bean + public Interceptor interceptor(){ + return new Interceptor(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(interceptor()); + } } diff --git a/src/main/java/com/okihouse/controller/IndexController.java b/src/main/java/com/okihouse/controller/IndexController.java index c8fd827..8e9e239 100644 --- a/src/main/java/com/okihouse/controller/IndexController.java +++ b/src/main/java/com/okihouse/controller/IndexController.java @@ -1,9 +1,6 @@ package com.okihouse.controller; -import org.springframework.ui.Model; -import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -14,14 +11,8 @@ @RequestMapping(value = "/") public class IndexController { - @Autowired - private Environment environment; - @RequestMapping(method = RequestMethod.GET) - public String index(Model model) { - boolean isProduction = environment.acceptsProfiles("production"); - model.addAttribute("isProduction", isProduction); - + public String index() { return "index"; } } diff --git a/src/main/java/com/okihouse/interceptor/Interceptor.java b/src/main/java/com/okihouse/interceptor/Interceptor.java new file mode 100644 index 0000000..1f7eb79 --- /dev/null +++ b/src/main/java/com/okihouse/interceptor/Interceptor.java @@ -0,0 +1,36 @@ +package com.okihouse.interceptor; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.type.TypeReference; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import com.fasterxml.jackson.core.type.TypeReference; + +public class Interceptor extends HandlerInterceptorAdapter { + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + Resource resource = new ClassPathResource("dist/manifest.json"); + if(resource.exists()) { + ObjectMapper objectMapper = new ObjectMapper(); + HashMap manifests = objectMapper.readValue(resource.getFile(), new TypeReference>(){}); + + manifests.forEach((k, v) -> { + modelAndView.addObject(k, v); + }); + } + + super.postHandle(request, response, handler, modelAndView); + } + +} diff --git a/src/main/resources/templates/index.hbs b/src/main/resources/templates/index.hbs index 6015004..26156cb 100644 --- a/src/main/resources/templates/index.hbs +++ b/src/main/resources/templates/index.hbs @@ -4,10 +4,6 @@
{{> shared/scripts}} - {{#if isProduction}} - - {{else}} - - {{/if}} + \ No newline at end of file diff --git a/src/main/resources/templates/shared/header.hbs b/src/main/resources/templates/shared/header.hbs index b3dc8cd..19a6473 100644 --- a/src/main/resources/templates/shared/header.hbs +++ b/src/main/resources/templates/shared/header.hbs @@ -4,9 +4,5 @@ spring-boot-webpack - {{#if isProduction}} - - {{else}} - - {{/if}} + \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 917e8ed..4ae1800 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,14 +5,18 @@ const path = require('path'); const CleanWebpackPlugin = require('clean-webpack-plugin'); const ExtractTextPlugin = require('extract-text-webpack-plugin'); const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); +const ManifestPlugin = require('webpack-manifest-plugin'); const webpack = require('webpack'); module.exports = function(env, argv) { const isProduction = argv.mode == 'production'; - const cssAssetName = isProduction ? '[name].min.css' : '[name].css'; + const cssAssetName = isProduction ? '[name].min.css?v=[hash]' : '[name].css?v=[hash]'; const plugins = [ - new ExtractTextPlugin(cssAssetName) + new ExtractTextPlugin(cssAssetName), + new ManifestPlugin({ + publicPath: '/dist/' + }) ]; if(isProduction) { @@ -35,10 +39,10 @@ module.exports = function(env, argv) { ] }, output: { - filename: isProduction ? '[name].min.js' : '[name].js', + filename: isProduction ? '[name].min.js?v=[hash]' : '[name].js?v=[hash]', path: path.join(__dirname, '/src/main/resources/dist') }, - devtool: isProduction ? 'source-map' : 'none', + devtool: isProduction ? 'none' : 'source-map', devServer: { historyApiFallback: true, contentBase: path.join(__dirname, 'src'), @@ -72,13 +76,13 @@ module.exports = function(env, argv) { options: { url: false, minimize: isProduction, - sourceMap: isProduction + sourceMap: !isProduction } }, { loader: 'sass-loader', options: { - sourceMap: isProduction + sourceMap: !isProduction } } ]