当我们在开发模式下运行 Vuetify 应用程序时,使用npm run dev
,工作正常。
然而,当我们使用prerender-spa-plugin
,Vuetify CSS 文件正确加载,但所有 JavaScript 组件都不起作用(即单击按钮打开抽屉不起作用等)。
我们尝试将脚本标签硬编码到 Vuetify 的 CDN,但这没有奏效。
我们在 GitHub 上搜索了使用 Vuetify 和 prerender-spa-plugin 的项目,但没有发现我们的代码和其他人的代码之间有任何差异。
我们尝试使用 Renderer() 的设置来解决该错误,但无法确定问题的根本原因。
Webpack 配置设置:
webpackConfig.plugins.push(
new PrerenderSPAPlugin({
// Required - The path to the webpack-outputted app to prerender.
staticDir: path.join(__dirname, '..', 'dist'),
// Required - Routes to render.
routes: [ '/', '/about-us.html', '/act-now.html' ],
postProcess (renderedRoute) {
// Ignore any redirects.
renderedRoute.route = renderedRoute.originalRoute
// Basic whitespace removal. (Don't use this in production.)
renderedRoute.html = renderedRoute.html.split(/>[\s]+</gmi).join('><')
// Remove /index.html from the output path if the dir name ends with a .html file extension.
// For example: /dist/dir/special.html/index.html -> /dist/dir/special.html
if (renderedRoute.route.endsWith('.html')) {
renderedRoute.outputPath = path.join(__dirname, '..', 'dist', renderedRoute.route)
}
return renderedRoute
},
renderer: new Renderer({
renderAfterDocumentEvent: 'app.rendered',
headless: false
})
})
)
main.js
import '@babel/polyfill'
import 'core-js/es6/promise'
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import '@/plugins/vuetify'
import App from './App'
import router from './router'
import 'vuetify/dist/vuetify.min.css'
import '../static/css/ms-theme.css'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: h => h(App),
mounted () {
document.dispatchEvent(new Event('app.rendered'))
}
})