-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(fetch-router): add static-middleware #10808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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', { |
There was a problem hiding this comment.
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 })
})Co-authored-by: Michael Jackson <[email protected]>
This is still a work in progress, so I'm just sharing for some early feedback.