-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Prefetch async route components #255
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
143c09a
use toRef rather than computed
pleek91 5173943
Add prefect config types
pleek91 901dde1
prefect must be optional
pleek91 d50c0de
Add a utility for determining if a component is an async wrapper
pleek91 bf3d5a3
WIP
pleek91 936435d
Add conditional to determine if a component should be prefetched or not
pleek91 def653b
Write tests
pleek91 9206ee5
export prefect value utility
pleek91 8acfb7a
Allow passing options when using a url with useLink
pleek91 a68c563
Fix prefetch prop and pass options into useLink
pleek91 119ddb4
Fix types
pleek91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,29 @@ | |
<script setup lang="ts"> | ||
import { computed } from 'vue' | ||
import { useRouter, useLink } from '@/compositions' | ||
import { PrefetchConfig } from '@/types/prefetch' | ||
import { RegisteredRouter } from '@/types/register' | ||
import { RouterPushOptions } from '@/types/routerPush' | ||
import { Url, isUrl } from '@/types/url' | ||
|
||
type ToCallback = (resolve: RegisteredRouter['resolve']) => string | ||
|
||
const props = defineProps<{ to: Url | ToCallback } & RouterPushOptions>() | ||
type RouterLinkProps = { | ||
/** | ||
* The url string to navigate to or a callback that returns a url string | ||
*/ | ||
to: Url | ToCallback, | ||
/** | ||
* Determines what assets are prefetched when router-link is rendered for this route. Overrides route level prefetch. | ||
*/ | ||
prefetch?: PrefetchConfig, | ||
} | ||
|
||
const props = withDefaults(defineProps<RouterLinkProps & RouterPushOptions>(), { | ||
// because prefetch can be a boolean vue automatically sets the default to false. | ||
// Specifically setting the default to undefined | ||
prefetch: undefined, | ||
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. boo macros 👎 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. In general I really like how vue defaults booleans to false. But it can cause some surprising errors when you're not really expecting it. |
||
}) | ||
|
||
defineSlots<{ | ||
default?: (props: { | ||
|
@@ -36,7 +52,7 @@ | |
return options | ||
}) | ||
|
||
const { href, isMatch, isExactMatch } = useLink(resolved) | ||
const { href, isMatch, isExactMatch } = useLink(resolved, options) | ||
|
||
const classes = computed(() => ({ | ||
'router-link--match': isMatch.value, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { isRecord } from '@/utilities' | ||
|
||
export type PrefetchConfigOptions = { | ||
/** | ||
* When true any component that is wrapped in vue's defineAsyncComponent will be prefetched | ||
* @default true | ||
*/ | ||
components?: boolean, | ||
} | ||
|
||
/** | ||
* Determines what assets are prefetched. A boolean enables or disables all prefetching. | ||
*/ | ||
export type PrefetchConfig = boolean | PrefetchConfigOptions | ||
|
||
export const DEFAULT_PREFETCH_CONFIG: Required<PrefetchConfigOptions> = { | ||
components: true, | ||
} | ||
|
||
type PrefetchConfigs = { | ||
routerPrefetch: PrefetchConfig | undefined, | ||
routePrefetch: PrefetchConfig | undefined, | ||
linkPrefetch: PrefetchConfig | undefined, | ||
} | ||
|
||
export function getPrefetchOption({ routerPrefetch, routePrefetch, linkPrefetch }: PrefetchConfigs, setting: keyof PrefetchConfigOptions): boolean { | ||
return getPrefetchConfigValue(linkPrefetch, setting) ?? getPrefetchConfigValue(routePrefetch, setting) ?? getPrefetchConfigValue(routerPrefetch, setting) ?? DEFAULT_PREFETCH_CONFIG[setting] | ||
} | ||
|
||
export function getPrefetchConfigValue(prefetch: PrefetchConfig | undefined, setting: keyof PrefetchConfigOptions): boolean | undefined { | ||
if (isRecord(prefetch)) { | ||
return prefetch[setting] | ||
} | ||
|
||
return prefetch | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
clever!