Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

fix(core): Remove trailing slash from routes #1238

Merged
merged 1 commit into from
Mar 7, 2016

Conversation

mleanos
Copy link
Member

@mleanos mleanos commented Feb 28, 2016

Prevents transitioning to a URL with a trailing slash.

If a trailing slash is found in the transitioning URL, then the transition is aborted & the user is redirected to the path without the trailing slash.

Adds missing client-side route configuration tests.

Demonstrates that the various route configurations can handle a trailing slash in the URL, and resolve to the correct client route.

Fixes #1075

@mleanos mleanos added this to the 0.5.0 milestone Feb 28, 2016
@mleanos mleanos self-assigned this Feb 28, 2016
@mleanos
Copy link
Member Author

mleanos commented Feb 28, 2016

@jloveland @vaucouleur How does this solution seem to you?

My fix assumes that we want to remove the trailing slash. However, maybe that's not the desired behavior? As I was exploring possible solutions, I didn't see a straight forward way of keeping the trailing slash, but resolving to the correct client route.

I don't see a good reason to keep the slash.

@mleanos mleanos force-pushed the fix/ui-router-trailing-slash branch 2 times, most recently from f41a25c to ef30bef Compare February 29, 2016 08:42
@mleanos
Copy link
Member Author

mleanos commented Feb 29, 2016

I'm a lot happier with this solution. We can define a $urlRouterProvider Rule that removes the trailing slashes from the URL, and it applies to all routes. The Rule is only defined once, in the Core module route configuration.

In order for this to work, we have to load the Core module routes into the angular app, before we load any other routes. This required a change to the default configuration, to change the order of which we're loading the client-side module javascript files. Now the Core module files are loaded first; which really makes sense to me anyways. We probably should have been doing that to begin with.

@jloveland @vaucouleur @ilanbiala Review?

@ilanbiala
Copy link
Member

The code seems pretty good. 👍

@mleanos mleanos changed the title Fix/ui router trailing slash fix(core): Remove trailing slash from routes Feb 29, 2016
@mleanos
Copy link
Member Author

mleanos commented Mar 2, 2016

@rhutchison pointed out that we can avoid manually ensuring that the Core module loads first; with my changes to the default asset configuration. If we register the Articles module with Core as a dependency; app.registerModule('articles', ['core']); Injecting Core as a dependency is probably the preferred approach. I'll update this PR accordingly.

@mleanos mleanos force-pushed the fix/ui-router-trailing-slash branch from ef30bef to 17f72e4 Compare March 2, 2016 18:30
@mleanos
Copy link
Member Author

mleanos commented Mar 5, 2016

@codydaig @ilanbiala @rhutchison LGTY?

@ilanbiala
Copy link
Member

LGTM.

@@ -1,7 +1,7 @@
(function (app) {
'use strict';

app.registerModule('articles');
app.registerModule('articles', ['core']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mleanos should we add a comment above this line to indicate core is required to provide deterministic order..forcing core to load first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't sound like a bad idea

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this forces Core to load first though. I know my wording above indicates that it does. But now that I think about it, all this does is add Core as a dependency; it's sort of ambiguous as to whether Core is indeed loading first, or not.

Perhaps a comment more along the lines of Core being required for special route handling? This aspect of module dependency feels weird to me. But I guess it's the intention of Angular's module dependency injection. I really feel like we should be able to tell Angular to load Core first, or into each module. Rather than injecting it into every single module definition. However, I guess that's a bit off topic :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your suggestion..something like "Core module is required for special route handling"

I am not sure either about the module loading order, from the docs it describes how modules are asynchronously loaded:

Because modules do nothing at load time they can be loaded into the VM in any order and thus script loaders can take advantage of this property and parallelize the loading process.

but then, the docs describe how dependencies are loaded first:

Modules can list other modules as their dependencies. Depending on a module implies that the required module needs to be loaded before the requiring module is loaded. In other words the configuration blocks of the required modules execute before the configuration blocks of the requiring module.

@jloveland
Copy link
Contributor

@mleanos one thing I noticed is that trying to navigate to a bad route on articles doesn't give a Page Not Found response as it does on the root.
for example, try http://0.0.0.0:3000/articles/foo it just shows blank page.
on http://0.0.0.0:3000/foo and http://0.0.0.0:3000/chat/foo it will show Page Not Found

@mleanos
Copy link
Member Author

mleanos commented Mar 7, 2016

@jloveland I'll look into that in a bit. There's probably a console error in the browser. If so, what is it?

@mleanos
Copy link
Member Author

mleanos commented Mar 7, 2016

Actually, just checked on the master branch, and I'm seeing the same behavior. No console errors, but the server is returning a 400 response, because of the invalid articleId param ("foo").

I'll see if there's an easy fix. It seems like a good idea to add a fix for that to this PR. WDYT?

@jloveland
Copy link
Contributor

Seems like fixing the articles invalid article param would be small enough to be included here..

@mleanos mleanos force-pushed the fix/ui-router-trailing-slash branch from 17f72e4 to 35c9208 Compare March 7, 2016 04:10
@mleanos
Copy link
Member Author

mleanos commented Mar 7, 2016

@ilanbiala @jloveland I added the comment to just the Articles client module. I think this is where it's more applicable, and generally users will use Articles to base their other modules off of.

To fix the invalid ArticleId issue, I added a new service for implementing $http interceptors for 400 & 404 server errors. Let me know if this looks good to you; I partly feel this should be in it's own PR, but if you're all happy then I don't think it's a huge deal (just more to review).

Adds an angular $urlRouterProvider service Rule to the Core module
configuration, that removes any trailing slashes in the URL for all routes.

The Rule is defined in the core routes configuration. Thus, in order for
this to work on all routes in the application, we have to inject the Core
module into each client module, as a dependecy in the client.module
configuration. Otherwise, we'd have to define the Rule in each module's route
configuration individually.

Adds missing client-side route configuration tests.

Tests demonstrate that the various route configurations can handle a trailing
slash in the URL, and gets resolved to the correct client route.

Fixes meanjs#1075
@mleanos mleanos force-pushed the fix/ui-router-trailing-slash branch from 35c9208 to b004986 Compare March 7, 2016 04:56
@mleanos
Copy link
Member Author

mleanos commented Mar 7, 2016

I reverted the commit with the $http Interceptors. I forgot why we hadn't implemented them for 404 & 400 already. We're sending back 400 & 404 error responses from the server, where we don't want to redirect the user. It's going to be a bit more involved to fix this issue. I'd rather tackle that in a separate issue, and PR.

@ilanbiala @jloveland SGTY?

@jloveland
Copy link
Contributor

SGTM

@codydaig
Copy link
Member

codydaig commented Mar 7, 2016

LGTM

@mleanos
Copy link
Member Author

mleanos commented Mar 7, 2016

Merging since all the feedback has been addressed.

mleanos added a commit that referenced this pull request Mar 7, 2016
fix(core): Remove trailing slash from routes
@mleanos mleanos merged commit 09697f8 into meanjs:master Mar 7, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Routes with trailing / don't resolve properly
4 participants