-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Open
Labels
Description
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
middlewareshould not be available as a method when defining multiple handlers like this(note, this is what TypeScript encourages me to type):
middlewareshould run for all handlers defined in thismap. I would find this a bit confusing though, given thatmiddlewareis at the same level as the route names