Gatsby: What is the good method to configure redirection from `/` page to another page ?

Created on 14 Oct 2019  路  19Comments  路  Source: gatsbyjs/gatsby

How can I redirect / to /page-2/ ?

I have tryed 2 solutions:

First solution

https://github.com/harobed/gatsby-redirect-on-root-index in master branch.

When I execute npm run develop, I have this error in Browser console:

The route "/" matches both a page and a redirect; this is probably not intentional.

but no error after npm build; npm serve

Second solution

https://github.com/harobed/gatsby-redirect-on-root-index in page-resources-is-undefined branch.

When I execute npm run develop, I have this error in Browser console:

TypeError: locationAndPageResources.pageResources is undefined

When I execute npm build; npm serve, I have this error:

NOT FOUND

this is not found page, redirection don't work.

Question

What is the good method to configure redirection on / page?

stale?

Most helpful comment

You can simply redirect like this

import { navigate } from 'gatsby'
navigate('/');

OR

    if (typeof window !== 'undefined') {
      window.location = '/'
    }

All 19 comments

You can simply redirect like this

import { navigate } from 'gatsby'
navigate('/');

OR

    if (typeof window !== 'undefined') {
      window.location = '/'
    }

You can simply redirect like this

import { navigate } from 'gatsby'
navigate('/');

@Pratikkakkad I would like a redirection from / to /page-2 and not the other way round.

Why can't I use this below?

    createRedirect({
        fromPath: '/',
        isPermanent: false,
        redirectInBrowser: true,
        toPath: '/page-2/'
    });

Why treat / page differently to other pages?

@KyleAMathews an idea? what is your vision / guideline about this? It is a bug or a feature?

The message The route "[...]" matches both a page and a redirect; this is probably not intentional. shows even if the page is 404.js, which seems to make this warning pointless as configured.

Redirects seem to generate page-data.json requests for the fromPath even in production builds.

This might be another issue, but do redirects belong in match-paths.json now?

Stopping gatsby develop and running gatsby clean after deleting index.js resolves the Cannot read property 'page' of undefined error and restores expected functionality of displaying the dev 404 page. Also, following the / redirect works correctly albeit with the matches both a page and redirect error.

This is also a bug... deleting a page file should not require restarting gatsby develop and really should not require running gatsby clean.

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

I have the same issue. I would expect to pass some option to createRedirect that I want only exact match such in react-router, something like this:

 createRedirect({
        fromPath: '/',
        exactPath: true,
        isPermanent: false,
        redirectInBrowser: true,
        toPath: '/page-2/'
    });

馃憢 @harobed, @n-mirzayev. I've been looking at this a little bit this morning and am not sure I'm seeing what's missing here.

From my testing, doing a redirect from / to /page-2/ works. You do get a warning, but I'm not sure that's wrong if you have a src/pages/index.

What is the expected behavior here?

@blainekasten In my case I really don't have index page as I am developing multi-language application I need to redirect to pages that builds in gatsby-node. For example if user opens example.com app I need the page redirected to example.com/en/about.

Regarding warning, When I am opening localhost:8000 I see in browser console such message:
The route "/" matches both a page and a redirect; this is probably not intentional.

@n-mirzayev So to make sure I understand, as it is today, this _works_. But the console message is a bit misleading and incorrect? Is that the problem? Or does something not work.

@blainekasten Functionality works, it redirects as expected. But we get a warning message in console. It can be a question if there is a way to have redirect without getting warning message, otherwise it can be considered as bug and we shouldn't get warning message.

Just checking if the other page is not 404 before showing the error would fix it. Very easy PR

Great! Thanks for the clarification everyone!

@sever1an (or @n-mirzayev or anyone else 馃槃) would you be interested in contributing a fix for this? We love empowering the community to be part of the Gatsby contributor base! Any way I can help get you involved let me know.

@blainekasten I would love to contribute.

@n-mirzayev great! This article explains how to setup gatsby locally to make changes. It sounds like the expected behavior is to just not do the log if there isn't an index file, or index route.

This log is executed in gatsby/cache-dir/navigation

@blainekasten @sever1an Created PR for this issue: https://github.com/gatsbyjs/gatsby/pull/19789 . As this is my first try all comments are welcome :)

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Hey again!

It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.
Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community! 馃挭馃挏

I have the same issue. I would expect to pass some option to createRedirect that I want only exact match such in react-router, something like this:

 createRedirect({
        fromPath: '/',
        exactPath: true,
        isPermanent: false,
        redirectInBrowser: true,
        toPath: '/page-2/'
    });

This got working for me. Thanks @n-mirzayev

Was this page helpful?
0 / 5 - 0 ratings