mattdesl / budo

:clapper: a dev server for rapid prototyping
MIT License
2.17k stars 106 forks source link

Allow dynamic generation of index.html #127

Closed ashaffer closed 7 years ago

ashaffer commented 8 years ago

For server-side rendering apps, it'd be nice to be able to specify your index generator as a function parameterized by the current request.

mattdesl commented 8 years ago

Can you provide a bit more details/code examples of how this might look?

Currently there are two fields that can be used to modify the response of the default index:

ashaffer commented 8 years ago

Hmm maybe the middleware option can do what i'm thinking. But essentially I just want defaultIndex to receive the request, so that it can say, serve a different index file based on the current url, or cookies or whatever.

yoshuawuyts commented 8 years ago

@ashaffer what you're describing feels relatively complex; I don't think this falls within the scope of what budo does. Instead I feel you should be able to roll your own, using the components that budo is built on, as described in #122. That way budo stays lean, but you can adjust whatever you need to fit your use case. Does that work for you?

ashaffer commented 8 years ago

@yoshuawuyts Is it complex? Rendering a dynamic index.html seems as simple as could be. Just expose a function like: index(req) -> Promise. Problem solved. Somewhere inside budo this is already happening anyway i'm sure.

mattdesl commented 8 years ago

I just want defaultIndex to receive the request, so that it can say, serve a different index file based on the current url, or cookies or whatever.

Hmm, it isn't so simple right now. The server checks if a default index exists in any of the static paths, and if not, then it calls defaultIndex to respond with the HTML stream.

Perhaps we could add a findDefaultIndex function option... but it's getting a bit hairy/complex. I think what you are aiming for should be possible with middleware, however, you might need to add inject-lr-script yourself. Maybe some changes in budo could be made to improve this (like querying the current live options) – I have run into this problem myself in another project.

mattdesl commented 8 years ago

Another option:

Right now I'm exploring/testing a forceDefaultIndex flag (may rename it). It will always serve the defualtIndex stream instead of something local. We could add a second parameter to the function, so things look like this:

budo({
  forceDefaultIndex: true,
  defaultIndex: function (opt, req) {
      if (req.url === '/index.html') return simpleHtmlStream();
      else return someOtherHtmlStream();
  }
})
ashaffer commented 8 years ago

Ya that looks great to me.

mattdesl commented 8 years ago

serve a different index file based on the current url, or cookies or whatever.

Ignore my last post. This isn't actually possible since the defaultIndex stream is only used when the req.url matches /index.html or /.

I think for custom routing you should use a middleware function; the only downside is you will not get LiveReload injected into an HTML response.

P.S. The server code has just been refactored a bunch. It should be a bit easier to grasp and also add custom middlewares.

mattdesl commented 7 years ago

I don't think this issue is relevant with 10 anymore, all the features should be there for custom middlewares/index/etc. Thanks!