Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manifest for assets caching #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
19 changes: 10 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,23 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>

<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>3.1.3</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
</dependency>

<!-- for local test -->
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/okihouse/config/MvcConfig.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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());
}

}
11 changes: 1 addition & 10 deletions src/main/java/com/okihouse/controller/IndexController.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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";
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/okihouse/interceptor/Interceptor.java
Original file line number Diff line number Diff line change
@@ -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<String, String> manifests = objectMapper.readValue(resource.getFile(), new TypeReference<HashMap<String, String>>(){});

manifests.forEach((k, v) -> {
modelAndView.addObject(k, v);
});
}

super.postHandle(request, response, handler, modelAndView);
}

}
6 changes: 1 addition & 5 deletions src/main/resources/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<div id="container"></div>
{{> shared/scripts}}

{{#if isProduction}}
<script src="/dist/index.min.js"></script>
{{else}}
<script src="/dist/index.js"></script>
{{/if}}
<script src="{{{[index.js]}}}"></script>
</body>
</html>
6 changes: 1 addition & 5 deletions src/main/resources/templates/shared/header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@

<title>spring-boot-webpack</title>

{{#if isProduction}}
<link rel="stylesheet" type="text/css" href="/dist/style.min.css">
{{else}}
<link rel="stylesheet" type="text/css" href="/dist/style.css">
{{/if}}
<link rel="stylesheet" type="text/css" href="{{{[style.css]}}}">
</head>
16 changes: 10 additions & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'),
Expand Down Expand Up @@ -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
}
}
]
Expand Down