-
-
Save chip/ddacac2c674095bacc1fd768d877407d to your computer and use it in GitHub Desktop.
Mixin to be injected into App.vue
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 characters
import axios from 'axios'; | |
export const refreshPageMixin = { | |
data() { | |
return { | |
currentHash: '{{POST_BUILD_ENTERS_HASH_HERE}}', | |
token: localStorage.getItem('user-token'), | |
hashChanged: false, | |
newHash: '' | |
} | |
}, | |
methods: { | |
initVersionCheck(url, frequency = 1000 * 60 * 2) { | |
setInterval(() => { | |
this.checkVersion(url); | |
}, frequency); | |
}, | |
async checkVersion(url) { | |
try { | |
const fileResponse = await axios.create({ | |
baseURL: `${this.$root.url}`, | |
headers: { | |
'Authorization': 'JWT ' + this.token, | |
'Content-type': 'application/json' | |
} | |
}).get(url + '?t=' + new Date().getTime()); | |
this.newHash = fileResponse.data.hash; | |
this.hashChanged = this.hasHashChanged(this.currentHash, this.newHash); | |
} catch (error) { | |
this.loading = false; | |
if (!error.response) { | |
this.errorStatus = 'Error: Network Error' | |
} else { | |
this.errorStatus = error.response.data.message; | |
} | |
} | |
}, | |
hasHashChanged(currentHash, newHash) { | |
if (!currentHash || currentHash === '{{POST_BUILD_ENTERS_HASH_HERE}}') { | |
return true; | |
} | |
return currentHash !== newHash; | |
}, | |
reloadApp() { | |
this.currentHash = this.newHash; | |
window.location.reload(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment