SEO Friendly Route in React
Hi Folks,
This is a software developer writing about “How to create dynamic route SEO friendly.”
Many times it needs for a website to have a route such that some special keywords need to apply so that it can improve that website rating on google search engine. For that, we as a developer have to keep it in mind while developing that “the route should be SEO friendly so that SEO team won’t get issues on rating the website and much more”.
When I was working on this first time, I researched a lot, and finally, I got a straightforward solution in React. React provides us a Higher-order component “Route” which provides us accessing the history and location of a parent component. If you want the react props inside a child component then you have to use “withRouter”. Wrap your child component using withRouter and then you can access react props inside it.
There are two types of routing that SEOs prefer:
- when you are fixing variables in URL separated by slashes. For Example, http://www.google.com/someroute/someotherroute/moreroutes/ So Here http://www.google.com is a domain name and the keywords you after that between slashes are the routes you want to make while going inside the website by clicking on links.
This works as you fixed the keyword someroute when clicked, it came just after the domain name and when someotherroute clicked it came at second position after the domain name, and so on …
It is very easy to do this, just follow step by step,
First, while defining your component inside route in the main component, give the route name followed by a colon “:”. Like, “/:Id”. It should make your route dynamic. Route name would be more than one also like “/:id/:name”. Provide as many keywords you want to show on a click but keep in mind that the link where you are clicking should also have the same number of keys separated by slashes so that it took place to their route correctly. When you are defining the dynamic route keep that route at bottom of all routes. For Example:-
Defining route in the main component:
<Route exact path=”/:id/:name” component={somecomponent}/>
when you are clicking on a link:
<Link to=”/1/somename”>Label</Link>
When you click on this link. It will redirect you to the component of somecomponent and the route generated for that component will be http://domain_name.com/1/somename
2. when you want to create your route like a breadcrumb. The route will be in the same series as the path you follow to reach there. For Example,
https://www.domain_name.com/someroute/someotherroute/moreroute
Like you enter in the domain_name, then you click on someroute, then on someotherroute and then on moreroute.
This type of URL generates according to your path you follow….
For these types of routes, follow this example code …

Now, Lastly, you need to know this “How to get routes keywords without using the string manipulation in a particular component”. For that just use this code,
this.props.match.params in class-based and props.match.params in a functional component. You will find an object containing your key as a defined route and value as a route from url.
Finally, you are done creating a dynamic route of your choice.
This is the basic concept of applying. It can be used in different ways according to your project needs. So bare with the concept and on your own to achieve the final desirable output.
Thank You.
Comments
Post a Comment