-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move prefetch logic out of useLink and into composables #288
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { computed, MaybeRef, ref, watch } from 'vue' | ||
import { isWithComponent, isWithComponents } from '@/types/createRouteOptions' | ||
import { getPrefetchOption, PrefetchConfigs } from '@/types/prefetch' | ||
import { ResolvedRoute } from '@/types/resolved' | ||
import { isAsyncComponent } from '@/utilities/components' | ||
|
||
export function usePrefetchedComponent(route: MaybeRef<ResolvedRoute | undefined>, prefetch: MaybeRef<PrefetchConfigs | undefined>): void { | ||
const routeRef = ref(route) | ||
const prefetchRef = ref(prefetch) | ||
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: using |
||
|
||
const matchesToPrefetchComponents = computed(() => { | ||
const matches = routeRef.value?.matches ?? [] | ||
|
||
return matches.filter(route => getPrefetchOption({ | ||
...prefetchRef.value, | ||
routePrefetch: route.prefetch, | ||
}, 'components')) | ||
}) | ||
|
||
watch(matchesToPrefetchComponents, routes => { | ||
routes.forEach(route => { | ||
if (isWithComponent(route) && isAsyncComponent(route.component)) { | ||
route.component.setup() | ||
} | ||
|
||
if (isWithComponents(route)) { | ||
Object.values(route.components).forEach(component => { | ||
if (isAsyncComponent(component)) { | ||
component.setup() | ||
} | ||
}) | ||
} | ||
}) | ||
}, { immediate: true }) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||
import { MaybeRef, Ref, ref, watch } from 'vue' | ||||||
import { usePropStore } from '@/compositions/usePropStore' | ||||||
import { PrefetchConfigs } from '@/types/prefetch' | ||||||
import { ResolvedRoute } from '@/types/resolved' | ||||||
|
||||||
export type UsePrefetchedProps = { | ||||||
prefetchProps: Ref<Record<string, unknown>>, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably don't need to expose this right? |
||||||
commitPrefetchedProps: () => void, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: kinda like how simple "commit" would be here.
Suggested change
|
||||||
} | ||||||
|
||||||
export function usePrefetchedProps(route: MaybeRef<ResolvedRoute | undefined>, prefetch: MaybeRef<PrefetchConfigs | undefined>): UsePrefetchedProps { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another spot to use One thing I did in my version of this was may this a single object so you could do this
Don't feel strongly, just felt nice when I was playing with it. |
||||||
const routeRef = ref(route) | ||||||
const prefetchRef = ref(prefetch) | ||||||
const store = usePropStore() | ||||||
const prefetchProps = ref<Record<string, unknown>>({}) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this isn't reactive so we should just use a
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you are exposing it. I guess I wouldn't expose it and I wouldn't make it reactive. That would be my suggestion. |
||||||
|
||||||
watch([routeRef, prefetchRef], ([route, prefetch]) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The single object works nicely here just because you end up with this
|
||||||
if (!route) { | ||||||
return | ||||||
} | ||||||
|
||||||
prefetchProps.value = store.getPrefetchProps(route, prefetch ?? {}) | ||||||
}, { immediate: true }) | ||||||
|
||||||
function commitPrefetchedProps(): void { | ||||||
store.setPrefetchProps(prefetchProps.value) | ||||||
} | ||||||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like the "commit" idea ❤️ I struggled with what to call this. |
||||||
|
||||||
return { prefetchProps, commitPrefetchedProps } | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MaybeRefOrGetter
is preferred overMaybeRef
just because its more flexible.