Skip to content

middleware in router.map does not apply for multiple handlers #10830

@brookslybrand

Description

@brookslybrand

I'm not certain whether this is a runtime bug, TypeScript bug, or just an API I don't completely understand. My guess is this has more to do with the types than actual runtime behavior, but I could be wrong

Take this simplified example from the 'router.map() with middleware' test:

let routes = route({
  profile: '/profile/:id',
})

let router = createRouter()

router.map(routes.profile, {
  middleware: [() => console.log('run the middleware')],
  // 👆 this middleware runs
  handler() {
    return new Response('OK')
  },
})

let response = await router.fetch('https://remix.run/profile/1')

However, if you map over multiple routes, the middleware will never be called

let routes = route({
  profile: {
    show: '/profile/:id',
    new: '/profile/new',
  }
})

let router = createRouter()

router.map(routes.profile, {
  middleware: [() => console.log('run the middleware')],
  // 👆 this middleware DOES NOT run
  show() {
    return new Response('OK')
  },
  new() {
    return new Response('OK')
  }
})

let response = await router.fetch('https://remix.run/profile/1')

The reason I tried this is because I saw that middleware can be applied to a router.map call, and my understanding is that router.map is most useful when applying handlers to multiple routers

Either:

  • The types are wrong and middleware should not be available as a method when defining multiple handlers like this(note, this is what TypeScript encourages me to type):
Image
  • middleware should run for all handlers defined in this map. I would find this a bit confusing though, given that middleware is at the same level as the route names

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions