Search code examples
meteorreactjsflow-router

Link to redirected route from root


For my application, the objective is that the user goes goes to the root and it automatically redirects to a route based on the day of the week. It currently works, as long as the user goes to the route by inputing the URI. However, I am unsure how to link to this, as when I try to do href="/", it just goes to the root of the application, not the intended redirect route. Not sure if I'm just thinking about this in the wrong way. Below is my current code for the routes.

FlowRouter.route('/boards/:slug', {
  name: 'Boards.show',
  action() {
    mount(AppContainer, {
      main: <BoardContainer/>,
    });
  },
});

FlowRouter.route('/', {
  name: 'Home',
  action() {
    let day = new Date().getDay();

    if (day === 0) {
      FlowRouter.go('/boards/sunday');
    } else if (day === 1) {
      FlowRouter.go('/boards/monday');
    } else if (day === 2) {
      FlowRouter.go('/boards/tuesday');
    } else if (day === 3) {
      FlowRouter.go('/boards/wednesday');
    } else if (day === 4) {
      FlowRouter.go('/boards/thursday');
    } else if (day === 5) {
      FlowRouter.go('/boards/friday');
    } else if (day === 6) {
      FlowRouter.go('/boards/saturday');
    }
  },
});

Then I simply link to the root by using <a href="/">Home</a>, and of course, this doesn't work.


Solution

  • I believe you want to use redirect instead of go since you are already in a route, ex:

    redirect('/boards/sunday')
    

    instead of

    FlowRouter.go('/boards/sunday')