I am trying to overwrite portions of my single page app using only javascript and AngularJS.
Overwrites are based on subdomain.
Every subdomain is pointing to the same doc root.
controllers.js
controller('AppController', ['$scope','$route','$routeParams','$location', function($scope, $route, $routeParams, $location) {
$scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
render();
});
var render = function(){
//Is it actually a subdomain?
if($location.host().split(".",1).length>2){
//Use subdomain folder if it is.
var url = "views/"+$location.host().split(".",1)+"/"+$route.current.template;
var http = new XMLHttpRequest();
http.onreadystatechange=function(){
if (http.readyState==4){
//If there isn't an overwrite, use the original.
$scope.page = (http.status!=404)?url:("views/"+$route.current.template);
}
}
http.open('HEAD', url, true);
http.send();
}
else{
//Else we are on the parent domain.
$scope.page = "views/"+$route.current.template;
}
};
}])
config.js
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/', {
template: 'home.html'
});
$routeProvider.when('/services', {
template: 'services.html'
});
$routeProvider.otherwise({redirectTo: '/'});
}]);
index.html
<html lang="en" ng-app="myApp" ng-controller="AppController">
<body>
<div ng-include src="page" class="container"></div>
</body>
</html>
Because this is a single page app, when you hit a URL directly, it's going to 404. That's why we apply rewrite rules on the server. In my case I'm using nginx:
location / {
try_files $uri /index.html;
}
This works great when I'm not on a subdomain, but then again I'm also not sending out an XMLHttpRequest. When I do use the subdomain, now we need to check for an overwrite.
The tricky part here is that the rewrite rules are forcing the XMLHttpRequest to return a 200.
Ideas on how I can have my cake and eat it too?