Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Deprecate routes with nested absolute paths #3246

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions modules/__tests__/isActive-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import IndexRoute from '../IndexRoute'
import Router from '../Router'
import Route from '../Route'
import qs from 'qs'
import shouldWarn from './shouldWarn'

describe('isActive', function () {

Expand Down Expand Up @@ -189,6 +190,10 @@ describe('isActive', function () {
})

describe('a pathname that matches a parent route, but not the URL directly', function () {
beforeEach(() => {
shouldWarn('deprecated')
})

describe('with no query', function () {
it('is active', function (done) {
render((
Expand Down Expand Up @@ -251,6 +256,10 @@ describe('isActive', function () {
})

describe('a pathname that matches a nested absolute path', function () {
beforeEach(() => {
shouldWarn('deprecated')
})

describe('with no query', function () {
it('is active', function (done) {
render((
Expand Down
28 changes: 16 additions & 12 deletions modules/__tests__/matchRoutes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ describe('matchRoutes', function () {
let routes
let
RootRoute, UsersRoute, UsersIndexRoute, UserRoute, PostRoute, FilesRoute,
AboutRoute, TeamRoute, ProfileRoute, GreedyRoute, OptionalRoute,
OptionalRouteChild, CatchAllRoute
AboutRoute, GreedyRoute, OptionalRoute, OptionalRouteChild, CatchAllRoute
let createLocation = createMemoryHistory().createLocation

beforeEach(function () {
/*
<Route>
<Route path="users">
<IndexRoute />
<Route path=":userID">
<Route path="/profile" />
</Route>
<Route path="/team" />
<Route path=":userID" />
</Route>
</Route>
<Route path="/about" />
Expand All @@ -42,16 +38,10 @@ describe('matchRoutes', function () {
UserRoute = {
path: ':userID',
childRoutes: [
ProfileRoute = {
path: '/profile'
},
PostRoute = {
path: ':postID'
}
]
},
TeamRoute = {
path: '/team'
}
]
}
Expand Down Expand Up @@ -234,6 +224,13 @@ describe('matchRoutes', function () {

describe('when the location matches a nested absolute route', function () {
it('matches the correct routes', function (done) {
shouldWarn('deprecated')

const TeamRoute = {
path: '/team'
}
UsersRoute.childRoutes.push(TeamRoute)

matchRoutes(routes, createLocation('/team'), function (error, match) {
expect(match).toExist()
expect(match.routes).toEqual([ RootRoute, UsersRoute, TeamRoute ])
Expand All @@ -244,6 +241,13 @@ describe('matchRoutes', function () {

describe('when the location matches an absolute route nested under a route with params', function () {
it('matches the correct routes and params', function (done) {
shouldWarn('deprecated')

const ProfileRoute = {
path: '/profile'
}
UserRoute.childRoutes.push(ProfileRoute)

matchRoutes(routes, createLocation('/profile'), function (error, match) {
expect(match).toExist()
expect(match.routes).toEqual([ RootRoute, UsersRoute, UserRoute, ProfileRoute ])
Expand Down
2 changes: 2 additions & 0 deletions modules/isActive.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function getMatchingRouteIndex(pathname, activeRoutes, activeParams) {
const pattern = route.path || ''

if (pattern.charAt(0) === '/') {
// This code path is deprecated, but we the deprecation warning will
// actually be hit from matchRoutes, not from here.
remainingPathname = pathname
paramNames = []
paramValues = []
Expand Down
63 changes: 38 additions & 25 deletions modules/matchRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ function matchRouteDeep(
let pattern = route.path || ''

if (pattern.charAt(0) === '/') {
if (remainingPathname !== location.pathname) {
warning(
false,
'Nested routes with absolute paths are deprecated. Nest those routes under pathless routes instead. http://tiny.cc/router-decouple-ui'
)
}

remainingPathname = location.pathname
paramNames = []
paramValues = []
Expand All @@ -91,34 +98,40 @@ function matchRouteDeep(
paramNames = [ ...paramNames, ...matched.paramNames ]
paramValues = [ ...paramValues, ...matched.paramValues ]

if (remainingPathname === '' && route.path) {
const match = {
routes: [ route ],
params: createParams(paramNames, paramValues)
}
if (remainingPathname === '') {
if (route.path) {
const match = {
routes: [ route ],
params: createParams(paramNames, paramValues)
}

getIndexRoute(route, location, function (error, indexRoute) {
if (error) {
callback(error)
} else {
if (Array.isArray(indexRoute)) {
warning(
indexRoute.every(route => !route.path),
'Index routes should not have paths'
)
match.routes.push(...indexRoute)
} else if (indexRoute) {
warning(
!indexRoute.path,
'Index routes should not have paths'
)
match.routes.push(indexRoute)
getIndexRoute(route, location, function (error, indexRoute) {
if (error) {
callback(error)
} else {
if (Array.isArray(indexRoute)) {
warning(
indexRoute.every(route => !route.path),
'Index routes should not have paths'
)
match.routes.push(...indexRoute)
} else if (indexRoute) {
warning(
!indexRoute.path,
'Index routes should not have paths'
)
match.routes.push(indexRoute)
}

callback(null, match)
}
})

callback(null, match)
}
})
return
return
}

// Re-normalize remaining path for continued match.
remainingPathname = '/'
}
}

Expand Down