2016-12-03 56 views
1

在我的春节,启动/应用程序做出反应,我想缓存bundle.js(1.2MB)。如果我使用http://(test-envionment-server-url)/myapp运行应用程序,则每次刷新页面时,我的bundle.js都会从状态304的缓存(208 B)中调用。到现在为止一切都很好。 但是当我使用https://(test-envionment-server-url)/myapp bundle.js不被缓存,每次我刷新它得到所有1.2MB,但所有其他图像和字体得到缓存。的WebPack /反应,和/春启动:缓存bundle.js以https

我在本地使用Tomcat和JBoss的7.0.x在我的测试环境

这里是我的webpack.config.js

const webpack = require('webpack'); 
const autoprefixer = require('autoprefixer'); 

module.exports = { 
cache: true, 
entry: [ 
    'webpack-dev-server/client?http://localhost:8008', 
    'webpack/hot/only-dev-server', 
    'babel-polyfill', 
    './src/index.js', 
], 
module: { 
    loaders: [ 
    { 
     test: /\.jsx?$/, 
    exclude: /node_modules/, 
    loader: 'react-hot!babel', 
    }, 
    { 
    test: /\.(jpe?g|png|gif|svg)$/i, 
    loaders: [ 
     'url?limit=2048', 
     'img', 
    ], 
    }, 
    { 
    test: /\.(woff|woff2|eot|ttf)$/, 
    loader: 'url-loader?limit=2048', 
    }, 
    { 
    test: /\.scss$/, 
    loaders: ['style', 'css', 'postcss-loader', 'sass'], 
    }, 
], 
    }, 
    resolve: { 
    extensions: ['', '.js', '.jsx'], 
    }, 
    output: { 
    path:`${__dirname}/(spring-boot-app-url/resources/static`, 
    publicPath: '/publicpath/', 
    filename: 'bundle.js', 
}, 
devServer: { 
port: 8008, 
contentBase: './dist', 
historyApiFallback: true, 
hot: true, 
proxy: { 
.....(proxy config) 
    }, 
    }, 
}, 
    }, 
    postcss:() => { 
return [autoprefixer]; 
}, 
    plugins: [ 
new webpack.HotModuleReplacementPlugin(), 
new webpack.optimize.UglifyJsPlugin(), 
new webpack.optimize.DedupePlugin(), 
new webpack.DefinePlugin({ 
    'process.env': { 
    'NODE_ENV': JSON.stringify('production'), 
    }, 
}), 
], 
    }; 
在我的春天

我有一些春天的安全配置:

@Override 
protected void configure(HttpSecurity httpSecurity) throws Exception { 
    httpSecurity 

      .csrf().disable() 

      .authorizeRequests() 
      .antMatchers(TOKEN_NAME).permitAll() // API 
      ...(antMatchers) 
      .and() 
      .authorizeRequests() 
      .antMatchers(TOKEN_BASED_AUTH_ENTRY_POINT).permitAll(); 

    httpSecurity 
      .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); 

    httpSecurity.headers().cacheControl().disable(); 
} 

这里是从我的网络标签中的请求首部:

Accept:*/* 
Accept-Encoding:gzip, deflate, sdch, br 
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 
Connection:keep-alive 
Host:***** 
Referer:https://****/appname/ 
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36 

这里是从网络选项卡中的标题输入反应:

Accept-Ranges:bytes 
Cache-Control:max-age=3600 
Connection:keep-alive 
Content-Length:1248915 
Content-Type:application/javascript; charset=UTF-8 
Date:Mon, 05 Dec 2016 12:56:47 GMT 
Last-Modified:Mon, 05 Dec 2016 09:08:06 GMT 
Server:JBoss-EAP/7 
Strict-Transport-Security:max-age=31536000 ; includeSubDomains 
X-Content-Type-Options:nosniff 
X-Frame-Options:DENY 
X-Powered-By:Undertow/1 
X-XSS-Protection:1; mode=block 

当我与HTTP URL运行它,头请求没有任何缓存控制。 关于如何缓存bundle.js文件的任何想法,即使我使用https运行我的应用程序?

谢谢

+0

,你能否告诉我们这个特定资源的请求和响应HTTP标头? –

+0

我添加了https请求和响应头,它们和http之间的区别在于为https添加了Cache-control。 – METTAIBI

+0

你是如何得到这些标题的?您是否在浏览器中设置了“禁用缓存”选项,或者是否使用“Ctrl + R”刷新了浏览器选项卡?如果不是,'Cache-Control:max-age = 0'意味着问题来自您的客户端并且服务器的行为正常。 –

回答

1

鉴于所提供的HTTP响应,似乎您的服务器不发送适当的Cache-Control头。当您更改静态资源的内容时,这样做可能会导致问题。

这就是为什么你应该:

  • 配置提供静态资源时
  • 配置与内容策略(see here

整体配置的资源链发送一个Cache-Control为您的生产概况应该是这样的:

spring.resources.cache-period=3600 # caching resources for 1 hour 
spring.resources.chain.strategy.content.enabled=true 
spring.resources.chain.strategy.content.paths=/** 
+0

现在在绑定响应中,我看到Cache-Control:max-age = 3600,但它仍然加载了1.2 MB的文件夹 – METTAIBI

+0

,然后不使用“ctrl + R”刷新并取消选中chrome中的“disable cache”选项devtools网络选项卡。这样做会向服务器发送“Cache-Control:max-age = 0”并强制重新下载资源。没有这些,你会看到资源从缓存中加载。 –

+0

我真的应该感谢你的回答我愚蠢的问题。目标是当我刷新时,我不希望服务器重新下载资源。我如何检查资源是否从缓存加载,因为我仍然看到1.2Mb,而不是(从磁盘缓存加载)或208B,因为它显示的是http。你能否澄清更多? – METTAIBI

1

的问题是在我的JBoss配置:

在standalone.xml我加入gzip压缩性能:

<system-properties> 
    <property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION" value="on"/> 
    <property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION_MIME_TYPES" value="text/javascript,text/css,text/html,application/xml,application/json"/> 
    <property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION_MIN_SIZE" value="1024"/> 
</system-properties> 

权下扩展。