-
Notifications
You must be signed in to change notification settings - Fork 916
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
Fixes #970 by adding middleware dev config option. #1062
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/pikapkg/snowpack/88j3vft3z |
Thanks for the PR! +1 on the overall idea here, but we may want to think this through a bit first. also /cc @Rich-Harris any interest in this feature on your end? A couple of thoughts:
|
Of course, it's just an idea. I agree that proxying a subtree of requests is the idiomatic way to go here, I just have legacy code outside of my control where that doesn't work due to strange design. Let's only merge this if others find it useful. In regards to the footgun you mentioned, there's no way for middleware to alter or even access the dev server assets (without explicitly sending a separate HTTP request to the dev server). However, I do see a danger of middleware routes unintentionally shadowing asset URLs. If there's some way to put the middleware "behind" the dev server, that problem would be mitigated, but Snowpack currently sends What if we reserved the |
That's a great point, this would come BEFORE the normal build pipeline, not after. So in this implementation, that concern of mine isn't actually real. huh, this may actually work best exactly as is, which is exciting because it looks so simple! /cc @drwpow any thoughts on this? |
Okay, lets do it. I feel really good about this direction, especially given the new interest in Snowpack's SSR story. I may rename to Thanks again for introducing this! |
Changes
Hello,
As an alternative to exposing the Snowpack dev server as Express-like middleware, I think we
could accomplish the same goal by adding a
middleware
dev option insnowpack.config.js
.It would accept a
(req, res, next) => void
function to allow users to intercept requests beforethey reach the dev server. This way, the logic is inverted and Snowpack is able to "drive" --
users don't have to worry about HMR, websockets, etc.
For example, I could handle
/api/route
like so:Testing
I tested my changes by using the code above and altering an app created with
app-template-blank to fetch
/api/route
and log the response. I got{ "some": "json" }
in the console and the rest of the page worked unchanged.I'm happy to add a test, but this affects the dev server only and I haven't come across
testing for that. Feel free to point me in the right direction.