sen*_*nty 5 javascript webpack vue.js vue-router laravel-mix
我有两个不同的 server.js 和 client.js 入口点。(我使用的是 vue-server-renderer 和 laravel-mix)-(我的 server.js 和 client.js 看起来与这里描述的完全一样-spatie/laravel-服务器端渲染,如果我进行静态导出,import Test from '../views/Test'它就可以工作..
如果我尝试在没有延迟加载的情况下导入路由,SSR 可以工作:
import Test from "../views/Test";
export const routes = [{
path: '/my-route',
name: "Test",
component: Test,
}]
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试延迟加载,它会在 SSR 上失败:
export const routes = [{
path: '/my-route',
name: "Test"
component: () => import('../views/Test.vue'),
}]
Run Code Online (Sandbox Code Playgroud)
找不到模块“./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);
});
});
Run Code Online (Sandbox Code Playgroud)
完整的错误是:
命令“/usr/bin/node /home/vagrant/Code/project/storage/app/ssr/1228cfee3f79dc5949bd898950384e53.js”失败退出代码:1(一般错误)
工作目录:/home/vagrant/Code/project/public 输出:
================ 错误输出:================ internal/modules/cjs/loader.js:628 throw err; ^
错误:找不到模块“./js/chunks/server/0.js?id=c3384f174123f0848451”
更新
我想我可能知道为什么会发生这种情况(我可能错了):
export const routes = [{
path: '/',
name: "Home",
component: () => import('../views/Home')
}]
Run Code Online (Sandbox Code Playgroud)
使用此代码,我收到一个错误:
错误:找不到模块“./js/chunks/server/0.js?id=c3384f174123f0848451”
命令“/usr/bin/node /home/vagrant/Code/project/storage/app/ssr/717358e60bfd52035a1e58256cdfbba0.js”失败。退出代码:1(一般错误)工作目录:/home/vagrant/Code/project/public 输出:================ 错误输出:======== ======== internal/modules/cjs/loader.js:628 抛出错误;^ 错误:找不到模块 './js/chunks/server/0.js?id=c3384f174123f0848451'
查看路径:在我编译的文件(位于public/js)中,我有这一行:
var chunk = require("./js/chunks/server/" + ({}[chunkId]||chunkId) + ".js?id=" + {"0":"c3384f174123f0848451"}[chunkId] + "");
Run Code Online (Sandbox Code Playgroud)
这似乎是一条相对路径。但是,该文件实际上是按照我在config/ssr.php- 'temp_path' => storage_path('app/ssr')- 中指定的方式运行的,因此它找不到路径。
但是,即使我更改了temp_pathtopublic_path()以便它可以从中找到块 ./js/chunks/server/(即public/js/chunks/server/0.js),它仍然会引发相同的错误。即使 SSR 的 temp_path 不同。
命令“/usr/bin/node /home/vagrant/Code/project/public/3560d8d101faa4bdef316054b14873cc.js”失败。退出代码:1(一般错误)工作目录:/home/vagrant/Code/project/public 输出:================ 错误输出:======== ======== internal/modules/cjs/loader.js:628 抛出错误;^ 错误:找不到模块 './js/chunks/server/0.js?id=c3384f174123f0848451'
此外,如果我console.log(_dirname)在renderVueComponentToString()里面返回我'/'
我解决了它,现在它只能在客户端与 SSR 和代码分割一起使用 - 如果你有更好的想法,我仍然洗耳恭听。
我使用了spatie/laravel-server-side-rendering,它的设置非常简单。
这是我的解决方案(以及我对 spatie/laravel-server-side-rendering 的更改):
我从charlesBochet 的评论中了解了如何分离捆绑包,但是我使用的不是 2xwebpack.mix.js个文件。
"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"
...
}
Run Code Online (Sandbox Code Playgroud)
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!')
}
Run Code Online (Sandbox Code Playgroud)
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);
});
});
Run Code Online (Sandbox Code Playgroud)
router.onReady(function() {
app.$mount('#app');
})
Run Code Online (Sandbox Code Playgroud)
export const routes = [
{
path: '/',
name: "Home",
component: () => import('../views/Home.vue')
},
Run Code Online (Sandbox Code Playgroud)