Skip to content

Commit

Permalink
feat: configure domains with NUXT_IMAGE_DOMAINS (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottyzen committed Apr 22, 2024
1 parent 1f5894a commit 1a227d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs/content/1.get-started/3.providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ For example, when using `<NuxtImg src="/nuxt-icon.png" />`, it should be placed
For more information, you can learn more about the [public directory](https://nuxt.com/docs/guide/directory-structure/public).

::callout
Image stored in the `assets/` directory are **not** proccessed with Nuxt Image because those images are managed by webpack.
Image stored in the `assets/` directory are **not** processed with Nuxt Image because those images are managed by your bundler (such as Vite or webpack).
::

### Remote Images

Using default provider, you can also optimize external URLs. For this, you need to add them to [`domains`](/get-started/configuration#domains) option.

You can also add domains for remote images by setting the `NUXT_IMAGE_DOMAINS` environment variable to a comma-separated list of domains.

```bash
NUXT_IMAGE_DOMAINS="example.com,yourdomain.com"
```

### Environment Detection

You can set default provider using `NUXT_IMAGE_PROVIDER` environment variable.
Expand Down
7 changes: 6 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import process from 'node:process'

import { parseURL, withLeadingSlash } from 'ufo'
import { defineNuxtModule, addTemplate, addImports, createResolver, addComponent, addPlugin } from '@nuxt/kit'
import { resolve } from 'pathe'
Expand Down Expand Up @@ -57,8 +59,11 @@ export default defineNuxtModule<ModuleOptions>({
// fully resolve directory
options.dir = resolve(nuxt.options.srcDir, options.dir)

// Domains from environment variable
const domainsFromENV = process.env.NUXT_IMAGE_DOMAINS?.replace(/\s/g, '').split(',') || []

// Normalize domains to hostname
options.domains = options.domains
options.domains = [...new Set([...options.domains, ...domainsFromENV])]
.map(d => d && (parseURL(d.startsWith('http') ? d : ('http://' + d)).host))
.filter(Boolean) as string[]

Expand Down

0 comments on commit 1a227d2

Please sign in to comment.