Using Pow for Single Page Applications

January 23, 2015

I recently started building out the front ends of a project I’ve been working on, and spent longer than I’d like to admit getting Pow to serve it properly. Since I’ve built the backend purely as a RESTful API, the front end is a single page application which manages it’s own state. This works perfectly fine when hitting the root route (ie, /), however Pow fails when hitting any other routes. Seeing as the files don’t actually exist this is understandable, however Single Page Applications are becoming more popular, and it seems like this should be an quick fix.

What we need to do here is rewrite all URLs to serve index.html. This seemed simple enough, however getting Pow to do this was not as straightforward as I had hoped (admittedly probably due to a lack of experience with Ruby). I finally got it working, and thought I’d put some straight forward directions here for future reference.

It turns out we can use the rack-rewrite gem in the config.ru file to rewrite the URLs, as follows:

Note that your Gemfile should look like:

These files need to be in the root directory in Pow; ie, at ~/.pow/<application>. If you are like me and symlink ~/.pow/<application>/public to your actual application, these files will be in the same directory as that symlink.

Run a bundle install, and all requests to http://<application>.dev/* will serve your index.html file.