Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

stackoverfloweth
Copy link
Contributor

this PR moves logic out of useLink.vue and into 2 new composables usePrefetchedComponent and usePrefetchedProps. The main goal here is reducing complexity of useLink but could also be useful in the future if additional places might use prefetching logic.

Copy link

netlify bot commented Oct 20, 2024

Deploy Preview for kitbag-router ready!

Name Link
🔨 Latest commit cdd7eb9
🔍 Latest deploy log https://app.netlify.com/sites/kitbag-router/deploys/671576b72ec2cc0008ee2763
😎 Deploy Preview https://deploy-preview-288--kitbag-router.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@pleek91 pleek91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. I'd be fine with merging this as is. Comments are mostly nits, thoughts, suggestions. Everything minor.

I was approaching this as a single prefetching composition. Which might be preferred in the future once we get to prefetch strategies. But I like the separation because its easier to grok.

Comment on lines +8 to +9
const routeRef = ref(route)
const prefetchRef = ref(prefetch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: using toValue inside the matchesToPrefetchComponents is preferred

import { ResolvedRoute } from '@/types/resolved'
import { isAsyncComponent } from '@/utilities/components'

export function usePrefetchedComponent(route: MaybeRef<ResolvedRoute | undefined>, prefetch: MaybeRef<PrefetchConfigs | undefined>): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MaybeRefOrGetter is preferred over MaybeRef just because its more flexible.

import { ResolvedRoute } from '@/types/resolved'

export type UsePrefetchedProps = {
prefetchProps: Ref<Record<string, unknown>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need to expose this right?


export type UsePrefetchedProps = {
prefetchProps: Ref<Record<string, unknown>>,
commitPrefetchedProps: () => void,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: kinda like how simple "commit" would be here.

Suggested change
commitPrefetchedProps: () => void,
commit: () => void,

commitPrefetchedProps: () => void,
}

export function usePrefetchedProps(route: MaybeRef<ResolvedRoute | undefined>, prefetch: MaybeRef<PrefetchConfigs | undefined>): UsePrefetchedProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another spot to use MaybeRefOrGetter

One thing I did in my version of this was may this a single object so you could do this

usePrefetchedProps(() => ({
  route: route.value,
  routerPrefetch: router.prefetch,
  linkPrefetch: options.prefetch
}))

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>>({})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't reactive so we should just use a let here. Also, we're in a prefetch props composition. We could probably simplify some of these names.

Suggested change
const prefetchProps = ref<Record<string, unknown>>({})
const props: Record<string, unknown> = {}

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

const store = usePropStore()
const prefetchProps = ref<Record<string, unknown>>({})

watch([routeRef, prefetchRef], ([route, prefetch]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

watch(option, ({ route, ...configs }) => {
  ...
})

Comment on lines +25 to +27
function commitPrefetchedProps(): void {
store.setPrefetchProps(prefetchProps.value)
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants