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

redirect option creates problems with routers #164

Open
cod3rshotout opened this issue Dec 22, 2020 · 1 comment
Open

redirect option creates problems with routers #164

cod3rshotout opened this issue Dec 22, 2020 · 1 comment

Comments

@cod3rshotout
Copy link

cod3rshotout commented Dec 22, 2020

I'm using Gatsby and I want build a single page site, so without create pages. For achieve this I edited gatsby-node.js with the following code:

exports.onCreatePage = async ({ page, actions }) => {
  const { createPage } = actions

  if (page.path === "/") {
    page.matchPath = "/*"
    createPage(page)
  }
}

in that case, each request is re-routed to the index.js page, which is the only one. Then, in the index.js page I have:

const IndexPage = () => {
  const intl = useIntl()

  const locale = intl.locale

  return (
    <BGTState>
      <BlogState>
        <Layout>
          <Router basepath={`/${locale}`}>
            <Home path="/" />
            <Section path="/:sectionSlug" />
            <Collection path="/:sectionSlug/:collectionSlug" />
            <Season path="/categorySlug/:collectionSlug/:seasonSlug" />
            <Product path="/:categorySlug/:collectionSlug/:seasonSlug/:prodSlug" />
            <Blog path="/blog" />
            <Article path="/blog/:articleSlug" />
            <NotFound default />
          </Router>
        </Layout>
      </BlogState>
    </BGTState>
  )
}

as you can see, I have different routers that load a specific component based on the url.The basepath contains the language chosed by the user. This mechanism is working fine for the home page only, but for the other links doesn't work. Infact, if I visit something like:

http://localhost:3001/en/category-home/prod-foo

which must load the Collection component, the site simply redirect to:

http://localhost:3001/en

and display the Home component again.

Pages structure:

As you can see I have just the index.js which handle all requests as I configured in the gatby-node.js. If I remove the localization plugin, at least using this configuration:

 {
      resolve: `gatsby-plugin-intl`,
      options: {
        // Directory with the strings JSON
        path: `${__dirname}/src/languages`,
        // Supported languages
        languages: ["it", "en", "ci", "fr"],
        // Default site language
        defaultLanguage: `it`,
        // Redirects to `it` in the route `/`
        //redirect: true,
        // Redirect SEO component
        redirectComponent: require.resolve(
          `${__dirname}/src/components/redirect.js`
        ),
      },
    },

and I don't prefix the url with intl.locale, everything is working fine. But adding redirect: true in the plugin configuration, and prefixing the link with the locale, the site redirect me to the home component.

Removing redirect is working fine, but I lose the redirect of course

@simokhalil
Copy link

Any updates on this issue please ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants