Skip to content

Commit

Permalink
vue ssr with router
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscorti committed Jan 21, 2021
1 parent 6f26bf1 commit 57493f4
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 44 deletions.
12 changes: 6 additions & 6 deletions build/webpack.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ module.exports = (env, argv) => {
chunks: 'initial',
name: 'vendor',
enforce: true
},
'css-vendor': {
test: /styles[\\/]index.(s)?css$/,
chunks: 'initial',
name: 'css-vendor',
enforce: true
}
// 'css-vendor': {
// test: /style[\\/]index.(s)?css$/,
// chunks: 'initial',
// name: 'css-vendor',
// enforce: true
// }
}
},
minimizer: [new OptimizeCssAssetsWebpackPlugin(), new TerserPlugin()]
Expand Down
8 changes: 7 additions & 1 deletion build/webpack.ssr.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const webpack = require('webpack');

module.exports = {
target: 'node',
Expand Down Expand Up @@ -42,5 +43,10 @@ module.exports = {
]
},

plugins: [new VueLoaderPlugin()]
plugins: [
new VueLoaderPlugin(),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
]
};
125 changes: 106 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"express": "4.17.1",
"vue": "2.6.12",
"vue-router": "3.4.9",
"vue-server-renderer": "2.6.12",
"vuex": "3.6.0"
},
"devDependencies": {
Expand All @@ -40,6 +41,7 @@
"eslint-plugin-promise": "4.2.1",
"eslint-plugin-standard": "5.0.0",
"html-webpack-plugin": "4.5.1",
"ignore-loader": "0.1.2",
"mini-css-extract-plugin": "1.3.3",
"node-sass": "5.0.0",
"nodemon": "2.0.7",
Expand All @@ -51,6 +53,7 @@
"webpack": "5.11.1",
"webpack-cli": "4.3.1",
"webpack-dev-middleware": "4.0.2",
"webpack-hot-middleware": "2.25.0"
"webpack-hot-middleware": "2.25.0",
"webpack-node-externals": "2.5.2"
}
}
23 changes: 20 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const express = require('express');
const path = require('path');
const fs = require('fs');

const { createBundleRenderer } = require('vue-server-renderer');

const config = {
port: process.env.PORT || 3000
};
Expand All @@ -13,6 +15,11 @@ const indexHTML = fs.readFileSync(
'utf-8'
);

const appBundle = fs.readFileSync(
path.join(__dirname, 'ssr', 'App.js'),
'utf-8'
);

app.use(express.static('public'));

if (process.env.NODE_ENV === 'development') {
Expand All @@ -21,9 +28,19 @@ if (process.env.NODE_ENV === 'development') {
}

app.get('/*', (req, res) => {
// res.send('index.html');
res.write(indexHTML);
res.end();
const renderer = createBundleRenderer(appBundle);

renderer.renderToString({ url: req.url }, (err, html) => {
if (err) {
console.log(err);
return res.status(500).send('server error');
}

const ssrIndexHTML = indexHTML.replace('{{APP}}', html);

res.write(ssrIndexHTML);
res.end();
});
});

app.use((req, res, next) => {
Expand Down
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Vue from 'vue';
import AppLayout from './theme/AppLayout.vue';
import router from './router';
import store from './vuex-state';
import 'bulma';
import './styles/index.scss';

// Vue.config.productionTip = false;

Expand Down
6 changes: 4 additions & 2 deletions src/client-entry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { app } from './app';
import { app, router } from './app';

app.$mount('#app');
router.onReady(() => {
app.$mount('#app');
});
4 changes: 2 additions & 2 deletions src/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import axios from 'axios';
axios.defaults.baseURL = 'https://api.fullstackweekly.com/';

axios.interceptors.request.use(config => {
if (window && window.localStorage.getItem('token')) {
if (typeof window !== 'undefined' && window.localStorage.getItem('token')) {
config.headers = {
Authorization: `Bearer ${window.localStorage.getItem('token')}`
};
Expand All @@ -19,7 +19,7 @@ const getPosts = async categoryId => {
);
return resp.data;
} catch (error) {
console.error(`ERROR HERE:: ${error.message}`);
console.error(`ERROR HERE at network.getPosts:: ${error.message}`);
return [
{
id: 0,
Expand Down

0 comments on commit 57493f4

Please sign in to comment.