- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有两个不同的 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/
中找到 block >(即 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'
此外,如果我在 renderVueComponentToString()
中 console.log(_dirname)
它返回我 '/'
最佳答案
我解决了它,现在它只能在客户端使用 SSR 和代码拆分 - 如果您有任何更好的想法,我仍然洗耳恭听。
我用了spatie/laravel-server-side-rendering而且设置起来非常简单。
这是我的解决方案(以及我对 spatie/laravel-server-side-rendering 的更改):
我了解了如何将 bundle 与 charlesBochet's comment 分开,但是我使用了一个文件而不是 2x webpack.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"
...
}
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!')
}
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);
});
});
router.onReady(function() {
app.$mount('#app');
})
export const routes = [
{
path: '/',
name: "Home",
component: () => import('../views/Home.vue')
},
关于javascript - 延迟加载的 vue-router 组件不适用于 SSR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58216963/
我是一名优秀的程序员,十分优秀!