@@ -23,6 +23,7 @@ import {
2323 getCurrentContext ,
2424 setCurrentContext ,
2525 IS_SSR_KEY ,
26+ LOADER_SET_KEY ,
2627} from 'unplugin-vue-router/data-loaders'
2728
2829import { shallowRef } from 'vue'
@@ -31,6 +32,7 @@ import {
3132 toLazyValue ,
3233} from './createDataLoader'
3334import type { ErrorDefault } from './types-config'
35+ import { warn } from '../core/utils'
3436
3537/**
3638 * Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version `data` is always defined.
@@ -215,6 +217,10 @@ export function defineBasicLoader<Data>(
215217 // let the navigation guard collect the result
216218 if ( d instanceof NavigationResult ) {
217219 to . meta [ NAVIGATION_RESULTS_KEY ] ! . push ( d )
220+ // help users find non-exposed loaders during development
221+ if ( process . env . NODE_ENV !== 'production' ) {
222+ warnNonExposedLoader ( { to, options, useDataLoader } )
223+ }
218224 } else {
219225 entry . staged = d
220226 entry . stagedError = null
@@ -229,6 +235,12 @@ export function defineBasicLoader<Data>(
229235 // e
230236 // )
231237 if ( entry . pendingLoad === currentLoad ) {
238+ // help users find non-exposed loaders during development
239+ if ( process . env . NODE_ENV !== 'production' ) {
240+ if ( e instanceof NavigationResult ) {
241+ warnNonExposedLoader ( { to, options, useDataLoader } )
242+ }
243+ }
232244 // in this case, commit will never be called so we should just drop the error
233245 // console.log(`🚨 error in "${options.key}"`, e)
234246 entry . stagedError = e
@@ -412,6 +424,32 @@ export function defineBasicLoader<Data>(
412424 return useDataLoader
413425}
414426
427+ /**
428+ * Dev only warning for loaders that return/throw NavigationResult but are not exposed
429+ *
430+ * @param to - [TODO:description]
431+ * @param options - [TODO:description]
432+ * @param useDataLoader - [TODO:description]
433+ */
434+ function warnNonExposedLoader ( {
435+ to,
436+ options,
437+ useDataLoader,
438+ } : {
439+ to : RouteLocationNormalizedLoaded
440+ options : DefineDataLoaderOptions_LaxData
441+ useDataLoader : UseDataLoader
442+ } ) {
443+ const loaders = to . meta [ LOADER_SET_KEY ]
444+ console . log ( options . key )
445+ if ( loaders && ! loaders . has ( useDataLoader ) ) {
446+ warn (
447+ 'A loader returned a NavigationResult but is not registered on the route. Did you forget to "export" it from the page component?' +
448+ ( options . key ? ` (loader key: "${ options . key } ")` : '' )
449+ )
450+ }
451+ }
452+
415453export interface DefineDataLoaderOptions_LaxData
416454 extends DefineDataLoaderOptionsBase_LaxData {
417455 /**
0 commit comments