-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best practice for redirects? #239
Comments
Actually, I realize the issue with the previous script. This script does work: <script>
module.exports = {
created(){
this.$router.push('/')
}
}
</script> The question still stands though, should this be something that can be accomplished by a YAML Front Matter property or something similar? |
You can add custom redirects using |
For anyone else that stumbles across this looking for a solution, add a export default ({ router }) => {
router.addRoutes([
{ path: '/foo/', redirect: '/' },
{ path: '/bar/', redirect: '/' }
])
} |
In addition, I'd recommend using Netlify which allows you to define CDN level redirects with proper response codes. |
@bayssmekanique do these work for you on production sites? I encountered #160 when I tried. Also, once the plugin support lands you should be able to adapt this if you want to generate a netlify redirect file. |
@mdaffin, I read your post twice without realizing you were addressing this same question, so I apologize for the duplication. It indeed fails in production, so I have reverted to using redirect scripts on each of the pages I need to have redirected. |
@bayssmekanique How did you accomplish that? I can't seem to get a redirection script through the build system. I would've expected something along these lines to work, but instead I get an error complaining about <script>window.location.href = 'https://example.com';</script> |
I had to export the theme and override the vue component to perform redirects. This was also on version |
What if I want to redirect on the specific page from "/". That suggestion doesn't properly work -> I see 404, but URL in the address bar is changed. |
Be carefull, here is a typo: |
Did you find a solution for this? |
awesome thanks @lauragift21. I ended up implementing something similar. #1803 (comment) |
In my case, the Redirect.vue <template>
</template>
<script>
/** Redirects to the given 'to' url, which is relative to the current location. */
export default {
name: 'Redirect',
props: {
to: {
type: String,
required: true
}
},
beforeMount() {
document.location.replace(this.to);
}
}
</script> Then, in my <Redirect to="foo/" /> This has also the advantage that the (awesome!) link checker plugin won't complain about missing routes. |
This not become effective for me, and I did it like this in export default ({ router }) => {
router.beforeEach((to, from, next) => {
const redirectList = {
'/': '/zh/',
}
const redirect = redirectList[to.path]
if (redirect) {
next({ path: redirect })
} else next()
})
} |
This doesn't seem to work for me. |
Is there a best practice solution for redirecting a route?
I tried using a simple redirect script, but it seems to have an issue with the route and just reloads the same page continually in a loop:
I've considered overloading the
router
property inconfig.js
, or building a redirect component and using that, but both options seem a little heavy for what should probably be a "baked-in" feature.The text was updated successfully, but these errors were encountered: