Browser-sync: HTML5 mode for single page apps

Created on 4 Jul 2015  路  4Comments  路  Source: BrowserSync/browser-sync

Hi,

Browsersync is great, thanks for putting it together!

In the web server settings, it would be useful to have an "HTML5" mode for single page apps.

Have a look here to see what I mean (search "html5Mode in the readme"): https://github.com/schickling/gulp-webserver

Any plan to support this soon?

Thanks!

Most helpful comment

This can be easily added via middleware. Please review previous conversations here https://github.com/BrowserSync/browser-sync/issues/204

tl;dr

var historyApiFallback = require('connect-history-api-fallback')
var bs = require('browser-sync').create();

bs.init({
   server: {
       baseDir: "./app",
       middleware: [ historyApiFallback() ]
    }
})

All 4 comments

This can be easily added via middleware. Please review previous conversations here https://github.com/BrowserSync/browser-sync/issues/204

tl;dr

var historyApiFallback = require('connect-history-api-fallback')
var bs = require('browser-sync').create();

bs.init({
   server: {
       baseDir: "./app",
       middleware: [ historyApiFallback() ]
    }
})

Hi @shakyShane, thanks for your help! The solution you propose doesn't work though when you try to navigate to a resource without using the hashbang mode.

For example, navigating to localhost:3000/#/profile works fine.

Navigating to localhost:3000/profile tries instead to fetch the profile file from server and returns Cannot GET /profile.

If the direct resource is not present the server needs to always serve index.html.

This is exactly what the gulp-webserver option I pointed to in the previous post does:

gulp.task('webserver', function() {
  gulp.src('app')
    .pipe(webserver({
      fallback: 'index.html'
    }));
});

Looking forward to hear your insight, thanks!

That's exactly what the middleware does. It's how the Browsersync UI works also.

See https://github.com/BrowserSync/recipes/tree/master/recipes/server.middleware

I finally found out where the problem lied. I omitted the baseDir option, thinking it would take the local path as default, since my index.html was in the root folder. Instead it looks like it needs to be specified. Possibly to avoid similar inconveniences in the future the default for baseDir could become ./.

Any thoughts? Cheers :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ericmorand picture ericmorand  路  3Comments

adjavaherian picture adjavaherian  路  4Comments

ilianaza picture ilianaza  路  4Comments

w88975 picture w88975  路  3Comments

sedubois picture sedubois  路  3Comments