延迟加载的 vue-router 组件不适用于 SSR
Lazy-Loaded vue-router components don't work with SSR
我有两个不同的入口点 server.js 和 client.js。(我正在使用 vue-server-renderer 和 laravel-mix)-(我的 server.js 和 client.js 看起来与此处描述的完全一样 - spatie/laravel-server-side-rendering 如果我进行静态导出 import Test from '../views/Test'
它可以工作..
如果我尝试在没有延迟加载的情况下导入路由,SSR 工作:
import Test from "../views/Test";
export const routes = [{
path: '/my-route',
name: "Test",
component: Test,
}]
但是如果我尝试延迟加载,它在 SSR 上会失败:
export const routes = [{
path: '/my-route',
name: "Test"
component: () => import('../views/Test.vue'),
}]
Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
对于() => import('../views/Home.vue)
,client.js有效,只有server.js无效。
我的server.js
:
import renderVueComponentToString from 'vue-server-renderer/basic';
import app from './app';
import {router} from './router/index';
new Promise((resolve, reject) => {
router.push(context.url);
router.onReady(() => {
resolve(app);
}, reject);
})
.then(app => {
renderVueComponentToString(app, (err, res) => {
if (err) throw new Error(err);
dispatch(res);
});
});
完整的错误是:
The command "/usr/bin/node /home/vagrant/Code/project/storage/app/ssr/1228cfee3f79dc5949bd898950384e53.js" failed Exit Code: 1(General error)
Working directory: /home/vagrant/Code/project/public Output:
================ Error Output: ================ internal/modules/cjs/loader.js:628 throw err; ^
Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
更新
我想我可能知道为什么会这样(我可能错了):
export const routes = [{
path: '/',
name: "Home",
component: () => import('../views/Home')
}]
使用这段代码,我得到一个错误:
Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
The command "/usr/bin/node /home/vagrant/Code/project/storage/app/ssr/717358e60bfd52035a1e58256cdfbba0.js" failed. Exit Code: 1(General error) Working directory: /home/vagrant/Code/project/public Output: ================ Error Output: ================ internal/modules/cjs/loader.js:628 throw err; ^ Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
查看路径:
在我的编译文件中(位于 public/js
)我有这一行:
var chunk = require("./js/chunks/server/" + ({}[chunkId]||chunkId) + ".js?id=" + {"0":"c3384f174123f0848451"}[chunkId] + "");
这似乎是一条相对路径。但是文件实际上是 运行 我在 config/ssr.php
- 'temp_path' => storage_path('app/ssr')
中指定的 - 所以它找不到路径。
然而,即使我将 temp_path
更改为 public_path()
以便它可以从 ./js/chunks/server/
(即 public/js/chunks/server/0.js
)中找到块,它仍然会抛出相同的错误错误。虽然SSR的temp_path不一样
The command "/usr/bin/node /home/vagrant/Code/project/public/3560d8d101faa4bdef316054b14873cc.js" failed. Exit Code: 1(General error) Working directory: /home/vagrant/Code/project/public Output: ================ Error Output: ================ internal/modules/cjs/loader.js:628 throw err; ^ Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
另外如果我 console.log(_dirname)
在 renderVueComponentToString()
它 returns 我 '/'
不确定问题出在哪里,但您应该阅读以下内容:
Note that it is still necessary to use router.onReady on both server and client before returning / mounting the app, because the router must resolve async route components ahead of time in order to properly invoke in-component hooks ... Vue SSR, Routing and Code-Splitting
所以不用
app.$mount('#app');
尝试
router.onReady(() => {
app.$mount('#app')
})
希望对您有所帮助。
我解决了它,现在它只能在客户端使用 SSR 和代码拆分 - 如果您有任何更好的想法,我仍然洗耳恭听。
我使用了 spatie/laravel-server-side-rendering,它的设置非常简单。
这是我的解决方案(以及我在 spatie/laravel-server-side-rendering 上所做的更改):
我学习了如何将捆绑包与 charlesBochet's comment 分开,但是我使用了一个文件而不是 2x webpack.mix.js
个文件。
- package.json
"scripts": {
// concurrently is just for building them asynchronously
"dev-all": "concurrently \"npm --section=server run dev\" \"npm --section=client run dev\" --kill-others-on-fail",
// can also build them separately if you wish
"dev-client": "npm --section=client run dev",
"dev-server": "npm --section=server run dev"
...
}
- webpack.mix.js
if (process.env.npm_config_section === 'server') {
mix.js('resources/js/app-server.js', 'public/js')
.webpackConfig({
target: 'node',
// Prevent code-splitting for server-build
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
})
],
})
// merge manifest is a package for merging manifests,
// otherwise they'll get overwritten by each other
// https://github.com/kabbouchi/laravel-mix-merge-manifest
.mergeManifest()
.version();
} else if (process.env.npm_config_section === 'client') {
mix.js('resources/js/app-client.js', 'public/js')
.webpackConfig({
target: 'web',
output: {
chunkFilename: 'js/chunks/[name].js?id=[chunkhash]',
publicPath: '/',
},
})
.mergeManifest()
.version();
// Only build css with the client build, server build only needs
// the html and not the css
mix.sass('resources/sass/app.scss', 'public/css')
} else {
console.log(
'\x1b[41m%s\x1b[0m',
'Provide correct --section argument to build command: server, client'
);
throw new Error('Provide correct --section argument to build command!')
}
- app-server.js - 应该等待路由器就绪
new Promise((resolve, reject) => {
router.push(context.url);
router.onReady(() => {
resolve(app);
}, reject);
})
.then(app => {
renderVueComponentToString(app, (err, res) => {
if (err) throw new Error(err);
dispatch(res);
});
});
- 应用-client.js
router.onReady(function() {
app.$mount('#app');
})
- 最后路由器文件代码分割适用于 app-client.js
export const routes = [
{
path: '/',
name: "Home",
component: () => import('../views/Home.vue')
},
我有两个不同的入口点 server.js 和 client.js。(我正在使用 vue-server-renderer 和 laravel-mix)-(我的 server.js 和 client.js 看起来与此处描述的完全一样 - spatie/laravel-server-side-rendering 如果我进行静态导出 import Test from '../views/Test'
它可以工作..
如果我尝试在没有延迟加载的情况下导入路由,SSR 工作:
import Test from "../views/Test";
export const routes = [{
path: '/my-route',
name: "Test",
component: Test,
}]
但是如果我尝试延迟加载,它在 SSR 上会失败:
export const routes = [{
path: '/my-route',
name: "Test"
component: () => import('../views/Test.vue'),
}]
Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
对于() => import('../views/Home.vue)
,client.js有效,只有server.js无效。
我的server.js
:
import renderVueComponentToString from 'vue-server-renderer/basic';
import app from './app';
import {router} from './router/index';
new Promise((resolve, reject) => {
router.push(context.url);
router.onReady(() => {
resolve(app);
}, reject);
})
.then(app => {
renderVueComponentToString(app, (err, res) => {
if (err) throw new Error(err);
dispatch(res);
});
});
完整的错误是:
The command "/usr/bin/node /home/vagrant/Code/project/storage/app/ssr/1228cfee3f79dc5949bd898950384e53.js" failed Exit Code: 1(General error)
Working directory: /home/vagrant/Code/project/public Output:
================ Error Output: ================ internal/modules/cjs/loader.js:628 throw err; ^
Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
更新
我想我可能知道为什么会这样(我可能错了):
export const routes = [{
path: '/',
name: "Home",
component: () => import('../views/Home')
}]
使用这段代码,我得到一个错误:
Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
The command "/usr/bin/node /home/vagrant/Code/project/storage/app/ssr/717358e60bfd52035a1e58256cdfbba0.js" failed. Exit Code: 1(General error) Working directory: /home/vagrant/Code/project/public Output: ================ Error Output: ================ internal/modules/cjs/loader.js:628 throw err; ^ Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
查看路径:
在我的编译文件中(位于 public/js
)我有这一行:
var chunk = require("./js/chunks/server/" + ({}[chunkId]||chunkId) + ".js?id=" + {"0":"c3384f174123f0848451"}[chunkId] + "");
这似乎是一条相对路径。但是文件实际上是 运行 我在 config/ssr.php
- 'temp_path' => storage_path('app/ssr')
中指定的 - 所以它找不到路径。
然而,即使我将 temp_path
更改为 public_path()
以便它可以从 ./js/chunks/server/
(即 public/js/chunks/server/0.js
)中找到块,它仍然会抛出相同的错误错误。虽然SSR的temp_path不一样
The command "/usr/bin/node /home/vagrant/Code/project/public/3560d8d101faa4bdef316054b14873cc.js" failed. Exit Code: 1(General error) Working directory: /home/vagrant/Code/project/public Output: ================ Error Output: ================ internal/modules/cjs/loader.js:628 throw err; ^ Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
另外如果我 console.log(_dirname)
在 renderVueComponentToString()
它 returns 我 '/'
不确定问题出在哪里,但您应该阅读以下内容:
Note that it is still necessary to use router.onReady on both server and client before returning / mounting the app, because the router must resolve async route components ahead of time in order to properly invoke in-component hooks ... Vue SSR, Routing and Code-Splitting
所以不用
app.$mount('#app');
尝试
router.onReady(() => {
app.$mount('#app')
})
希望对您有所帮助。
我解决了它,现在它只能在客户端使用 SSR 和代码拆分 - 如果您有任何更好的想法,我仍然洗耳恭听。
我使用了 spatie/laravel-server-side-rendering,它的设置非常简单。
这是我的解决方案(以及我在 spatie/laravel-server-side-rendering 上所做的更改):
我学习了如何将捆绑包与 charlesBochet's comment 分开,但是我使用了一个文件而不是 2x webpack.mix.js
个文件。
- package.json
"scripts": {
// concurrently is just for building them asynchronously
"dev-all": "concurrently \"npm --section=server run dev\" \"npm --section=client run dev\" --kill-others-on-fail",
// can also build them separately if you wish
"dev-client": "npm --section=client run dev",
"dev-server": "npm --section=server run dev"
...
}
- webpack.mix.js
if (process.env.npm_config_section === 'server') {
mix.js('resources/js/app-server.js', 'public/js')
.webpackConfig({
target: 'node',
// Prevent code-splitting for server-build
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
})
],
})
// merge manifest is a package for merging manifests,
// otherwise they'll get overwritten by each other
// https://github.com/kabbouchi/laravel-mix-merge-manifest
.mergeManifest()
.version();
} else if (process.env.npm_config_section === 'client') {
mix.js('resources/js/app-client.js', 'public/js')
.webpackConfig({
target: 'web',
output: {
chunkFilename: 'js/chunks/[name].js?id=[chunkhash]',
publicPath: '/',
},
})
.mergeManifest()
.version();
// Only build css with the client build, server build only needs
// the html and not the css
mix.sass('resources/sass/app.scss', 'public/css')
} else {
console.log(
'\x1b[41m%s\x1b[0m',
'Provide correct --section argument to build command: server, client'
);
throw new Error('Provide correct --section argument to build command!')
}
- app-server.js - 应该等待路由器就绪
new Promise((resolve, reject) => {
router.push(context.url);
router.onReady(() => {
resolve(app);
}, reject);
})
.then(app => {
renderVueComponentToString(app, (err, res) => {
if (err) throw new Error(err);
dispatch(res);
});
});
- 应用-client.js
router.onReady(function() {
app.$mount('#app');
})
- 最后路由器文件代码分割适用于 app-client.js
export const routes = [
{
path: '/',
name: "Home",
component: () => import('../views/Home.vue')
},