Nuxt generate no query params after redirection

https://github.com/nuxt/nuxt/issues/31387 I'm facing the issue here. My redirect middleware gets transformed into a meta tag with http-equiv="refresh". Hence the new URL doesn't contain the query params of the redirected URL
GitHub
Query params get lost on redirection in after generating static fil...
Environment Got from stackblitz test project: Nuxt project info: Operating System: Linux Node Version: v18.20.3 Nuxt Version: 3.16.0 CLI Version: 3.23.0 Nitro Version: 2.11.6 Package Manager: npm@1...
2 Replies
kapa.ai
kapa.ai3mo ago
Hi @FireArachna. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I understand you're facing an issue with query parameters being lost during redirects in Nuxt when using the nuxt generate command. Based on the knowledge sources, this appears to be related to how Nuxt handles redirects in static site generation. When a redirect is processed during static generation, it gets transformed into a meta refresh tag in the HTML, which can cause query parameters to be lost. There are a few potential solutions to this issue: 1. Use server-side redirects instead of client-side redirects When using static site generation, server-side redirects (like those configured in hosting platforms) are more reliable for preserving query parameters. 2. Configure your hosting platform's redirect rules If you're using Netlify, for example, you can set up redirect rules in your netlify.toml file to handle redirects with proper query parameter preservation. Just make sure the ordering of your redirect rules is correct, as shown in this Stack Overflow post. 3. Use a different approach for navigation Instead of relying on middleware redirects, you could use navigateTo in your component logic or consider using <NuxtLink> with the appropriate query parameters. 4. Watch for route query changes If you need to react to query parameter changes, you can set up a watcher as shown in this Stack Overflow solution:
const route = useRoute()
watch(
() => route.query.qry,
() => {
console.log(route.query.qry);
},
);
const route = useRoute()
watch(
() => route.query.qry,
() => {
console.log(route.query.qry);
},
);
The issue you're experiencing is likely related to how static site generation handles redirects differently from server-side rendering. In static sites, redirects often need to be handled at the hosting/CDN level rather than within the Nuxt application itself to properly preserve query parameters. If you need more specific help, you might want to share your middleware code and how you're implementing the redirect. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@133792935526400000> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?