【发布时间】:2020-11-29 09:35:12
【问题描述】:
我正在构建一个安装了 vue-cli-plugin-compression 和 vue-cli-plugin-prerender-spa 的 vue cli 3 应用程序。 (在后台这些使用 prerender-spa-plugin 和 compression-webpack-plugin)。
prerender-spa-plugin 将 index.html 重命名为 app.html。然后它预渲染 app.html 并将生成的 html 存储在新的 index.html 中。该页面已正确预呈现,并且 app.html 已正确压缩。但是,生成的 index.html(作为预渲染结果的页面)是 未 gzip 压缩的。我怎样才能让预渲染的结果也被压缩?
这是我的 vue.config.js:
module.exports = {
devServer: {
port: 3000
},
configureWebpack: {
devtool: 'source-map'
},
pluginOptions: {
prerenderSpa: {
customRendererConfig: {
injectProperty: '__PRERENDER_INJECTED',
inject: {},
},
registry: undefined,
renderRoutes: [
'/'
],
useRenderEvent: true,
headless: true,
onlyProduction: true,
postProcess: route => {
// Defer scripts and tell Vue it's been server rendered to trigger hydration
route.html = route.html
.replace(/<script (.*?)>/g, '<script $1 defer>')
.replace('id="app"', 'id="app" data-server-rendered="true"');
return route;
}
},
compression:{
gzip: {
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.(js|js\.map|css|html)$/,
minRatio: 0.8,
}
}
}
};
我尝试在压缩之前进行预渲染,但它并没有改变任何东西:
chainWebpack: (config) => {
config.plugin('pre-render').before('gzip-compression');
config.plugin('gzip-compression').after('html');
},
【问题讨论】:
-
我一直在寻找解决同样问题的方法,但是 prerender 插件在其他所有内容之后运行。所以它不会工作,我想你需要重新运行压缩脚本之后
标签: javascript vue.js webpack single-page-application prerender