Skip to content

Commit 132d23a

Browse files
committed
Articles trailing slash example
Example of the Articles trailing slash $urlRouterProvider.rule
1 parent 5105eb1 commit 132d23a

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

modules/articles/client/config/articles.client.routes.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,27 @@
55
.module('articles.routes')
66
.config(routeConfig);
77

8-
routeConfig.$inject = ['$stateProvider'];
8+
routeConfig.$inject = ['$stateProvider', '$urlRouterProvider'];
9+
10+
function routeConfig($stateProvider, $urlRouterProvider) {
11+
12+
// This Rule works here, but NOT when defined in /core/client/app/init.js
13+
// And as a bonus behavior, this Rule is applied to all routes in the application
14+
// which lends me to believe we're seeing this behavior due to the order of which the
15+
// modules are loaded; alphabetically?
16+
$urlRouterProvider.rule(function ($injector, $location) {
17+
var path = $location.path();
18+
var hasTrailingSlash = path.length > 1 && path[path.length - 1] === '/';
19+
20+
if (hasTrailingSlash) {
21+
22+
//if last charcter is a slash, return the same url without the slash
23+
var newPath = path.substr(0, path.length - 1);
24+
//return newPath;
25+
$location.replace().path(newPath);
26+
}
27+
});
928

10-
function routeConfig($stateProvider) {
1129
$stateProvider
1230
.state('articles', {
1331
abstract: true,

modules/core/client/app/init.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,54 @@
44
angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies);
55

66
// Setting HTML5 Location Mode
7-
angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', '$httpProvider',
8-
function ($locationProvider, $httpProvider) {
7+
angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', '$httpProvider', '$urlRouterProvider',
8+
function ($locationProvider, $httpProvider, $urlRouterProvider) {
9+
10+
// This Rule works for all routes
11+
$urlRouterProvider.rule(function ($injector, $location) {
12+
//what this function returns will be set as the $location.url
13+
var path = $location.path(), normalized = path.toLowerCase();
14+
if (path !== normalized) {
15+
//instead of returning a new url string, I'll just change the $location.path directly so I don't have to worry about constructing a new url string and so a new state change is not triggered
16+
$location.replace().path(normalized);
17+
}
18+
// because we've returned nothing, no state change occurs
19+
});
20+
21+
/*
22+
This route config will cause conflicts
23+
24+
articles - { url: '/articles', abstract: true }
25+
articles.list - { url: '' }
26+
articles.view - { url: '/:articleId' }
27+
28+
Is the solution to refactor this route config? The below routes won't have the same conflict
29+
30+
articles - { url: '/articles', abstract: true }
31+
articles.list - { url: '' }
32+
33+
article - { url: '/article', abstract: true }
34+
article.view - { url: '/:articleId' }
35+
36+
*/
37+
38+
// This Rule won't work with routes that have the above mentioned configuration,
39+
// but it DOES work when defined directly in the Articles routes config with the same configuration.
40+
// Why would we see this behavior??
41+
42+
/*$urlRouterProvider.rule(function ($injector, $location) {
43+
var path = $location.path();
44+
var hasTrailingSlash = path.length > 1 && path[path.length - 1] === '/';
45+
46+
if (hasTrailingSlash) {
47+
48+
//if last character is a slash, return the same url without the slash
49+
var newPath = path.substr(0, path.length - 1);
50+
//return newPath;
51+
$location.replace().path(newPath);
52+
}
53+
});*/
54+
955
$locationProvider.html5Mode(true).hashPrefix('!');
1056

1157
$httpProvider.interceptors.push('authInterceptor');

0 commit comments

Comments
 (0)