How can I know with flow router if us is the first page load or a route change?


#1

Hello guys,

How can I know with flow router if us is the first page load or a route change?
I want to use redirect if it is the first page load or stop if it is a route change.
How can I do that?

Thanks


#2

Guys please help me.


#3

You could just have a variable “firstRouteChange = false”, and just run the check in route change, and make it true.


#4

Not 100% sure I understand. Do you want to know, when a route changes, whether you arrived there via external link or after a page refresh? If so…

You could either do what @diaconutheodor suggests, or, for more control, take advantage of oldRoute property in the current route.

import { Tracker } from 'meteor/tracker';
import { FlowRouter } from 'meteor/kadira:flow-router';

Tracker.autorun(() => {
  FlowRouter.watchPathChange();
  const current = FlowRouter.current();
  const itIsFresh = !current.oldRoute;
  if (itIsFresh) { FlowRouter.go('/my-redirect-path'); }
});

Better still, instead of redirecting in the frontend, with all these checks and the inevitable flicker, better do it on the server:

import { WebApp } from 'meteor/webapp';

WebApp.connectHandlers
      .use((req, res, next) => {
        if (req.url === 'xxx' /* Your route check condition here */) {
          // 302 Temporary Redirect
          res.writeHead(302, {
            Location: '/my-redirect-path',
          });
          res.end();
        } else {
          // Let other handlers match
          next();
        }
      });

This one is guaranteed to redirect when arriving from the outside


#5

@jpfernandezl please provide more information about your problem.


#6

@illustreets got it right.
That is what I needed, thank you so much.
I like the !current.oldRoute; option.


#7

Glad it worked :slight_smile: