From 97f96d647bedb13ad47de1afd2547ca25e819443 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Fri, 15 Mar 2024 12:19:50 +0000 Subject: [PATCH] feat: support domains option with Netlify Image CDN --- docs/content/3.providers/netlify.md | 14 ++++++++------ src/provider.ts | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/content/3.providers/netlify.md b/docs/content/3.providers/netlify.md index bbfe07419..b22b29336 100644 --- a/docs/content/3.providers/netlify.md +++ b/docs/content/3.providers/netlify.md @@ -28,15 +28,17 @@ To test image transformations locally, use [Netlify Dev](https://docs.netlify.co ## Remote images -To transform a source image hosted on another domain, you must first configure allowed domains in your `netlify.toml` file. +To transform a source image hosted on another domain, you must first configure allowed domains: -```toml [netlify.toml] -[images] - remote_images = ["https://my-images.com/.*", "https://animals.more-images.com/[bcr]at/.*"] +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + image: { + provider: 'netlify', + domains: ['images.example.com'] + } +}) ``` -The `remote_images` property accepts an array of regex. If your images are in specific subdomains or directories, you can use regex to allow just those subdomains or directories. - ## Modifiers Beyond the [standard properties](https://image.nuxt.com/usage/nuxt-img), you can use the [Netlify Image CDN `position` parameter](https://docs.netlify.com/image-cdn/overview/#position) as a modifier for Nuxt Image. diff --git a/src/provider.ts b/src/provider.ts index 74ac4f65d..c0fcb344a 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -88,6 +88,18 @@ export const providerSetup: Partial> = } } }) + }, + // https://docs.netlify.com/image-cdn/create-integration/ + netlify (_providerOptions, moduleOptions, nuxt: Nuxt) { + if (moduleOptions.domains?.length > 0) { + nuxt.options.nitro = defu(nuxt.options.nitro, { + netlify: { + images: { + remote_images: moduleOptions.domains.map(domain => `https?:\\/\\/${domain.replaceAll('.', '\\.')}\\/.*`) + } + } + }) + } } }