How can I avoid the performance hit each time a user refreshes an angularjs page when using html 5 mode with rails? I am using Angularjs html5 mode Ruby on Rails route redirects. With my setup if I visit http://localhost:3000/users the url immediately becomes http://localhost:3000/?goto=users which then redirects to http://localhost:3000/users. What can I do to avoid redirects of this nature when using html5 mode?
Rails Routes
myApp::Application.routes.draw do
namespace :api, defaults: {format: :json} do
namespace :v1 do
resources :users
end
end
# Re-route all angular requests to the angular router
get "/*path" => redirect("/?goto=%{path}")
root to: 'application#home'
end
Angular Routes
myApp.config(function($routeProvider, $locationProvider, $httpProvider) {
$httpProvider.defaults.headers.common['X-CSRF-Token'] =
$('meta[name=csrf-token]').attr('content');
$routeProvider.
when('/', {
templateUrl: '../assets/mainIndex.html',
controller: 'MainController',
redirectTo:
function(current, path, search){
if(search.goto){
// if we were passed in a search param, and it has a path
// to redirect to, then redirect to that path
return "/" + search.goto;
}
else{
// else just redirect back to this location
// angular is smart enough to only do this once.
return "/";
}
}
}).when('/users', {
templateUrl: '../assets/users/userIndex.html',
controller: 'UsersController'
}).otherwise({
redirectTo:'/'
});
$locationProvider.html5Mode(true);
});
The above is from this blog: http://omarriott.com/aux/angularjs-html5-routing-rails/