Skip to content
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

Closed
bayssmekanique opened this issue Apr 24, 2018 · 16 comments
Closed

Best practice for redirects? #239

bayssmekanique opened this issue Apr 24, 2018 · 16 comments

Comments

@bayssmekanique
Copy link

bayssmekanique commented Apr 24, 2018

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:

<script>
module.exports = {
  created(){
    this.$router.go('/')
  }
}
</script>

I've considered overloading the router property in config.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.

@bayssmekanique
Copy link
Author

bayssmekanique commented Apr 24, 2018

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?

@yyx990803
Copy link
Member

You can add custom redirects using enhanceApp.js + router.addRoutes.

@bayssmekanique
Copy link
Author

For anyone else that stumbles across this looking for a solution, add a .vuepress/encahnceApp.js file and define your redirects within it like this:

export default ({ router }) => {
  router.addRoutes([
    { path: '/foo/', redirect: '/' },
    { path: '/bar/', redirect: '/' }
  ])
}

@yyx990803
Copy link
Member

In addition, I'd recommend using Netlify which allows you to define CDN level redirects with proper response codes.

@mdaffin
Copy link
Contributor

mdaffin commented Apr 25, 2018

@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.

@bayssmekanique
Copy link
Author

@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.

@rasapetter
Copy link

@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 window being undefined.

<script>window.location.href = 'https://example.com';</script>

@bayssmekanique
Copy link
Author

I had to export the theme and override the vue component to perform redirects. This was also on version 0.8.4, so things have likely changed by now.

@telepenin
Copy link
Contributor

telepenin commented Dec 13, 2018

You can add custom redirects using enhanceApp.js + router.addRoutes.

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.

@nanxiaobei
Copy link

nanxiaobei commented Mar 11, 2020

encahnceApp

For anyone else that stumbles across this looking for a solution, add a .vuepress/encahnceApp.js file and define your redirects within it like this:

export default ({ router }) => {
  router.addRoutes([
    { path: '/foo/', redirect: '/' },
    { path: '/bar/', redirect: '/' }
  ])
}

Be carefull, here is a typo:
encahnceApp
enhanceApp

@saragee3
Copy link

@telepenin

You can add custom redirects using enhanceApp.js + router.addRoutes.

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.

Did you find a solution for this?

@lauragift21
Copy link

@saragee3 I used the solution here #1382 and it worked for me.

@saragee3
Copy link

saragee3 commented Apr 1, 2020

awesome thanks @lauragift21. I ended up implementing something similar. #1803 (comment)

@Dan1ve
Copy link

Dan1ve commented May 22, 2020

In my case, the enhanceApp.js routing didn't work for some reason.
Hence, I implemented a tiny custom component to do the redirect, which seems to work fine:

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 readme.md, I simply use

<Redirect to="foo/" />

This has also the advantage that the (awesome!) link checker plugin won't complain about missing routes.

@ocian
Copy link

ocian commented Oct 18, 2020

For anyone else that stumbles across this looking for a solution, add a .vuepress/encahnceApp.js file and define your redirects within it like this:

export default ({ router }) => {
  router.addRoutes([
    { path: '/foo/', redirect: '/' },
    { path: '/bar/', redirect: '/' }
  ])
}

This not become effective for me, and I did it like this in .vuepress/enhanceApp.js
It's a centralized control Router Redirect powered by Vue Router.

export default ({ router }) => {
  router.beforeEach((to, from, next) => {
    const redirectList = {
      '/': '/zh/',
    }
    const redirect = redirectList[to.path]

    if (redirect) {
      next({ path: redirect })
    } else next()
  })
}

@krofax
Copy link

krofax commented Nov 25, 2022

For anyone else that stumbles across this looking for a solution, add a .vuepress/encahnceApp.js file and define your redirects within it like this:

export default ({ router }) => {
  router.addRoutes([
    { path: '/foo/', redirect: '/' },
    { path: '/bar/', redirect: '/' }
  ])
}

This doesn't seem to work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests