gatsby-plugin-intl icon indicating copy to clipboard operation
gatsby-plugin-intl copied to clipboard

Support for client routes

Open igor-krupa opened this issue 5 years ago • 7 comments

Hello. Is there support for client routes? I've noticed that everything that's wrapped in Router from @reach/router is not being displayed at all when using this plugin. It's displayed only on index page when using redirect: false option (so on / route). But every other language index doesn't display what's in Router. Is there maybe some option for that? I'm not using any other plugin that can conflict with that.

igor-krupa avatar Jan 09 '20 13:01 igor-krupa

+1

Yes please.

Steffi3rd avatar Feb 03 '20 15:02 Steffi3rd

Did anyone come up with anything?

mmlngl avatar Mar 30 '20 21:03 mmlngl

This worked for me: (note: I'm re-routing to "/me"):


const re = /^\/([\w-]+)\/me/;
exports.onCreatePage = async ({ page, actions }) => {
  const { createPage } = actions;

  const match = re.exec(page.path);
  // Only update the `/me` page.
  if (match) {
    // page.matchPath is a special key that's used for matching pages
    // with corresponding routes only on the client.
    page.matchPath = `${match[0]}/*`;
    // Update the page.
    createPage(page);
  }
};

mmlngl avatar Mar 30 '20 21:03 mmlngl

Thanks @mmmoli ! Worked out perfectly for me

jayjayzheng avatar May 18 '20 15:05 jayjayzheng

Extending @mmmoli 's solution, for multiple routes the following could be added to gatsby-node.js

exports.onCreatePage = ({ page, actions }) => {
  const { createPage } = actions
  const matchPathProducts = page.path.match(/^\/([\w-]+)\/products/)
  const matchPathProduct = page.path.match(/^\/([\w-]+)\/product/)
  const matchPathUser = page.path.match(/^\/([\w-]+)\/user/)

  if (matchPathProducts) {
    page.matchPath = `${matchPathProducts[0]}/*`
    createPage(page)
  } else if (matchPathProduct) {
    page.matchPath = `${matchPathProduct[0]}/*`
    createPage(page)
  } else if (matchPathUser) {
    page.matchPath = `${matchPathUser[0]}/*`
    createPage(page)
  }
}

blazelon avatar Nov 15 '20 10:11 blazelon

+1

ltremago avatar May 18 '21 03:05 ltremago

Worked above solutions but needed to add this to the router, for my "app" page, to listen for path change: <Router basepath={`${intl.locale}`+"/app"}>

mcarnoky001 avatar Dec 22 '21 10:12 mcarnoky001