2

Having problem while refreshing the page when HTML5 Mode is set. When navigating to /path, it works fine but when i refresh the page or directly type localhost/path it does not work.

HTML5 Mode

Configuration:

$routeProvider
  .when('/path', {
    templateUrl: 'path.html',
  });
$locationProvider
  .html5Mode(true);

You should set the base in HTML-file

<html>
  <head>
    <base href="/">
  </head>
</html>

In this mode you can use links without the # in HTML files

<a href="/path">link</a>

Link in Browser:

http://www.example.com/base/path

Update on the server. At this moment, i am just using gulp module to connect.

var gulp = require('gulp');
var connect = require('gulp-connect');

gulp.task('connect', function(){
  connect.server({
      root: './app',
      port: 8080
  })
})
5
  • HTML5 Mode uses PushState to change the URL without making a new server request. however, refreshes and direct page loads will still make a server call, and need to be handled by the server. Which server technology are you using, and is it configured with PushState support? Commented Mar 8, 2015 at 20:56
  • @Claies: We are using Java as backend. Commented Mar 8, 2015 at 20:57
  • ok, so what, Jetty? Jakarta Tomcat? 4J? some other web server? Commented Mar 8, 2015 at 21:00
  • @Claies: At this moment i am just using gulp connect module to host.... i have not yet integrated with actual back-end. updated my original post. Commented Mar 8, 2015 at 21:02
  • right, so node then. well, this is easy with express since it supports rewrites, but I don't know about gulp-connect. In essence, you need to take any request that comes in to your server and redirect it to your index.html. Commented Mar 8, 2015 at 21:06

2 Answers 2

2

To make it work, you need to override all requests to non-static resources by your index.html...

For connect using connect-modrewrite module it would be, something like this:

var connectModRewrite = require('connect-modrewrite');

connect.server({
  root: './app',
  port: 8080,
  middleware: function (connect) {
    return [
      connectModRewrite([
        '!\\.html|\\.js|\\.css|\\.ico|\\.png|\\.gif|\\.jpg|\\.jpeg|\\.swf.*$ /index.html [NC,L]'
      ]),
      connect.static('./app')
    ];
  }
});   
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a middle-ware to solve the problem. Do an npm install --save connect-history-api-fallback. Add the following to your gulpfile.js

var gulp = require('gulp');
var connect = require('gulp-connect');
gulp.task('connect', function() {
  connect.server({
    root: './app',
    port: 8080,
    livereload: true,

    middleware: function(connect, opt) {
      return [ historyApiFallback ];
    }

  });
});

1 Comment

Please take note of the changed API since v1.0.0. The middleware is no longer exported, but needs to be explicitly created. refer: github.com/bripkens/connect-history-api-fallback/issues/10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.