Redirects in component scripts don't work on any mobile devices.

Hi, I have this redirect function using the useRouter() composable, but it only seems to be working on my computer, but not on my mobile devices. I have also tried adding a .then() function to the $router.push() one with an alert() and it did alert, but no route change... Thanks for any help, this is a big confusion for me.
No description
9 Replies
kapa.ai
kapa.ai4w ago
Hi @Michal Winter. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, it seems that the issue you're experiencing with redirects not working on mobile devices might be related to the use of useRouter() and router.push(). Here's a potential solution and explanation: 1. Instead of using useRouter() and router.push(), it's recommended to use the navigateTo() helper function for navigation in Nuxt 3. This should resolve the "glitchy" behavior and work consistently across devices. Here's how you can modify your code:
// Instead of:
const router = useRouter();
router.push({ path: '/some-path' });

// Use:
return navigateTo('/some-path');
// Instead of:
const router = useRouter();
router.push({ path: '/some-path' });

// Use:
return navigateTo('/some-path');
This recommendation is supported by the answer in a Stack Overflow post Redirect to auth screen if no token is available in defineNuxtRouteMiddleware (https://stackoverflow.com/questions/77246657), which suggests:
// Replace:
router.push(loginURL);

// With:
return navigateTo(loginURL);
// Replace:
router.push(loginURL);

// With:
return navigateTo(loginURL);
2. The navigateTo() function is designed to work universally in Nuxt 3, handling both client-side and server-side navigation smoothly. This makes it more reliable across different devices and environments. 3. If you're using this in a middleware or plugin, make sure you're returning the result of navigateTo(). This is important for Nuxt to properly handle the navigation. 4. For more complex redirect logic, you might want to consider using Nuxt's built-in redirect system in the nuxt.config.js file, especially for static redirects. This is explained in the YouTube video Nuxt 3: Redirects in Nuxt 3 (https://www.youtube.com/watch?v=ALQcCDEusjI). If you're still experiencing issues after making these changes, it might be worth checking if there are any specific mobile-related configurations in your Nuxt setup that could be interfering with the navigation. Remember, when working with Nuxt 3, it's generally recommended to use the provided helpers like navigateTo() for navigation tasks, as they are designed to work consistently across different environments and devices. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@349455518336679939> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Michal Winter
Michal WinterOP4w ago
@kapa.ai But I have even tried navigateTo(), which also isn't working...
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I'm sorry to hear that you're still experiencing issues. If navigateTo() is not working as expected, it might be due to a variety of reasons. Here are a few things you could check: 1. Correct usage of navigateTo(): Ensure that you're using navigateTo() correctly. It should be used with await or return when calling it. Here's an example from the Nuxt documentation (https://nuxt.com/docs/api/utils/navigate-to):
// passing 'to' as a string
await navigateTo('/search')

// ... or as a route object
await navigateTo({ path: '/search' })

// ... or as a route object with query parameters
await navigateTo({
path: '/search',
query: {
page: 1,
sort: 'asc'
}
})
// passing 'to' as a string
await navigateTo('/search')

// ... or as a route object
await navigateTo({ path: '/search' })

// ... or as a route object with query parameters
await navigateTo({
path: '/search',
query: {
page: 1,
sort: 'asc'
}
})
2. Check for errors: If there's an error during navigation, navigateTo() will throw an exception. Make sure to catch and handle these exceptions to understand what might be going wrong. 3. External URLs: If you're trying to navigate to an external URL, you need to set the external parameter to true. By default, navigating to an external URL is not allowed and will throw an error. Here's an example from the Nuxt documentation (https://nuxt.com/docs/api/utils/navigate-to#external-url):
// will redirect successfully with the 'external' parameter set to 'true'
await navigateTo('<https://nuxt.com',> {
external: true
})
// will redirect successfully with the 'external' parameter set to 'true'
await navigateTo('<https://nuxt.com',> {
external: true
})
4. Middleware and plugins: If you're using middleware or plugins that might affect routing, ensure they're not interfering with the navigation. 5. Possible bug: If you've checked all of the above and the issue persists, it might be a bug. In this case, you should consider reporting the bug (https://nuxt.com/docs/community/reporting-bugs) to the Nuxt team. They're actively developing Nuxt and every issue helps make it better. Remember, debugging is a process of elimination. Try to isolate the problem by testing the simplest case of navigation, and then gradually add complexity to identify where the issue might be. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@349455518336679939> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Michal Winter
Michal WinterOP4w ago
@kapa.ai Still not working. I have tried everything. Navigating works on my PC but not on my mobile device.
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I'm sorry to hear that you're still experiencing issues. If you've tried all the suggestions and navigateTo() is still not working on your mobile device, it might be a bug or a device-specific issue. Here are a few more steps you can take: 1. Test on different mobile devices and browsers: This can help you determine if the issue is specific to a certain device or browser. 2. Check for console errors: If possible, check for any console errors on your mobile device. This can provide clues about what's going wrong. 3. Update Nuxt: Ensure you're using the latest version of Nuxt. There might have been bug fixes that could solve your issue. 4. Report the issue: If you're still unable to resolve the issue, consider reporting it to the Nuxt team (https://nuxt.com/docs/community/reporting-bugs). Provide as much detail as possible, including the device and browser where the issue occurs, and any error messages you're seeing. This could be a bug that the Nuxt team isn't aware of yet. Remember, when reporting a bug, try to provide a minimal reproduction of the issue. This will make it easier for the Nuxt team to identify and fix the problem. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@349455518336679939> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Michal Winter
Michal WinterOP4w ago
Interesing. Might be a bug then Okay it is not a problem in the redirect. For some reason on mobile the session doesnt get updated. So the redirected page redirects back to login...
Want results from more Discord servers?
Add your server