Route Function

Route Functions give us full programmatic flexibility to define our routing logic.

// /pages/film/admin.page.route.js

export default (pageContext) => {
  // Route Functions allow us to implement advanced routing such as route guards.
  if (! pageContext.user.isAdmin) {
    return false
  }
  const { url } = pageContext
  // We can use RegExp or any JavaScript tool/library we want.
  if (! /\/film\/[0-9]+\/admin/.test(url)) {
    return { match: false } // equivalent to `return false`
  }
  filmId = url.split('/')[2]
  return {
    match: true,
    // We make `filmId` available as `pageContext.routeParams.filmId`
    routeParams: { filmId }
  }
}

The match value can be a (negative) number which enables us to resolve route conflicts; the higher the number, the higher the priority.

Edit this page