fus-marcom / franciscan-react

A new website for Franciscan University of Steubenville built with ReactJS.
https://gcp.franciscan.university
MIT License
171 stars 79 forks source link

Automatically redirecting old slugs #96

Open cr101 opened 6 years ago

cr101 commented 6 years ago

In WordPress when you change a 'post slug' in the Permalink, WordPress will automatically redirect a link containing an old slug to the new link with new slug and sets the status to 301 Moved Permanently. This is very useful especially when you change a post's slug after Google has already indexed that page otherwise you will end up with lots of 404s and that's probably not good for SEO and certainly not good UX.

Since this is not a WordPress theme the functionality to redirect old post slugs will need to be created.

JesseRWeigel commented 6 years ago

I like this idea, but I am not sure how to add it to our current routing setup in server.js.

cr101 commented 6 years ago

This is how I would tackle it over the top of my head.

Take for example pages/job.js, perhaps if possible move the graphql query inside getInitialProps() like below:

static async getInitialProps ({ query: { id, type } }) {
    <Query
          query={JobQuery(type)}
          variables={{ name: id }}
        >
          {result => {
if (result.error)  // Here you either redirect the user to the new post url or to a custom 404 page
/* Either way, have a look at https://github.com/zeit/next.js/wiki/Redirecting-in-%60getInitialProps%60 AND https://stackoverflow.com/questions/45099395/conditional-redirection-in-next-js */
}}
        </Query>

    return { result }
  }

On the backend side - take a look at wp_old_slug_redirect() in https://core.trac.wordpress.org/browser/tags/4.9.7/src//wp-includes/query.php#L0

Btw, last year I opened issue #289 on wp-graphql

Fixing this issue will also fix #93 and #108

I hope this helps at all.

Emiliano-Bucci commented 6 years ago

@cr101 Hi, i was trying to figure out how to handle redirects. You think that redirecting inside the query result is just fine? You know if there's a particular error message that i have to look for? Thank you very much for your time :)

cr101 commented 6 years ago

@Emiliano-Bucci I haven't implemented this functionality so I'm not sure. If you figure it out please post your solution here