]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(warn): help migrating catch all routes
authorMasaya Kazama <miyan@t-p.jp>
Wed, 30 Sep 2020 11:47:17 +0000 (20:47 +0900)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 2 Oct 2020 09:47:53 +0000 (11:47 +0200)
__tests__/matcher/addingRemoving.spec.ts
__tests__/matcher/pathParser.spec.ts
src/matcher/index.ts
src/matcher/pathTokenizer.ts

index a3ebabb3160b566320df16dd90e1631b429960ee..6130754f4b6e66b9575954ade2fcc045c61f3d8c 100644 (file)
@@ -15,6 +15,24 @@ describe('Matcher: adding and removing records', () => {
     })
   })
 
+  it('throws when adding *', () => {
+    const matcher = createRouterMatcher([], {})
+    expect(() => {
+      matcher.addRoute({ path: '*', component })
+    }).toThrowError('Catch all')
+  })
+
+  it('does not throw when adding * in children', () => {
+    const matcher = createRouterMatcher([], {})
+    expect(() => {
+      matcher.addRoute({
+        path: '/something',
+        component,
+        children: [{ path: '*', component }],
+      })
+    }).not.toThrow()
+  })
+
   it('adds children', () => {
     const matcher = createRouterMatcher([], {})
     matcher.addRoute({ path: '/parent', component, name: 'home' })
index c3355ea6119e720d8a66f8ed444b6a2678df8213..b897243bbd4e340e7dc43957d8fc886eea0b6c44 100644 (file)
@@ -13,6 +13,10 @@ describe('Path parser', () => {
       expect(tokenizePath('')).toEqual([[]])
     })
 
+    it('not start with /', () => {
+      expect(() => tokenizePath('a')).toThrowError(`"a" should be "/a"`)
+    })
+
     it('escapes :', () => {
       expect(tokenizePath('/\\:')).toEqual([
         [{ type: TokenType.Static, value: ':' }],
index de0150911cab3fbe6ac6468578f1b670ebf07029..cb5cc8cd91c7b7945f722fd2c98e4d92c48fa0aa 100644 (file)
@@ -116,6 +116,13 @@ export function createRouterMatcher(
           parent.record.path + (path && connectingSlash + path)
       }
 
+      if (__DEV__ && normalizedRecord.path === '*') {
+        throw new Error(
+          'Catch all routes ("*") must now be defined using a param with a custom regexp.\n' +
+            'See more at https://next.router.vuejs.org/guide/migration/#removed-star-or-catch-all-routes.'
+        )
+      }
+
       // create the object before hand so it can be passed to children
       matcher = createRouteRecordMatcher(normalizedRecord, parent, options)
 
index f5913d3d5ba2054e110c3934c4f5c1a845a8bdc2..92b0cbaa1a83b4bdc19d57bba2e3bc69ea91497b 100644 (file)
@@ -46,9 +46,18 @@ const VALID_PARAM_RE = /[a-zA-Z0-9_]/
 export function tokenizePath(path: string): Array<Token[]> {
   if (!path) return [[]]
   if (path === '/') return [[ROOT_TOKEN]]
+  // // v3 catchAll must be renew
+  // if (/^\/?\*/.test(path))
+  //   throw new Error(
+  //     `Catch all routes (/*) must now be defined using a parameter with a custom regex: /:catchAll(.*)`
+  //   )
   // remove the leading slash
-  if (!path.startsWith('/'))
-    throw new Error(`Route "${path}" should be "/${path}".`)
+  if (__DEV__ && !path.startsWith('/')) {
+    throw new Error(
+      `Route path should start with a "/": "${path}" should be "/${path}". This will break in production.`
+    )
+    path = '/' + path
+  }
 
   // if (tokenCache.has(path)) return tokenCache.get(path)!