{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/preventing-loading-progress-bar-flashes-with-vue","result":{"data":{"allGhostPost":{"edges":[{"node":{"title":"Preventing Loading Progress Bar Flashes with Vue.js","html":"

The Problem

You're using a loading progress bar like the one shown below, but your website is fast enough for it to flash for a few milliseconds and disappear near instantly. This can make users feel that your website is slower than it actually is.

\"NProgress

The Solution

Display a progress bar only if the request takes \"too long.\" A good duration to use 200ms.

The Code

const progressFns = () => {\n    let progressTimeout, count = 0\n\n    const start = () => {\n        count++\n\n        progressTimeout = setTimeout(() => {\n            NProgress.start()\n        }, 200)\n    }\n\n    const stop = () => {\n        count = Math.max(0, count - 1)\n        if (count > 0) return\n\n        NProgress.done()\n        clearTimeout(progressTimeout)\n    }\n\n    return { start, stop }\n}

progressFns returns an object with two functions:

It also keeps track of how many requests are in progress and only hides the bar when the last one is done. Usually you would intercept your request library and/or Vue Router's requests and show/hide the progress bar as needed.

Usage

Get instances of the functions:

const {\n    start: progressStart,\n    stop: progressStop,\n} = progressFns()

Vue.js

router.beforeResolve((to, from, next) => {\n    if (to.name) {\n        progressStart()\n    }\n\n    next()\n})\n\nrouter.afterEach((to, from) => {\n    progressStop()\n})

Axios

window.axios.interceptors.request.use(config => {\n    if (!config.__noProgress) progressStart()\n\n    return config\n}, error => {\n    progressStop()\n\n    return Promise.reject(error)\n})\n\nwindow.axios.interceptors.response.use(response => {\n    if (!response.config.__noProgress) progressStop()\n\n    return response\n}, error => {\n    progressStop()\n\n    return Promise.reject(error)\n})

Checking config.__noProgress lets me selectively disable the progress bar completely for certain requests, like so:

axios\n    .get(\"/auth/me\", { __noProgress: true })\n    ...

Thanks to Next.js 7 for making me aware of this!
https://github.com/jamiebuilds/react-loadable#avoiding-flash-of-loading-component

","published_at":"2018-09-26T17:17:52.000+03:00","slug":"preventing-loading-progress-bar-flashes-with-vue","tags":[],"plaintext":"The Problem\nYou're using a loading progress bar like the one shown below, but your website\nis fast enough for it to flash for a few milliseconds and disappear near\ninstantly. This can make users feel that your website is slower than it actually\nis.\n\nThe Solution\nDisplay a progress bar only if the request takes \"too long.\" A good duration to\nuse 200ms.\n\nThe Code\nconst progressFns = () => {\n let progressTimeout, count = 0\n\n const start = () => {\n count++\n\n progressTimeout = setTimeout(() => {\n NProgress.start()\n }, 200)\n }\n\n const stop = () => {\n count = Math.max(0, count - 1)\n if (count > 0) return\n\n NProgress.done()\n clearTimeout(progressTimeout)\n }\n\n return { start, stop }\n}\n\nprogressFns returns an object with two functions:\n\n * start to queue the progress bar for display after 200ms\n * stop to hide the progress bar\n\nIt also keeps track of how many requests are in progress and only hides the bar\nwhen the last one is done. Usually you would intercept your request library\nand/or Vue Router's requests and show/hide the progress bar as needed.\n\nUsage\nGet instances of the functions:\n\nconst {\n start: progressStart,\n stop: progressStop,\n} = progressFns()\n\nVue.js\nrouter.beforeResolve((to, from, next) => {\n if (to.name) {\n progressStart()\n }\n\n next()\n})\n\nrouter.afterEach((to, from) => {\n progressStop()\n})\n\nAxios\nwindow.axios.interceptors.request.use(config => {\n if (!config.__noProgress) progressStart()\n\n return config\n}, error => {\n progressStop()\n\n return Promise.reject(error)\n})\n\nwindow.axios.interceptors.response.use(response => {\n if (!response.config.__noProgress) progressStop()\n\n return response\n}, error => {\n progressStop()\n\n return Promise.reject(error)\n})\n\nChecking config.__noProgress lets me selectively disable the progress bar\ncompletely for certain requests, like so:\n\naxios\n .get(\"/auth/me\", { __noProgress: true })\n ...\n\nThanks to Next.js 7 for making me aware of this!\nhttps://github.com/jamiebuilds/react-loadable#avoiding-flash-of-loading-component","meta_description":null}}]}},"pageContext":{"slug":"preventing-loading-progress-bar-flashes-with-vue","prev":"how-to-set-global-shortcut-open-new-iterm2-window","next":"routing-urls-to-openfaas-functions"}},"staticQueryHashes":["3649515864"]}