I have a router set up with page.js that routes an index page (at '/'
), project pages (at '/projects/<projectId>/'
), and static (image) files. It looks, basically, like this:
JavaScript
init: function () {
page.base('/');
// IndexPage
page('', function (context, next) {
displayIndexPage();
});
// ProjectPage
page('projects/:project', function (context, next) {
displayProjectPage();
});
// image files
page(/^.+\.(jpg|png|gif|bmp)$/, function (context, next) {
window.location = '/' + context.path;
});
// Exit the middleware
page();
}
In my /projects/
folder, I have an .htaccess
file with the following:
bash
Options +FollowSymLinks
RewriteEngine On
# html5 pushstate (history) support
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index
RewriteRule ^.*$ ../index.html [QSA,L,NE]
The routing is generally working correctly. When I navigate to a static image file, .htaccess
drops me outside of my single-page app, and the static file appears on screen. However, when I navigate back in my browser, I don't re-enter my app; URL changes, but there is no navigation, the image file remains on screen. No amount of popping or pushing state from there gets me back into my app.
I'm not totally sure I'm asking the right question here; not sure this is about serving static files as much as it is about routing a single-page app...I would be satisfied with any answer that allows page.js to pick back up on a popState
.
EDIT: I'm exempting files from my RewriteRule
because I need to serve them in the pages rendered by my app. However, when a visitor clicks on an image in a rendered page, I'd like the browser to simply display that image, with no markup.