2

I am having a page template, which creates multiple pages. One of them is my homepage and it has a slug "home". How can I set it to be the default page when I visit the site - example.com

Bonus: how to make it so when I visit example.com/home to redirect me to example.com

Hristo Hristov
  • 111
  • 3
  • 13
  • Looks like we are asking the very same question: https://stackoverflow.com/questions/59721965/the-right-way-to-define-a-sourced-page-as-gatsbys-front-page – radscheit Jan 13 '20 at 18:46
  • 1
    Does this answer your question? [The right way to define a sourced page as Gatsby's front page](https://stackoverflow.com/questions/59721965/the-right-way-to-define-a-sourced-page-as-gatsbys-front-page) – radscheit Jan 14 '20 at 06:35
  • There is an official solution: https://stackoverflow.com/a/64426759/10495311 – Yaroslav Oct 19 '20 at 11:57

1 Answers1

1

Gatsby integrates well with @reach/router. A Router redirect allows redirecting to any other route, no matter where it comes from. Apply it on src/pages/index.js

import React from "react"
import { Router, Redirect } from "@reach/router"

const IndexPage = () => (
  <React.Fragment>
    <Router>
      <Redirect noThrow
        from="/"
        to="/whateverYourFrontpagesRouteIs"
      />
    </Router>
  </React.Fragment>
)

export default IndexPage

This also a valid answer to your bonus question.

radscheit
  • 352
  • 4
  • 22