Last active
March 1, 2020 18:50
-
-
Save arash16/2f711e56f2d864d7f0c691a133de0b39 to your computer and use it in GitHub Desktop.
This file contains 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
export default function asyncDataWrapper(originalAsyncData) { | |
return async function(ctx) { | |
try { | |
return await originalAsyncData.call(this, ctx); | |
} | |
catch (e) { | |
// statusCode=1 is connectivity error (assigned inside API-Layer) | |
if (process.client && e.statusCode === 1) { | |
window.addEventListener('online', () => { | |
// reload the page when connection is back | |
window.location.reload(true); | |
}); | |
} | |
ctx.error(e); | |
} | |
}; | |
} |
This file contains 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
<script> | |
import asyncDataWrapper from '~/utils/asyncDataWrapper'; | |
export default { | |
// ... | |
asyncData: asyncDataWrapper(async function ({ params, query, redirect }) { | |
return { data: await api.xyz({ id: params.id }) }; | |
}) | |
// ... | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment