I'm trying to redirect to a 404 page if the user profile visited does not exist. I'm running an asynchronous created lifecycle hook which checks if the user exists, and if not I use $router.push to push to the 404 page, as shown below:
component.vue
async created() {
const userExists = await this.$store.dispatch("user/creatorExists", {
userId: this.$route.params.userId
});
if (!userExists) {
this.$router.push({ name: "pageNotFound" });
}
The problem is that during the call to check if the user exists or not, component.vue will load fully, so the end user will see the initial component flicker on their screen before being redirected.
Is there a better way to redirect to a 404 page if a user does not exist?