]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(warn): warn if component is a promise
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 May 2020 09:35:26 +0000 (11:35 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 May 2020 09:35:26 +0000 (11:35 +0200)
__tests__/warnings.spec.ts
src/navigationGuards.ts

index 26255078b5f16d9b955ffde442f908d5daadc837..23366b4cda06620d307f601878db65de0b72b6c3 100644 (file)
@@ -163,4 +163,15 @@ describe('warnings', () => {
       'duplicated params with name "id" for path "/:id/:id"'
     ).toHaveBeenWarned()
   })
+
+  it('warns if component is a promise instead of a function that returns a promise', async () => {
+    const router = createRouter({
+      history: createMemoryHistory(),
+      // simulates import('./component.vue')
+      routes: [{ path: '/foo', component: Promise.resolve(component) }],
+    })
+
+    await expect(router.push({ path: '/foo' })).resolves.toBe(undefined)
+    expect('"/foo" is a Promise instead of a function').toHaveBeenWarned()
+  })
 })
index f1c96a7633d42c0b22af67d46cc6d4ec1797696b..c711d830bc117760e15531f1f16ff55506b6a158 100644 (file)
@@ -166,7 +166,16 @@ export function extractComponentsGuards(
 
   for (const record of matched) {
     for (const name in record.components) {
-      const rawComponent = record.components[name]
+      let rawComponent = record.components[name]
+      // warn if user wrote import('/component.vue') instead of () => import('./component.vue')
+      if (__DEV__ && 'then' in rawComponent) {
+        warn(
+          `Component "${name}" in record with path "${record.path}" is a Promise instead of a function that returns a Promise. Did you write "import('./MyPage.vue')" instead of "() => import('./MyPage.vue')"? This will break in production if not fixed.`
+        )
+        let promise = rawComponent
+        rawComponent = () => promise
+      }
+
       if (isRouteComponent(rawComponent)) {
         // __vccOpts is added by vue-class-component and contain the regular options
         let options: ComponentOptions =
@@ -182,7 +191,7 @@ export function extractComponentsGuards(
 
         if (__DEV__ && !('catch' in componentPromise)) {
           warn(
-            `Component "${name}" at record with path "${record.path}" is a function that does not return a Promise. If you were passing a functional component, make sure to add a "displayName" to the component. This will break in production if not fixed.`
+            `Component "${name}" in record with path "${record.path}" is a function that does not return a Promise. If you were passing a functional component, make sure to add a "displayName" to the component. This will break in production if not fixed.`
           )
           componentPromise = Promise.resolve(componentPromise as RouteComponent)
         } else {