How to use the next-server/dist/lib/router/router.events function in next-server

To help you get started, we’ve selected a few next-server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zeit / next.js / packages / next / client / router.js View on Github external
export function makePublicRouterInstance (router) {
  const instance = {}

  for (const property of urlPropertyFields) {
    if (typeof router[property] === 'object') {
      instance[property] = {...router[property]} // makes sure query is not stateful
      continue
    }

    instance[property] = router[property]
  }

  // Events is a static property on the router, the router doesn't have to be initialized to use it
  instance.events = _Router.events

  propertyFields.forEach((field) => {
    // Here we need to use Object.defineProperty because, we need to return
    // the property assigned to the actual router
    // The value might get changed as we change routes and this is the
    // proper way to access it
    Object.defineProperty(instance, field, {
      get () {
        return router[field]
      }
    })
  })

  coreMethodFields.forEach((field) => {
    instance[field] = (...args) => {
      return router[field](...args)
github zeit / next.js / packages / next / client / router.js View on Github external
SingletonRouter.ready(() => {
    _Router.events.on(event, (...args) => {
      const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(1)}`
      if (SingletonRouter[eventField]) {
        try {
          SingletonRouter[eventField](...args)
        } catch (err) {
          console.error(`Error when running the Router event: ${eventField}`)
          console.error(`${err.message}\n${err.stack}`)
        }
      }
    })
  })
})