Comments (4)
If anyone wonders - this is how I achieved it:
I learn about separating bundles from charlesBochet's comment, however instead of 2x webpack.mix.js
files I used one.
- package.json
"scripts": {
"dev-all": "concurrently \"npm --section=server run dev\" \"npm --section=client run dev\" --kill-others-on-fail",
"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-split for server-build
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
})
],
})
.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 - should wait for the router to be ready
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);
});
});
- and finally router file code-splitting works for app-client.js
export const routes = [
{
path: '/',
name: "Home",
component: () => import('../views/Home.vue')
},
from laravel-server-side-rendering.
I think I have an idea why this is happening:
export const routes = [{
path: '/',
name: "Home",
component: () => import('../views/Home')
}]
with this code, and this app-server.js setup
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(() => {
// const matchedComponents = router.getMatchedComponents();
// if (!matchedComponents.length) {
// return reject({ code: 404 });
// }
resolve(app);
}, reject);
})
.then(app => {
renderVueComponentToString(app, (err, res) => {
if (err) throw new Error(err);
dispatch(res);
});
});
I get an error:
Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
The command "/usr/bin/node /home/vagrant/Code/project/storage/app/ssr/ba362ac6c1b2711a2e802afa2fa15c78.js" failed.
Exit Code: 1(General error)
Working directory: /home/vagrant/Code/project/public
Error Output:
internal/modules/cjs/loader.js:628
throw err;
^
Error: Cannot find module './js/chunks/server/0.js?id=c3384f174123f0848451'
Require stack:
- /home/vagrant/Code/project/storage/app/ssr/ba362ac6c1b2711a2e802afa2fa15c78.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:625:15)
Look at the paths:
In my compiled file (which is at public/js
) I have this line:
var chunk = require("./js/chunks/server/" + ({}[chunkId]||chunkId) + ".js?id=" + {"0":"c3384f174123f0848451"}[chunkId] + "");
That seems like a relative path. However the file is actually running in what I specify in the config/ssr.php
- 'temp_path' => storage_path('app/ssr')
- so it cannot find the path.
However, even I change the temp_path
to public_path()
so that it can find the chunk from ./js/chunks/server/
(which is public/js/chunks/server/0.js
), it still throws the same error. Even though the SSR's temp_path is different.
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'
@sebastiandedeyne Can you please take a look?
from laravel-server-side-rendering.
why I got this error?
vendor.js:26457 [Vue warn]: Failed to mount component: template or render function not defined.
found in
--->
at resources/js/src/App.vue
from laravel-server-side-rendering.
Just wrestled with this all day. In addition to the solutions above, separate webpack or mix configs for client and server, and waiting on vue-router to be ready. I had to change the output path in webpack to match the storage/app/ssr
directory being used for the temp file. The problem was the temp file was stored in storage/app/ssr
and my async components were in public/dist
.
Used the storage path helper in my blade template
{!! ssr(storage_path('app/ssr/js/app-server.js'))->render() !!}
Changed Webpack config to
path: path.resolve(__dirname, 'storage/app/ssr'),
It's all working now. Hope this helps anyone else who was stuck on this like me.
from laravel-server-side-rendering.
Related Issues (20)
- SSR React with import 'style.scss' dosnt work HOT 2
- process.env always empty HOT 2
- Target class [] does not exist. BindingResolutionException HOT 1
- It is not working HOT 1
- Svelte - TypeError : cannot set property '.svelte' of undefined at registerExtension. HOT 1
- How to catch errors in vue render HOT 1
- PHP 8 Support HOT 1
- error when using Style section
- Question about webcomponents
- vue3 support HOT 1
- Can rollback to support for Laravel 5.5 ~ 5.8? HOT 1
- html parameter of renderVueComponentToString is an empty string HOT 5
- nuxtjs SSR HOT 1
- nextjs SSR HOT 1
- self is not defined HOT 3
- Cannot find element: #app HOT 1
- document is not defined HOT 1
- Spatie-Laravel Server Side working absolutely fine over localhost but when I uploaded it to the server it shows ssr() is undefined Please help how to solve it? HOT 3
- Laravel SSR: ReferenceError: window is not defined HOT 1
- Use spatie/laravel-server-side-rendering for rendering blade and its js. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-server-side-rendering.