You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

778 lines
22 KiB

4 weeks ago
  1. import Vue from 'vue'
  2. import fetch from 'unfetch'
  3. import middleware from './middleware.js'
  4. import {
  5. applyAsyncData,
  6. promisify,
  7. middlewareSeries,
  8. sanitizeComponent,
  9. resolveRouteComponents,
  10. getMatchedComponents,
  11. getMatchedComponentsInstances,
  12. flatMapComponents,
  13. setContext,
  14. getLocation,
  15. compile,
  16. getQueryDiff,
  17. globalHandleError
  18. } from './utils.js'
  19. import { createApp, NuxtError } from './index.js'
  20. import fetchMixin from './mixins/fetch.client'
  21. import NuxtLink from './components/nuxt-link.client.js' // should be included after ./index.js
  22. // Fetch mixin
  23. if (!Vue.__nuxt__fetch__mixin__) {
  24. Vue.mixin(fetchMixin)
  25. Vue.__nuxt__fetch__mixin__ = true
  26. }
  27. // Component: <NuxtLink>
  28. Vue.component(NuxtLink.name, NuxtLink)
  29. Vue.component('NLink', NuxtLink)
  30. if (!global.fetch) { global.fetch = fetch }
  31. // Global shared references
  32. let _lastPaths = []
  33. let app
  34. let router
  35. // Try to rehydrate SSR data from window
  36. const NUXT = window.__NUXT__ || {}
  37. Object.assign(Vue.config, {"silent":false,"performance":true})
  38. const logs = NUXT.logs || []
  39. if (logs.length > 0) {
  40. const ssrLogSyle = 'background: #2E495E;border-radius: 0.5em;color: white;font-weight: bold;padding: 2px 0.5em;'
  41. console.group && console.group ('%cNuxt SSR', ssrLogSyle)
  42. logs.forEach(logObj => (console[logObj.type] || console.log)(...logObj.args))
  43. delete NUXT.logs
  44. console.groupEnd && console.groupEnd()
  45. }
  46. // Setup global Vue error handler
  47. if (!Vue.config.$nuxt) {
  48. const defaultErrorHandler = Vue.config.errorHandler
  49. Vue.config.errorHandler = (err, vm, info, ...rest) => {
  50. // Call other handler if exist
  51. let handled = null
  52. if (typeof defaultErrorHandler === 'function') {
  53. handled = defaultErrorHandler(err, vm, info, ...rest)
  54. }
  55. if (handled === true) {
  56. return handled
  57. }
  58. if (vm && vm.$root) {
  59. const nuxtApp = Object.keys(Vue.config.$nuxt)
  60. .find(nuxtInstance => vm.$root[nuxtInstance])
  61. // Show Nuxt Error Page
  62. if (nuxtApp && vm.$root[nuxtApp].error && info !== 'render function') {
  63. vm.$root[nuxtApp].error(err)
  64. }
  65. }
  66. if (typeof defaultErrorHandler === 'function') {
  67. return handled
  68. }
  69. // Log to console
  70. if (process.env.NODE_ENV !== 'production') {
  71. console.error(err)
  72. } else {
  73. console.error(err.message || err)
  74. }
  75. }
  76. Vue.config.$nuxt = {}
  77. }
  78. Vue.config.$nuxt.$nuxt = true
  79. const errorHandler = Vue.config.errorHandler || console.error
  80. // Create and mount App
  81. createApp().then(mountApp).catch(errorHandler)
  82. function componentOption (component, key, ...args) {
  83. if (!component || !component.options || !component.options[key]) {
  84. return {}
  85. }
  86. const option = component.options[key]
  87. if (typeof option === 'function') {
  88. return option(...args)
  89. }
  90. return option
  91. }
  92. function mapTransitions (toComponents, to, from) {
  93. const componentTransitions = (component) => {
  94. const transition = componentOption(component, 'transition', to, from) || {}
  95. return (typeof transition === 'string' ? { name: transition } : transition)
  96. }
  97. const fromComponents = from ? getMatchedComponents(from) : []
  98. const maxDepth = Math.max(toComponents.length, fromComponents.length)
  99. const mergedTransitions = []
  100. for (let i=0; i<maxDepth; i++) {
  101. // Clone original objects to prevent overrides
  102. const toTransitions = Object.assign({}, componentTransitions(toComponents[i]))
  103. const transitions = Object.assign({}, componentTransitions(fromComponents[i]))
  104. // Combine transitions & prefer `leave` properties of "from" route
  105. Object.keys(toTransitions)
  106. .filter(key => typeof toTransitions[key] !== 'undefined' && !key.toLowerCase().includes('leave'))
  107. .forEach((key) => { transitions[key] = toTransitions[key] })
  108. mergedTransitions.push(transitions)
  109. }
  110. return mergedTransitions
  111. }
  112. async function loadAsyncComponents (to, from, next) {
  113. // Check if route changed (this._routeChanged), only if the page is not an error (for validate())
  114. this._routeChanged = Boolean(app.nuxt.err) || from.name !== to.name
  115. this._paramChanged = !this._routeChanged && from.path !== to.path
  116. this._queryChanged = !this._paramChanged && from.fullPath !== to.fullPath
  117. this._diffQuery = (this._queryChanged ? getQueryDiff(to.query, from.query) : [])
  118. if ((this._routeChanged || this._paramChanged) && this.$loading.start && !this.$loading.manual) {
  119. this.$loading.start()
  120. }
  121. try {
  122. if (this._queryChanged) {
  123. const Components = await resolveRouteComponents(
  124. to,
  125. (Component, instance) => ({ Component, instance })
  126. )
  127. // Add a marker on each component that it needs to refresh or not
  128. const startLoader = Components.some(({ Component, instance }) => {
  129. const watchQuery = Component.options.watchQuery
  130. if (watchQuery === true) {
  131. return true
  132. }
  133. if (Array.isArray(watchQuery)) {
  134. return watchQuery.some(key => this._diffQuery[key])
  135. }
  136. if (typeof watchQuery === 'function') {
  137. return watchQuery.apply(instance, [to.query, from.query])
  138. }
  139. return false
  140. })
  141. if (startLoader && this.$loading.start && !this.$loading.manual) {
  142. this.$loading.start()
  143. }
  144. }
  145. // Call next()
  146. next()
  147. } catch (error) {
  148. const err = error || {}
  149. const statusCode = err.statusCode || err.status || (err.response && err.response.status) || 500
  150. const message = err.message || ''
  151. // Handle chunk loading errors
  152. // This may be due to a new deployment or a network problem
  153. if (/^Loading( CSS)? chunk (\d)+ failed\./.test(message)) {
  154. window.location.reload(true /* skip cache */)
  155. return // prevent error page blinking for user
  156. }
  157. this.error({ statusCode, message })
  158. this.$nuxt.$emit('routeChanged', to, from, err)
  159. next()
  160. }
  161. }
  162. function applySSRData (Component, ssrData) {
  163. if (NUXT.serverRendered && ssrData) {
  164. applyAsyncData(Component, ssrData)
  165. }
  166. Component._Ctor = Component
  167. return Component
  168. }
  169. // Get matched components
  170. function resolveComponents (router) {
  171. const path = getLocation(router.options.base, router.options.mode)
  172. return flatMapComponents(router.match(path), async (Component, _, match, key, index) => {
  173. // If component is not resolved yet, resolve it
  174. if (typeof Component === 'function' && !Component.options) {
  175. Component = await Component()
  176. }
  177. // Sanitize it and save it
  178. const _Component = applySSRData(sanitizeComponent(Component), NUXT.data ? NUXT.data[index] : null)
  179. match.components[key] = _Component
  180. return _Component
  181. })
  182. }
  183. function callMiddleware (Components, context, layout) {
  184. let midd = []
  185. let unknownMiddleware = false
  186. // If layout is undefined, only call global middleware
  187. if (typeof layout !== 'undefined') {
  188. midd = [] // Exclude global middleware if layout defined (already called before)
  189. layout = sanitizeComponent(layout)
  190. if (layout.options.middleware) {
  191. midd = midd.concat(layout.options.middleware)
  192. }
  193. Components.forEach((Component) => {
  194. if (Component.options.middleware) {
  195. midd = midd.concat(Component.options.middleware)
  196. }
  197. })
  198. }
  199. midd = midd.map((name) => {
  200. if (typeof name === 'function') {
  201. return name
  202. }
  203. if (typeof middleware[name] !== 'function') {
  204. unknownMiddleware = true
  205. this.error({ statusCode: 500, message: 'Unknown middleware ' + name })
  206. }
  207. return middleware[name]
  208. })
  209. if (unknownMiddleware) {
  210. return
  211. }
  212. return middlewareSeries(midd, context)
  213. }
  214. async function render (to, from, next) {
  215. if (this._routeChanged === false && this._paramChanged === false && this._queryChanged === false) {
  216. return next()
  217. }
  218. // Handle first render on SPA mode
  219. if (to === from) {
  220. _lastPaths = []
  221. } else {
  222. const fromMatches = []
  223. _lastPaths = getMatchedComponents(from, fromMatches).map((Component, i) => {
  224. return compile(from.matched[fromMatches[i]].path)(from.params)
  225. })
  226. }
  227. // nextCalled is true when redirected
  228. let nextCalled = false
  229. const _next = (path) => {
  230. if (from.path === path.path && this.$loading.finish) {
  231. this.$loading.finish()
  232. }
  233. if (from.path !== path.path && this.$loading.pause) {
  234. this.$loading.pause()
  235. }
  236. if (nextCalled) {
  237. return
  238. }
  239. nextCalled = true
  240. next(path)
  241. }
  242. // Update context
  243. await setContext(app, {
  244. route: to,
  245. from,
  246. next: _next.bind(this)
  247. })
  248. this._dateLastError = app.nuxt.dateErr
  249. this._hadError = Boolean(app.nuxt.err)
  250. // Get route's matched components
  251. const matches = []
  252. const Components = getMatchedComponents(to, matches)
  253. // If no Components matched, generate 404
  254. if (!Components.length) {
  255. // Default layout
  256. await callMiddleware.call(this, Components, app.context)
  257. if (nextCalled) {
  258. return
  259. }
  260. // Load layout for error page
  261. const errorLayout = (NuxtError.options || NuxtError).layout
  262. const layout = await this.loadLayout(
  263. typeof errorLayout === 'function'
  264. ? errorLayout.call(NuxtError, app.context)
  265. : errorLayout
  266. )
  267. await callMiddleware.call(this, Components, app.context, layout)
  268. if (nextCalled) {
  269. return
  270. }
  271. // Show error page
  272. app.context.error({ statusCode: 404, message: 'This page could not be found' })
  273. return next()
  274. }
  275. // Update ._data and other properties if hot reloaded
  276. Components.forEach((Component) => {
  277. if (Component._Ctor && Component._Ctor.options) {
  278. Component.options.asyncData = Component._Ctor.options.asyncData
  279. Component.options.fetch = Component._Ctor.options.fetch
  280. }
  281. })
  282. // Apply transitions
  283. this.setTransitions(mapTransitions(Components, to, from))
  284. try {
  285. // Call middleware
  286. await callMiddleware.call(this, Components, app.context)
  287. if (nextCalled) {
  288. return
  289. }
  290. if (app.context._errored) {
  291. return next()
  292. }
  293. // Set layout
  294. let layout = Components[0].options.layout
  295. if (typeof layout === 'function') {
  296. layout = layout(app.context)
  297. }
  298. layout = await this.loadLayout(layout)
  299. // Call middleware for layout
  300. await callMiddleware.call(this, Components, app.context, layout)
  301. if (nextCalled) {
  302. return
  303. }
  304. if (app.context._errored) {
  305. return next()
  306. }
  307. // Call .validate()
  308. let isValid = true
  309. try {
  310. for (const Component of Components) {
  311. if (typeof Component.options.validate !== 'function') {
  312. continue
  313. }
  314. isValid = await Component.options.validate(app.context)
  315. if (!isValid) {
  316. break
  317. }
  318. }
  319. } catch (validationError) {
  320. // ...If .validate() threw an error
  321. this.error({
  322. statusCode: validationError.statusCode || '500',
  323. message: validationError.message
  324. })
  325. return next()
  326. }
  327. // ...If .validate() returned false
  328. if (!isValid) {
  329. this.error({ statusCode: 404, message: 'This page could not be found' })
  330. return next()
  331. }
  332. let instances
  333. // Call asyncData & fetch hooks on components matched by the route.
  334. await Promise.all(Components.map((Component, i) => {
  335. // Check if only children route changed
  336. Component._path = compile(to.matched[matches[i]].path)(to.params)
  337. Component._dataRefresh = false
  338. const childPathChanged = Component._path !== _lastPaths[i]
  339. // Refresh component (call asyncData & fetch) when:
  340. // Route path changed part includes current component
  341. // Or route param changed part includes current component and watchParam is not `false`
  342. // Or route query is changed and watchQuery returns `true`
  343. if (this._routeChanged && childPathChanged) {
  344. Component._dataRefresh = true
  345. } else if (this._paramChanged && childPathChanged) {
  346. const watchParam = Component.options.watchParam
  347. Component._dataRefresh = watchParam !== false
  348. } else if (this._queryChanged) {
  349. const watchQuery = Component.options.watchQuery
  350. if (watchQuery === true) {
  351. Component._dataRefresh = true
  352. } else if (Array.isArray(watchQuery)) {
  353. Component._dataRefresh = watchQuery.some(key => this._diffQuery[key])
  354. } else if (typeof watchQuery === 'function') {
  355. if (!instances) {
  356. instances = getMatchedComponentsInstances(to)
  357. }
  358. Component._dataRefresh = watchQuery.apply(instances[i], [to.query, from.query])
  359. }
  360. }
  361. if (!this._hadError && this._isMounted && !Component._dataRefresh) {
  362. return
  363. }
  364. const promises = []
  365. const hasAsyncData = (
  366. Component.options.asyncData &&
  367. typeof Component.options.asyncData === 'function'
  368. )
  369. const hasFetch = Boolean(Component.options.fetch) && Component.options.fetch.length
  370. const loadingIncrease = (hasAsyncData && hasFetch) ? 30 : 45
  371. // Call asyncData(context)
  372. if (hasAsyncData) {
  373. const promise = promisify(Component.options.asyncData, app.context)
  374. .then((asyncDataResult) => {
  375. applyAsyncData(Component, asyncDataResult)
  376. if (this.$loading.increase) {
  377. this.$loading.increase(loadingIncrease)
  378. }
  379. })
  380. promises.push(promise)
  381. }
  382. // Check disabled page loading
  383. this.$loading.manual = Component.options.loading === false
  384. // Call fetch(context)
  385. if (hasFetch) {
  386. let p = Component.options.fetch(app.context)
  387. if (!p || (!(p instanceof Promise) && (typeof p.then !== 'function'))) {
  388. p = Promise.resolve(p)
  389. }
  390. p.then((fetchResult) => {
  391. if (this.$loading.increase) {
  392. this.$loading.increase(loadingIncrease)
  393. }
  394. })
  395. promises.push(p)
  396. }
  397. return Promise.all(promises)
  398. }))
  399. // If not redirected
  400. if (!nextCalled) {
  401. if (this.$loading.finish && !this.$loading.manual) {
  402. this.$loading.finish()
  403. }
  404. next()
  405. }
  406. } catch (err) {
  407. const error = err || {}
  408. if (error.message === 'ERR_REDIRECT') {
  409. return this.$nuxt.$emit('routeChanged', to, from, error)
  410. }
  411. _lastPaths = []
  412. globalHandleError(error)
  413. // Load error layout
  414. let layout = (NuxtError.options || NuxtError).layout
  415. if (typeof layout === 'function') {
  416. layout = layout(app.context)
  417. }
  418. await this.loadLayout(layout)
  419. this.error(error)
  420. this.$nuxt.$emit('routeChanged', to, from, error)
  421. next()
  422. }
  423. }
  424. // Fix components format in matched, it's due to code-splitting of vue-router
  425. function normalizeComponents (to, ___) {
  426. flatMapComponents(to, (Component, _, match, key) => {
  427. if (typeof Component === 'object' && !Component.options) {
  428. // Updated via vue-router resolveAsyncComponents()
  429. Component = Vue.extend(Component)
  430. Component._Ctor = Component
  431. match.components[key] = Component
  432. }
  433. return Component
  434. })
  435. }
  436. function showNextPage (to) {
  437. // Hide error component if no error
  438. if (this._hadError && this._dateLastError === this.$options.nuxt.dateErr) {
  439. this.error()
  440. }
  441. // Set layout
  442. let layout = this.$options.nuxt.err
  443. ? (NuxtError.options || NuxtError).layout
  444. : to.matched[0].components.default.options.layout
  445. if (typeof layout === 'function') {
  446. layout = layout(app.context)
  447. }
  448. this.setLayout(layout)
  449. }
  450. // When navigating on a different route but the same component is used, Vue.js
  451. // Will not update the instance data, so we have to update $data ourselves
  452. function fixPrepatch (to, ___) {
  453. if (this._routeChanged === false && this._paramChanged === false && this._queryChanged === false) {
  454. return
  455. }
  456. const instances = getMatchedComponentsInstances(to)
  457. const Components = getMatchedComponents(to)
  458. Vue.nextTick(() => {
  459. instances.forEach((instance, i) => {
  460. if (!instance || instance._isDestroyed) {
  461. return
  462. }
  463. if (
  464. instance.constructor._dataRefresh &&
  465. Components[i] === instance.constructor &&
  466. instance.$vnode.data.keepAlive !== true &&
  467. typeof instance.constructor.options.data === 'function'
  468. ) {
  469. const newData = instance.constructor.options.data.call(instance)
  470. for (const key in newData) {
  471. Vue.set(instance.$data, key, newData[key])
  472. }
  473. // Ensure to trigger scroll event after calling scrollBehavior
  474. window.$nuxt.$nextTick(() => {
  475. window.$nuxt.$emit('triggerScroll')
  476. })
  477. }
  478. })
  479. showNextPage.call(this, to)
  480. // Hot reloading
  481. setTimeout(() => hotReloadAPI(this), 100)
  482. })
  483. }
  484. function nuxtReady (_app) {
  485. window.onNuxtReadyCbs.forEach((cb) => {
  486. if (typeof cb === 'function') {
  487. cb(_app)
  488. }
  489. })
  490. // Special JSDOM
  491. if (typeof window._onNuxtLoaded === 'function') {
  492. window._onNuxtLoaded(_app)
  493. }
  494. // Add router hooks
  495. router.afterEach((to, from) => {
  496. // Wait for fixPrepatch + $data updates
  497. Vue.nextTick(() => _app.$nuxt.$emit('routeChanged', to, from))
  498. })
  499. }
  500. const noopData = () => { return {} }
  501. const noopFetch = () => {}
  502. // Special hot reload with asyncData(context)
  503. function getNuxtChildComponents ($parent, $components = []) {
  504. $parent.$children.forEach(($child) => {
  505. if ($child.$vnode && $child.$vnode.data.nuxtChild && !$components.find(c =>(c.$options.__file === $child.$options.__file))) {
  506. $components.push($child)
  507. }
  508. if ($child.$children && $child.$children.length) {
  509. getNuxtChildComponents($child, $components)
  510. }
  511. })
  512. return $components
  513. }
  514. function hotReloadAPI(_app) {
  515. if (!module.hot) return
  516. let $components = getNuxtChildComponents(_app.$nuxt, [])
  517. $components.forEach(addHotReload.bind(_app))
  518. }
  519. function addHotReload ($component, depth) {
  520. if ($component.$vnode.data._hasHotReload) return
  521. $component.$vnode.data._hasHotReload = true
  522. var _forceUpdate = $component.$forceUpdate.bind($component.$parent)
  523. $component.$vnode.context.$forceUpdate = async () => {
  524. let Components = getMatchedComponents(router.currentRoute)
  525. let Component = Components[depth]
  526. if (!Component) {
  527. return _forceUpdate()
  528. }
  529. if (typeof Component === 'object' && !Component.options) {
  530. // Updated via vue-router resolveAsyncComponents()
  531. Component = Vue.extend(Component)
  532. Component._Ctor = Component
  533. }
  534. this.error()
  535. let promises = []
  536. const next = function (path) {
  537. this.$loading.finish && this.$loading.finish()
  538. router.push(path)
  539. }
  540. await setContext(app, {
  541. route: router.currentRoute,
  542. isHMR: true,
  543. next: next.bind(this)
  544. })
  545. const context = app.context
  546. if (this.$loading.start && !this.$loading.manual) {
  547. this.$loading.start()
  548. }
  549. callMiddleware.call(this, Components, context)
  550. .then(() => {
  551. // If layout changed
  552. if (depth !== 0) {
  553. return
  554. }
  555. let layout = Component.options.layout || 'default'
  556. if (typeof layout === 'function') {
  557. layout = layout(context)
  558. }
  559. if (this.layoutName === layout) {
  560. return
  561. }
  562. let promise = this.loadLayout(layout)
  563. promise.then(() => {
  564. this.setLayout(layout)
  565. Vue.nextTick(() => hotReloadAPI(this))
  566. })
  567. return promise
  568. })
  569. .then(() => {
  570. return callMiddleware.call(this, Components, context, this.layout)
  571. })
  572. .then(() => {
  573. // Call asyncData(context)
  574. let pAsyncData = promisify(Component.options.asyncData || noopData, context)
  575. pAsyncData.then((asyncDataResult) => {
  576. applyAsyncData(Component, asyncDataResult)
  577. this.$loading.increase && this.$loading.increase(30)
  578. })
  579. promises.push(pAsyncData)
  580. // Call fetch()
  581. Component.options.fetch = Component.options.fetch || noopFetch
  582. let pFetch = Component.options.fetch.length && Component.options.fetch(context)
  583. if (!pFetch || (!(pFetch instanceof Promise) && (typeof pFetch.then !== 'function'))) { pFetch = Promise.resolve(pFetch) }
  584. pFetch.then(() => this.$loading.increase && this.$loading.increase(30))
  585. promises.push(pFetch)
  586. return Promise.all(promises)
  587. })
  588. .then(() => {
  589. this.$loading.finish && this.$loading.finish()
  590. _forceUpdate()
  591. setTimeout(() => hotReloadAPI(this), 100)
  592. })
  593. }
  594. }
  595. async function mountApp (__app) {
  596. // Set global variables
  597. app = __app.app
  598. router = __app.router
  599. // Create Vue instance
  600. const _app = new Vue(app)
  601. // Load layout
  602. const layout = NUXT.layout || 'default'
  603. await _app.loadLayout(layout)
  604. _app.setLayout(layout)
  605. // Mounts Vue app to DOM element
  606. const mount = () => {
  607. _app.$mount('#__nuxt')
  608. // Add afterEach router hooks
  609. router.afterEach(normalizeComponents)
  610. router.afterEach(fixPrepatch.bind(_app))
  611. // Listen for first Vue update
  612. Vue.nextTick(() => {
  613. // Call window.{{globals.readyCallback}} callbacks
  614. nuxtReady(_app)
  615. // Enable hot reloading
  616. hotReloadAPI(_app)
  617. })
  618. }
  619. // Resolve route components
  620. const Components = await Promise.all(resolveComponents(router))
  621. // Enable transitions
  622. _app.setTransitions = _app.$options.nuxt.setTransitions.bind(_app)
  623. if (Components.length) {
  624. _app.setTransitions(mapTransitions(Components, router.currentRoute))
  625. _lastPaths = router.currentRoute.matched.map(route => compile(route.path)(router.currentRoute.params))
  626. }
  627. // Initialize error handler
  628. _app.$loading = {} // To avoid error while _app.$nuxt does not exist
  629. if (NUXT.error) {
  630. _app.error(NUXT.error)
  631. }
  632. // Add beforeEach router hooks
  633. router.beforeEach(loadAsyncComponents.bind(_app))
  634. router.beforeEach(render.bind(_app))
  635. // If page already is server rendered and it was done on the same route path as client side render
  636. if (NUXT.serverRendered && NUXT.routePath === _app.context.route.path) {
  637. mount()
  638. return
  639. }
  640. // First render on client-side
  641. const clientFirstMount = () => {
  642. normalizeComponents(router.currentRoute, router.currentRoute)
  643. showNextPage.call(_app, router.currentRoute)
  644. // Don't call fixPrepatch.call(_app, router.currentRoute, router.currentRoute) since it's first render
  645. mount()
  646. }
  647. render.call(_app, router.currentRoute, router.currentRoute, (path) => {
  648. // If not redirected
  649. if (!path) {
  650. clientFirstMount()
  651. return
  652. }
  653. // Add a one-time afterEach hook to
  654. // mount the app wait for redirect and route gets resolved
  655. const unregisterHook = router.afterEach((to, from) => {
  656. unregisterHook()
  657. clientFirstMount()
  658. })
  659. // Push the path and let route to be resolved
  660. router.push(path, undefined, (err) => {
  661. if (err) {
  662. errorHandler(err)
  663. }
  664. })
  665. })
  666. }