-
-
Save nhatphamcdn/1939300366837e12a79ba2772ab615bb to your computer and use it in GitHub Desktop.
Revisions
-
bovas85 revised this gist
Oct 21, 2018 . 2 changed files with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ import { cacheAdapterEnhancer } from 'axios-extensions' import LRUCache from 'lru-cache' const ONE_HOUR = 1000 * 60 * 60 const defaultCache = new LRUCache({ maxAge: ONE_HOUR }) export default function ({ $axios }) { const defaults = $axios.defaults // https://github.com/kuitos/axios-extensions defaults.adapter = cacheAdapterEnhancer(defaults.adapter, { enabledByDefault: false, cacheFlag: 'useCache', defaultCache }) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,5 +15,9 @@ module.exports = { }) } }, plugins: [ '~/plugins/axios.js', // ...other plugins here ] ... // other config } -
bovas85 revised this gist
Oct 21, 2018 . 2 changed files with 13 additions and 85 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,6 @@ module.exports = { ** Generate dynamic pages configuration */ generate: { routes: function () { return axios.get(`${Config.wpDomain}${Config.api.projects}`).then(res => { return res.data.map(project => { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,99 +1,28 @@ import Vuex from 'vuex' import Config from '~/assets/config.js' const createStore = () => { return new Vuex.Store({ state: { homePage: [] }, mutations: { setHomepage (state, obj) { state.homePage = obj } }, actions: { async nuxtServerInit ({ commit }, { app, route }) { console.log('============= Server Init API calls =============') try { console.log('home') const home = await app.$axios.get( Config.wpDomain + Config.api.homePage, { useCache: true } ) commit('setHomepage', home.data) } catch (e) { console.log('error with API', e) } } } -
bovas85 created this gist
Aug 11, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ const Config = require('./assets/config') const axios = require('axios') module.exports = { ... // other config /* ** Generate dynamic pages configuration */ generate: { interval: 2000, routes: function () { return axios.get(`${Config.wpDomain}${Config.api.projects}`).then(res => { return res.data.map(project => { return { route: '/' + project.slug, payload: project } }) }) } }, ... // other config } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,103 @@ import Vuex from 'vuex' import Config from '~/assets/config.js' let arr = [] let count = 0 const createStore = () => { return new Vuex.Store({ state: { homePage: [], projects: [], window: 320, connection: null, navOpen: false, current: null, modalOpen: false, menuScrolled: false }, mutations: { resetMenus (state) { state.modalOpen = false state.navOpen = false }, hideMenuBg (state) { state.menuScrolled = false }, showMenuBg (state) { state.menuScrolled = true }, closeMenu (state) { state.navOpen = false state.modalOpen = false }, openMenu (state) { state.navOpen = true state.modalOpen = true }, sortProjects (state, obj) { // sorting alphabetically if (obj != null) { try { let filteredProjects = obj.sort(function (a, b) { return a.slug.localeCompare(b.slug) }) state.projects = filteredProjects } catch (e) {} } }, setHomePage (state, obj) { state.homePage = obj }, setProjects (state, obj) { state.projects = obj }, windowResize (state, size) { state.window = size }, setConnection (state, type) { state.connection = type } }, actions: { async nuxtServerInit ({ commit }, { app }) { /** * This is the secret sauce. * If the data being requested is cached, subsequent API calls will not be made * Also, if using nuxt generate, nuxtServerInit will be called for every page * Because of this caching, the API calls will only be done once */ if (count === 0 || count > 15) { count = 0 arr = [] console.log('============= Server Init API calls =============') try { console.log('home') let home = await app.$axios.get( Config.wpDomain + Config.api.homePage ) arr.push(home.data) commit('setHomePage', home.data) console.log('case studies') let projects = await app.$axios.get( Config.wpDomain + Config.api.projects ) arr.push(projects.data) commit('setProjects', projects.data) count++ } catch (e) { console.log('error with API') arr = [] } } else { count++ console.log('using cached api') commit('setHomePage', arr[0]) commit('setProjects', arr[1]) } } } }) } export default createStore