Edit this page

onBeforeRoute() hook

The onBeforeRoute() hook provides full control over routing.

It is usually used for internationalization:

// Environment: server & client
 
export { onBeforeRoute }
 
import type { PageContext } from 'vike/types'
 
function onBeforeRoute(pageContext: PageContext) {
  const { urlWithoutLocale, locale } = extractLocale(pageContext.urlPathname)
  return {
    pageContext: {
      locale,
      urlLogical: urlWithoutLocale
    }
  }
}

You can also use it to implement a custom router.

⚠️
For use cases other than i18n, open a GitHub ticket or reach out on Discord: onBeforeRoute() is purposely limited but the limitations can be lifted in order to unlock new capabilities.

See also