Skip to content

Instantly share code, notes, and snippets.

@arash16
Last active March 1, 2020 18:50
Show Gist options
  • Save arash16/2f711e56f2d864d7f0c691a133de0b39 to your computer and use it in GitHub Desktop.
Save arash16/2f711e56f2d864d7f0c691a133de0b39 to your computer and use it in GitHub Desktop.
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);
}
};
}
<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