Skip to content

Conversation

@markdalgleish
Copy link
Member

@markdalgleish markdalgleish commented Oct 31, 2025

This is still a work in progress, so I'm just sharing for some early feedback.

@MichaelDeBoey MichaelDeBoey linked an issue Nov 1, 2025 that may be closed by this pull request
@MichaelDeBoey MichaelDeBoey changed the title Add static-middleware to fetch-router feat(fetch-router): add static-middleware Nov 1, 2025
Copy link
Member

@mjackson mjackson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great. Very clean! I love how organized you're keeping the code.

There are just a few small ways I think we could re-arrange things that would make it a little more ergonomic IMO. Hopefully the feedback I left helps.

)

return async (context, next) => {
let response = await handler(context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we unwound this a bit so we didn't have so much wrapped up inside the handler?

For example, we could do something like:

let resolveFile = createFsFileResolver(root, pathResolver)
let file = resolveFile(context)

if (file == null) {
  return next()
}

return sendFile(file, context)

This way we wouldn't have to generate a 404 Response in the handler that we just throw away at this level.

middleware.push(storeContext())

middleware.push(
staticFiles('./public/root', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where staticFiles really shines, IMO. In the "global" middleware case.

Trying to re-use staticFiles as an inline middleware on a specific route feels a little awkward because we have to pass the param from the route to the middleware somehow. I think I'd prefer to have a more direct approach in a route handler, something like:

router.get('/assets/*path', (context) => {
  let file = findFile('./assets', context.params.path)
  if (file != null) return sendFile(file, context) // sendFile could handle ETag, Ranges, etc.
  return new Response('Not Found', { status: 404 })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add static-middleware to fetch-router

4 participants