Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
98501f4
feat: preconnect to external image provider origins
luc122c Oct 17, 2026
01adcf4
docs: document the `preconnect` option
luc122c Oct 17, 2026
613b6a8
fix: guard useRequestURL in usePreconnect, cover NuxtPicture
luc122c Oct 17, 2026
08354ba
types: make CreateImageOptions.preconnect optional
luc122c Oct 17, 2026
ffcc8c8
docs: document preconnect on the NuxtPicture page
luc122c Oct 17, 2026
13f307b
chore: clarify usePreconnect JSDoc
luc122c Oct 17, 2026
3adcc1b
test: cover preconnect crossorigin hint and distinct connection modes
luc122c Oct 17, 2026
3ee6d22
feat: preconnect to a placeholder's origin too
luc122c Oct 17, 2026
a19b0ed
chore: drop a redundant comment in NuxtImg preconnect block
luc122c Oct 17, 2026
f45627e
fix: mirror the image's crossorigin in NuxtPicture preconnect hints
luc122c Oct 17, 2026
a84f921
test: update bundle size snapshot
luc122c Oct 17, 2026
01f614b
docs: clarify preconnect dedupe by connection mode
luc122c Oct 18, 2026
7f77bef
refactor: rename `usePreconnect` to `addPreconnect`
luc122c Oct 20, 2026
729ccd6
refactor: move preconnect helper out of the public composables
luc122c Oct 20, 2026
6421946
chore: mark preconnect helper as `@internal`
luc122c Oct 20, 2026
e415259
refactor: consolidate the preconnect helpers into one module
luc122c Oct 20, 2026
396ae4e
feat: make preconnect opt-in via a component prop
luc122c Oct 30, 2026
e5c80c5
chore: update bundle size snapshot
luc122c Oct 30, 2026
36f1494
Merge remote-tracking branch 'upstream/main'
luc122c Nov 2, 2026
d087bed
fix: let the global preconnect option apply when the prop is absent
luc122c Nov 2, 2026
739687f
chore: update bundle size snapshot after merging main
luc122c Nov 2, 2026
14c024c
style: compress preconnect comments to single lines
luc122c Nov 2, 2026
a3bf60c
fix: keep protocol-relative preconnect origins scheme-less
luc122c Nov 2, 2026
947d366
refactor: stop exporting the internal origin resolver
luc122c Nov 2, 2026
9bd85bb
chore: retrigger CI
luc122c Nov 2, 2026
67618ad
fix: keep an explicit port on protocol-relative preconnect hints
luc122c Nov 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/1.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ export default defineNuxtConfig({
})
```

## `preconnect`

Nuxt Image can add a [`<link rel="preconnect">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preconnect) hint to the page head for the origins of the images rendered on the server. This lets the browser start the DNS, TCP and TLS handshake with your image provider before the images are parsed.

Preconnecting is opt-in: enable it per image with the [`preconnect` prop](/modules/image/usage/nuxt-img#preconnect), or for every image by setting `preconnect` to `true` in your configuration.

```ts [nuxt.config.ts]
export default defineNuxtConfig({
image: {
preconnect: true
}
})
```

Only external `http(s)` origins are preconnected — relative and same-origin images (for example the default `ipx` provider) are ignored. Images from the same origin and connection mode (`crossorigin`) share a single hint.

Enable preconnecting for the images that appear above the fold. Every hint starts a connection, so browsers and Lighthouse recommend keeping the number of preconnected origins small.

## `quality`

The quality for the generated image(s).
Expand Down
16 changes: 16 additions & 0 deletions docs/2.usage/1.nuxt-img.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,22 @@ Accepted values are `auto`, `high`, or `low`.
<NuxtImg src="/nuxt-icon.png" :preload="{ fetchPriority: 'high' }" />
```

### `preconnect`

Adds a [`<link rel="preconnect">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preconnect) hint for the origin of the image that is rendered on the server, so the browser can start the DNS, TCP and TLS handshake with your image provider before the image is parsed.

```vue
<NuxtImg
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
preconnect
/>
```

Only external `http(s)` origins produce a hint — relative and same-origin images (for example the default `ipx` provider) are ignored, and images sharing an origin and `crossorigin` mode share a single hint. When a [`placeholder`](#placeholder) is rendered, the hint follows the placeholder URL, as that is the image the browser fetches first.

Preconnecting is disabled by default. Enable it for images that appear above the fold rather than every image on the page, since each hint starts a connection. To enable it for every image, set [`preconnect`](/modules/image/configuration#preconnect) in your configuration.

### `loading`

This is a [native](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading) attribute that provides a hint
Expand Down
2 changes: 2 additions & 0 deletions docs/2.usage/2.nuxt-picture.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Learn more about the [`<picture>` tag on MDN](https://developer.mozilla.org/en-U
Unlike the native [`<picture>` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture), `<NuxtPicture>` currently doesn't support using multiple image sources. See [#309](https://github.com/nuxt/image/issues/309) for more info.
::

**Note:** Like `<NuxtImg>`, `<NuxtPicture>` can preconnect to the origins of its external image sources during server rendering. Pass the [`preconnect` prop](/modules/image/usage/nuxt-img#preconnect) to opt in.

## Props

::tip
Expand Down
4 changes: 4 additions & 0 deletions playground/app/pages/img-preload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
height="500"
densities="x1"
preload
preconnect
/>
<h1>Preload with densities and no sizes prop</h1>
<nuxt-img
Expand All @@ -15,6 +16,7 @@
height="500"
densities="x1 x2"
preload
preconnect
/>

<h1>Preload with densities and sizes prop</h1>
Expand All @@ -25,6 +27,7 @@
sizes="100vw md:500px"
densities="x1 x2"
preload
preconnect
/>

<h1>Preload with densities, sizes prop and default fetch priority high</h1>
Expand All @@ -35,6 +38,7 @@
sizes="100vw md:500px"
densities="x1 x2"
:preload="{ fetchPriority: 'high' }"
preconnect
/>
</div>
</template>
11 changes: 11 additions & 0 deletions playground/app/pages/picture-preconnect.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
<nuxt-picture
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="500"
height="500"
preconnect
/>
</div>
</template>
20 changes: 20 additions & 0 deletions playground/app/pages/preconnect-crossorigin-picture.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<template>
<div>
<nuxt-picture
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="100"
height="100"
:img-attrs="{ crossorigin: 'use-credentials' }"
preconnect
/>
<nuxt-picture
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="120"
height="120"
:img-attrs="{ crossorigin: '' }"
preconnect
/>
</div>
</template>
27 changes: 27 additions & 0 deletions playground/app/pages/preconnect-crossorigin.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<template>
<div>
<nuxt-img
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="100"
height="100"
crossorigin="anonymous"
preconnect
/>
<nuxt-img
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="120"
height="120"
preconnect
/>
<nuxt-img
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="140"
height="140"
crossorigin="anonymous"
preconnect
/>
</div>
</template>
12 changes: 12 additions & 0 deletions playground/app/pages/preconnect-disabled.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<template>
<div>
<!-- opts out of the global `image.preconnect` option -->
<nuxt-img
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="100"
height="100"
:preconnect="false"
/>
</div>
</template>
26 changes: 26 additions & 0 deletions playground/app/pages/preconnect-ignored.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<div>
<!-- none of these sources can be preconnected: data, non-http(s) and same-origin relative -->
<nuxt-img
provider="none"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
width="10"
height="10"
preconnect
/>
<nuxt-img
provider="none"
src="ftp://cdn.example.com/a.jpg"
width="20"
height="20"
preconnect
/>
<nuxt-img
provider="none"
src="/images/nuxt.png"
width="30"
height="30"
preconnect
/>
</div>
</template>
11 changes: 11 additions & 0 deletions playground/app/pages/preconnect-opt-out.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
<!-- renders a cross-origin image without opting in to preconnect -->
<nuxt-img
provider="cloudinary"
src="/remote/nuxt-org/blog/going-full-static/main"
width="100"
height="100"
/>
</div>
</template>
11 changes: 11 additions & 0 deletions playground/app/pages/preconnect-placeholder.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
<nuxt-img
src="/images/nuxt.png"
width="100"
height="100"
placeholder="https://res.cloudinary.com/nuxt/image/upload/images/nuxt.png"
preconnect
/>
</div>
</template>
26 changes: 26 additions & 0 deletions playground/app/pages/preconnect-protocol-relative.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<div>
<!-- a protocol-relative and an explicit http source must not share a hint -->
<nuxt-img
provider="none"
src="//res.cloudinary.com/nuxt/image/upload/images/nuxt.png"
width="100"
height="100"
preconnect
/>
<nuxt-img
provider="none"
src="http://res.cloudinary.com/nuxt/image/upload/images/nuxt.png"
width="120"
height="120"
preconnect
/>
<nuxt-img
provider="none"
src="//res.cloudinary.com:80/nuxt/image/upload/images/nuxt.png"
width="140"
height="140"
preconnect
/>
</div>
</template>
1 change: 1 addition & 0 deletions playground/app/pages/provider/[provider].vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
:provider="provider.name as any"
v-bind="sample"
:preload="{ fetchPriority: 'auto' }"
preconnect
/>
<pre>{{ sample }}</pre>
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ModuleOptions extends ImageProviders {
densities: number[]
format: CreateImageOptions['format']
quality?: CreateImageOptions['quality']
preconnect: boolean
}

export * from './types'
Expand All @@ -45,6 +46,7 @@ export default defineNuxtModule<ModuleOptions>({
providers: {},
alias: {},
densities: [1, 2],
preconnect: false,
}),
meta: {
name: '@nuxt/image',
Expand Down Expand Up @@ -113,6 +115,7 @@ export default defineNuxtModule<ModuleOptions>({
'densities',
'format',
'quality',
'preconnect',
])

const providers = await resolveProviders(nuxt, options)
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/components/NuxtImg.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { computed, onMounted, ref, useAttrs, useTemplateRef } from 'vue'
import type { ImgHTMLAttributes } from 'vue'

import { useImage } from '../composables'
import { addPreconnect } from '../utils/preconnect'
import { prerenderStaticImages } from '../utils/prerender'
import { markFeatureUsage } from '../utils/performance'
import { useImageProps } from '../utils/props'
Expand All @@ -35,7 +36,8 @@ export interface ImageProps<Provider extends keyof ConfiguredImageProviders> ext
placeholderClass?: string
}

const props = defineProps<ImageProps<Provider>>()
// `undefined` lets the global `image.preconnect` option apply
const props = withDefaults(defineProps<ImageProps<Provider>>(), { preconnect: undefined })

const emit = defineEmits<{
(event: 'load', payload: Event): unknown
Expand Down Expand Up @@ -103,6 +105,11 @@ const mainSrc = computed(() =>

const src = computed(() => placeholder.value || mainSrc.value)

if (import.meta.server && (props.preconnect ?? $img.options.preconnect)) {
// use the rendered URL: reading `mainSrc` forces `getSizes` during SSR with a placeholder
addPreconnect([src.value], { crossorigin: normalizedAttrs.value.crossorigin })
}

if (import.meta.server && props.preload) {
const hasMultipleDensities = sizes.value.srcset.includes('x, ')
const isResponsive = hasMultipleDensities || !!sizes.value.sizes
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/components/NuxtPicture.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { computed, onMounted, useAttrs, useTemplateRef } from 'vue'
import { prerenderStaticImages } from '../utils/prerender'
import { markFeatureUsage } from '../utils/performance'
import { useImage } from '../composables'
import { addPreconnect, type PreconnectOptions } from '../utils/preconnect'
import { useImageProps } from '../utils/props'
import type { BaseImageProps } from '../utils/props'
import type { DataAttributes } from '../types'
Expand All @@ -44,7 +45,8 @@ export interface PictureProps<Provider extends keyof ConfiguredImageProviders> e

defineOptions({ inheritAttrs: false })

const props = defineProps<PictureProps<Provider>>()
// `undefined` lets the global `image.preconnect` option apply
const props = withDefaults(defineProps<PictureProps<Provider>>(), { preconnect: undefined })

const emit = defineEmits<{
(event: 'load', payload: Event): unknown
Expand Down Expand Up @@ -119,6 +121,12 @@ const sources = computed<Source[]>(() => {
})
})

if (import.meta.server && (props.preconnect ?? $img.options.preconnect)) {
// `imgAttrs` is merged into `attrs.img` after the prop, so use that same value
const crossorigin = attrs.value.img.crossorigin as PreconnectOptions['crossorigin']
addPreconnect(sources.value.map(source => source.src), { crossorigin })
}

if (import.meta.server && props.preload) {
useHead({ link: () => {
const firstSource = sources.value[0]
Expand Down
Loading
Loading