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

What is the good method to configure redirection from / page to another page ? #18665

Closed
harobed opened this issue Oct 14, 2019 · 19 comments · Fixed by #19789
Closed

What is the good method to configure redirection from / page to another page ? #18665

harobed opened this issue Oct 14, 2019 · 19 comments · Fixed by #19789
Labels
stale? Issue that may be closed soon due to the original author not responding any more.

Comments

@harobed
Copy link

harobed commented Oct 14, 2019

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?

@gatsbot gatsbot bot added the type: question or discussion Issue discussing or asking a question about Gatsby label Oct 14, 2019
@harobed harobed changed the title What is this good pratice to configure redirection on / page? What is this good practice to configure redirection on / page? Oct 14, 2019
@harobed harobed changed the title What is this good practice to configure redirection on / page? What is the good practice to configure redirection on / page? Oct 14, 2019
@harobed harobed changed the title What is the good practice to configure redirection on / page? What is the good method to configure redirection on / page? Oct 14, 2019
@Pratikkakkad
Copy link

Pratikkakkad commented Oct 15, 2019

You can simply redirect like this

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

OR

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

@harobed
Copy link
Author

harobed commented Oct 15, 2019

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?

@harobed
Copy link
Author

harobed commented Oct 15, 2019

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

@harobed harobed changed the title What is the good method to configure redirection on / page? What is the good method to configure redirection from / page to another page ? Oct 15, 2019
@mi-na-bot
Copy link
Contributor

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?

@mi-na-bot
Copy link
Contributor

mi-na-bot commented Oct 15, 2019

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.

@gatsbot
Copy link

gatsbot bot commented Nov 5, 2019

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’s 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! 💪💜

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Nov 5, 2019
@n-mirzayev
Copy link

n-mirzayev commented Nov 13, 2019

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/'
    });

@blainekasten
Copy link
Contributor

👋 @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 blainekasten self-assigned this Nov 19, 2019
@n-mirzayev
Copy link

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

@blainekasten
Copy link
Contributor

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

@n-mirzayev
Copy link

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

@mi-na-bot
Copy link
Contributor

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

@blainekasten
Copy link
Contributor

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.

@n-mirzayev
Copy link

@blainekasten I would love to contribute.

@blainekasten
Copy link
Contributor

@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

@mnurik
Copy link
Contributor

mnurik commented Nov 26, 2019

@blainekasten @Sever1an Created PR for this issue: #19789 . As this is my first try all comments are welcome :)

@LekoArts LekoArts added type: feature or enhancement and removed stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby labels Feb 25, 2020
@github-actions
Copy link

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’s 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! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Mar 16, 2020
@github-actions
Copy link

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.
Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m 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! 💪💜

@manivaidhy
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants