pandeiro / boot-http

A simple HTTP serve task for the Boot build tool
62 stars 34 forks source link

pushState compatibility #23

Closed briandunn closed 8 years ago

briandunn commented 9 years ago

I'd love to see pushState compatibility. I want to serve with dir, but have all 404s redirect to the index file. Is there a way to achieve this without writing a custom ring handler?

briandunn commented 9 years ago

wrote a ring handler that is working for me: https://github.com/briandunn/seqseq/blob/0cb3b6b870d3a7802fc715fb0fcf26252dbb3b9c/src/seqseq/history_handler.clj

kirillsalykin commented 9 years ago

+1, would be awesome!

pandeiro commented 9 years ago

@briandunn @kirillsalykin Maybe this could take the form of an additional parameter, :not-found, which could specify a var representing a ring handler function for handling 404s. I think that would have broader uses as well, but also allow for simply serving index.html or whatever for html5 routing and things like that. I'd take a PR along those lines if you're interested.

kirillsalykin commented 9 years ago

@pandeiro i am newbie in the magic clojure world, but i'll try ;) P.S. not sure how long it would take

pandeiro commented 9 years ago

Happy to help if you're blocked with anything. :)

briandunn commented 9 years ago

Don't have time to role a PR out of this right now but it should do the trick. @kirillsalykin

https://github.com/briandunn/seqseq/commit/0cb3b6b870d3a7802fc715fb0fcf26252dbb3b9c

kirillsalykin commented 9 years ago

@briandunn thanks! will try find time to make PR from this

briandunn commented 8 years ago

@pandeiro working on this PR and I have a couple questions.

This description: https://github.com/pandeiro/boot-http/blob/2bde616d023057af032d42a41764d51838a7c3ea/src/pandeiro/boot_http.clj#L28

Makes it sound like boot-http can serve both resources and a directory. However if I am reading this bit correctly:

https://github.com/pandeiro/boot-http/blob/2bde616d023057af032d42a41764d51838a7c3ea/src/pandeiro/boot_http/impl.clj#L102-L104

it is going to either serve a handler, or a directory, or resources, but never both at once. Does that sound right? And in that case, would you be interested in a PR that clarifies the usage and raises when misconfigured to do both? (i.e. specifying both a :root and a :dir)

And another thing; I'm hazy on what resources are. Is a fallback 404 handler appropriate or useful in the resource serving context?

pandeiro commented 8 years ago

Hey @briandunn -- Sorry for taking so long to reply.

So you are correct that there are those three "modes" of serving, but the directory handler also includes resources. (This is something we may want to provide a flag to turn off, some day -- it can produce some magical results that will work with boot-http but not work when serving the compiled assets from another HTTP server.)

Serving resources means serving files from the classpath, essentially. These can be both things that are contained in JARs included in your project and things specified in directories that are included in your project's Boot :resource-paths key.

kirillsalykin commented 8 years ago

@pandeiro hi, also sorry for taking so long :)

i wanted to discuss - do we need this inside boot-http or we can just add some documentation explaining how to add this?

pandeiro commented 8 years ago

Hey @kirillsalykin -- I'd say both are viable options. What do you think?

Because I haven't really had the need for this, I am not sure I've thought through all the possibilities. But I think possibly this use case, where you want any previously unspecified route (e.g. a "frontend" route like /views/record/123) to simply load index.html and resolve the route on the client side, is something one would just write a specific handler for. The downside there is that then, if you want to serve directories/resources, you have to manually set that up, but it's not too much code at all.

The other option I see would be to add a special flag that, when present, causes "Not found" routes to go to index.html instead of a default 404 page. I think that would also work -- but like I said, I haven't thought through that / tried to implement that, so I may be missing some consequences of that.

kirillsalykin commented 8 years ago

as i can see only me and @briandunn required it :)

And thanks to @briandunn for working solution.

So i guess putting it in the readme will be enough. @briandunn, what do you think?

yeehaa123 commented 8 years ago

@briandunn Thanks for writing the handler. For me, however, it only works for one level of routes. Nested routes don't work properly. I think, @pandeiro's second option would be a fitter for my setup, but I don't know how to implement that.

I tried the following change without success:

(def app (-> handler
             (wrap-file (str dir "css/")  {:index-files? false})
             (wrap-file (str dir '"js/") {:index-files? false})
             (wrap-index dir)))

In other words, I removed wrap-resource and specifically added a wrap-file for each of my static content dirs.

Can anyone contributing to this conversation (@brianddunn @pandeiro @kirillsalykin) guide me in the right direction?

yeehaa123 commented 8 years ago

think I found the problem. In addition to all the changes that @briandunn includes in his pull request, you also need to add a forward-slash in front of your asset-path under compiler-options. Was a really trick thing to debug. Took me an entire day...

Also made a version of the history_handler that uses hiccup to render the html dynamically. You can find it here:

https://gist.github.com/yeehaa123/d73304e2516cf1ae4c32

briandunn commented 8 years ago

@yeehaa123 Happy to hear that you solved it. I finally got around to rolling this PR, but I don't know if it will help in your specific situation.

pandeiro commented 8 years ago

This was handled via #35.