Skip to content

Instantly share code, notes, and snippets.

@webislife
Created April 18, 2019 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webislife/c06893353755aea319b147ae3ba12d00 to your computer and use it in GitHub Desktop.
Save webislife/c06893353755aea319b147ae3ba12d00 to your computer and use it in GitHub Desktop.
Sample vue client index js
import { createApp } from '@root/app.js';
import { Cookies } from './lib/cookie.js';
const APP = createApp();
global.cookies = new Cookies({env: 'browser'});
global.APP = APP;
if (window.__INITIAL_STATE__) {
APP.Store.replaceState(window.__INITIAL_STATE__);
}
let token = cookies.get('token');
if(typeof token === 'string') {
APP.API.BGLN.Request.defaults.headers.common['Authorization'] = token;
}
APP.Router.onReady(() => {
//Check auth token in URL
let token = APP.Router.currentRoute.query.token;
if(typeof token !== 'undefined') {
APP.API.BGLN.Request.defaults.headers.common['Authorization'] = token;
}
APP.Router.beforeResolve((to, from, next) => {
const matched = APP.Router.getMatchedComponents(to);
const prevMatched = APP.Router.getMatchedComponents(from);
let diffed = false;
const activated = matched.filter((c, i) => {
return diffed || (diffed = (prevMatched[i] !== c));
});
if (activated.length === 0) {
return next();
}
//TODO add loader
Promise.all(activated.map(component => {
if(typeof component.asyncData === 'function') {
return component.asyncData({
store: APP.Store,
route: to,
});
}
})).then(() => {
next();
}).catch(next);
});
//Redirect to 500 err page
if(APP.Store.state.ui.asyncFail === true) {
APP.Router.push('/500');
}
//Start app
APP.Vue.$mount('#app');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment