Skip to content

Commit 7763619

Browse files
committed
feat(loaders): nuxt temp workaround
1 parent 9320646 commit 7763619

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/data-loaders/navigation-guard.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,35 @@ export function setupLoaderGuard({
9999

100100
// we only add async modules because otherwise the component doesn't have any loaders and the user should add
101101
// them with the `loaders` array
102-
if (isAsyncModule(component)) {
103-
const promise = component().then(
104-
(viewModule: Record<string, unknown>) => {
105-
for (const exportName in viewModule) {
106-
const exportValue = viewModule[exportName]
107-
108-
if (isDataLoader(exportValue)) {
109-
record.meta[LOADER_SET_KEY]!.add(exportValue)
110-
}
111-
}
112-
if (Array.isArray(viewModule.__loaders)) {
113-
for (const loader of viewModule.__loaders) {
114-
if (isDataLoader(loader)) {
115-
record.meta[LOADER_SET_KEY]!.add(loader)
116-
}
117-
}
102+
const promise = (
103+
isAsyncModule(component)
104+
? component()
105+
: // we also support __loaders exported as an option to get around some temporary limitations
106+
Promise.resolve(
107+
component as Record<string, unknown> | (() => unknown)
108+
)
109+
).then((viewModule) => {
110+
// avoid checking functional components
111+
if (typeof viewModule === 'function') return
112+
113+
for (const exportName in viewModule) {
114+
const exportValue = viewModule[exportName]
115+
116+
if (isDataLoader(exportValue)) {
117+
record.meta[LOADER_SET_KEY]!.add(exportValue)
118+
}
119+
}
120+
// TODO: remove once nuxt doesn't wrap with `e => e.default` async pages
121+
if (Array.isArray(viewModule.__loaders)) {
122+
for (const loader of viewModule.__loaders) {
123+
if (isDataLoader(loader)) {
124+
record.meta[LOADER_SET_KEY]!.add(loader)
118125
}
119126
}
120-
)
127+
}
128+
})
121129

122-
lazyLoadingPromises.push(promise)
123-
}
130+
lazyLoadingPromises.push(promise)
124131
}
125132
}
126133
}

0 commit comments

Comments
 (0)