Skip to content

Commit

Permalink
fix(vercel): added missing formats config & improved documentation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mathix420 authored Sep 26, 2024
1 parent ed3071e commit 543ddd6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
36 changes: 27 additions & 9 deletions docs/content/3.providers/vercel.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ When deploying your nuxt applications to [Vercel](https://vercel.com/) platform,

This provider will be enabled by default in vercel deployments.

::callout{icon="i-heroicons-exclamation-triangle" color="amber"}
Vercel requires you to explicitly list all the widths used in your app. [See example below.](#sizes)
::

## Domains

To use external URLs (images not in `public/` directory), hostnames should be whitelisted.
Expand All @@ -30,19 +34,33 @@ export default {

## Sizes

Specify any custom `width` property you use in `<NuxtImg>`, `<NuxtPicture>` and `$img`.
You need to specify **every custom width** used in `<NuxtImg>`, `<NuxtPicture>` or `$img` for vercel to resize them properly. [Source.](https://vercel.com/docs/build-output-api/v3/configuration#api)

If a width is not defined, image will fallback to the next bigger width.

::callout{icon="i-heroicons-light-bulb"}
Don't forget to also take into account [`densities`](/get-started/configuration#densities).
::

**Example:**

```ts [nuxt.config]
export default {
image: {
screens: {
icon: 40,
avatar: 24
::code-group

```html [index.vue]
<template>
<NuxtImg height="40" width="40" preset="cover" src="/nuxt-icon.png" />
</template>
```

```ts [nuxt.config]
export default {
image: {
screens: {
icon: 40,
icon2x: 80
}
}
}
}
```
```

::
3 changes: 2 additions & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const providerSetup: Partial<Record<ImageProviderName, ProviderSetup>> =
ipx: ipxSetup(),
ipxStatic: ipxSetup({ isStatic: true }),

// https://vercel.com/docs/more/adding-your-framework#images
// https://vercel.com/docs/build-output-api/v3/configuration#images
vercel(_providerOptions, moduleOptions, nuxt: Nuxt) {
nuxt.options.nitro = defu(nuxt.options.nitro, {
vercel: {
Expand All @@ -65,6 +65,7 @@ export const providerSetup: Partial<Record<ImageProviderName, ProviderSetup>> =
domains: moduleOptions.domains,
minimumCacheTTL: 60 * 5,
sizes: Array.from(new Set(Object.values(moduleOptions.screens || {}))),
formats: ['image/webp', 'image/avif'],
},
},
} satisfies NitroConfig['vercel'],
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/providers/vercel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { stringifyQuery } from 'ufo'
import type { ProviderGetImage } from '../../module'

// https://vercel.com/docs/more/adding-your-framework#images
// https://vercel.com/docs/build-output-api/v3/configuration#images

export const getImage: ProviderGetImage = (src, { modifiers, baseURL = '/_vercel/image' } = {}, ctx) => {
const validWidths = Object.values(ctx.options.screens || {}).sort((a, b) => a - b)
Expand Down

0 comments on commit 543ddd6

Please sign in to comment.