Skip to content

Commit

Permalink
feat: allow to specify preload fetch priority
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli committed Sep 15, 2023
1 parent 2dfc4b3 commit 305b813
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion playground/pages/provider/[provider].vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<br>
<div class="providerShowcase">
<div v-for="sample of provider.samples" :key="sample.src">
<nuxt-img :provider="provider.name" v-bind="sample" />
<nuxt-img :provider="provider.name" v-bind="sample" :preload="{ fetchPriority: 'auto' }" />
<pre>{{ sample }}</pre>
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/components/_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export const baseImageProps = {

sizes: { type: [Object, String] as unknown as () => string | Record<string, any>, default: undefined },
densities: { type: String, default: undefined },
preload: { type: Boolean, default: undefined },
preload: {
type: [Boolean, Object] as unknown as () => boolean | {
fetchPriority: 'auto' | 'high' | 'low'
},
default: undefined
},

// <img> attributes
width: { type: [String, Number], default: undefined },
Expand Down
1 change: 1 addition & 0 deletions src/runtime/components/nuxt-img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default defineComponent({
link: [{
rel: 'preload',
as: 'image',
...(typeof props.preload !== 'boolean' && props.preload.fetchPriority ? { fetchpriority: props.preload.fetchPriority } : {}),
...(!isResponsive
? { href: src.value }
: {
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/components/nuxt-picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ export default defineComponent({
const lastSourceIndex = computed(() => sources.value.length - 1)

if (props.preload) {
const link: any = { rel: 'preload', as: 'image', imagesrcset: sources.value[0].srcset }
const link: any = {
rel: 'preload',
as: 'image',
imagesrcset: sources.value[0].srcset,
...(typeof props.preload !== 'boolean' && props.preload.fetchPriority
? { fetchpriority: props.preload.fetchPriority }
: {})
}

if (sources.value?.[0]?.sizes) { link.imagesizes = sources.value[0].sizes }

Expand Down

0 comments on commit 305b813

Please sign in to comment.