I'm new to Nuxt 3 (and Vue) and running into a issue with component reuse/caching in development mode. This problem never occurs when I build and preview the application.
The Problem
I'm using a dynamic route with a query parameter for a "magic login" process:
I access the page with the first token: /magic-login?token=token-A
The validation logic runs correctly.
I access the page a second time with a different token: /magic-login?token=token-B
The page loads, but the validation logic is weird. It's like not run at all. It just navigate to
/select-rolelike first logic. and I also see there nothing happend in network tab. So I assume it like cache entire state or something ( or just see that it same route. just navigate to/select-role)
This is my setup
I have a simple page component that loads a main component.
pages/magic-login.vue (Note: I've tried to force a new component key, but it didn't solve the issue.)
<template>
<LoginCallbackPage />
</template>
<script setup>
import LoginCallbackPage from '~/context/component/Login/LoginCallback.vue';
definePageMeta({
layout: false,
key: (route) => {
return route.fullPath;
},
keepalive: false,
});
</script>
LoginCallbackPage (The component containing the validation logic.)
<template>
<div>Validating...</div>
</template>
<script setup lang="ts">
import API from '~/context/global/api/api-client';
import { useAuthStore } from '~/context/global/store/auth-store';
import getApiError from '~/context/global/utils/api/get-error';
const route = useRoute();
const toast = useToast();
const config = useRuntimeConfig();
const authStore = useAuthStore();
const redirectToLogin = () => {
toast.add({
title: 'Failed to login',
color: 'error',
});
navigateTo({
path: '/login',
query: {
FAILED_TO_LOGIN: 'true',
},
});
};
const handleValidateToken = async (token: string) => {
try {
const res = await API.User.ValidateTokenAndGetInfo(token);
if (res.status_code === 200) {
const { token: receivedToken, ...userDataWithoutToken } = res.data;
if (receivedToken && userDataWithoutToken) {
authStore.setAuth(receivedToken, userDataWithoutToken);
return navigateTo('/select-role');
}
}
redirectToLogin();
} catch (error) {
const err = getApiError(error);
redirectToLogin();
throw error;
}
};
onMounted(() => {
const token = String(route.query.token);
handleValidateToken(token);
});
</script>
I also try watch like this but seem not working
watch(
() => route.query.token,
() => {
const token = String(route.query.token);
handleValidateToken(token);
},
{ immediate: true },
);
I use nuxt 3.19.3 and called the API with ofetch