-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LFS only
- Loading branch information
Showing
7 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: Netlify Provider | ||
description: Optimize images with Netlify's dynamic image transformation service. | ||
navigation: | ||
title: Netlify | ||
--- | ||
|
||
Netlify offers dynamic image transformation for all JPEG, PNG, and GIF files you have set to be tracked with [Netlify Large Media](https://docs.netlify.com/large-media/overview/). | ||
|
||
:::alert{type="warning"} | ||
Before setting `provider: 'netlify'`, make sure you have followed the steps to enable [Netlify Large Media](https://docs.netlify.com/large-media/overview/). | ||
::: | ||
|
||
## Modifiers | ||
|
||
In addition to `height` and `width`, the Netlify provider supports the following modifiers: | ||
|
||
### `fit` | ||
|
||
* **Default**: `contain` | ||
* **Valid options**: `contain` (equivalent to `nf_resize=fit`) and `fill` (equivalent to `nf_resize=smartcrop`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ const BuiltInProviders = [ | |
'imagekit', | ||
'imgix', | ||
'ipx', | ||
'netlify', | ||
'prismic', | ||
'sanity', | ||
'static', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { joinURL } from 'ufo' | ||
import type { ProviderGetImage } from 'src' | ||
import { createOperationsGenerator } from '~image' | ||
|
||
export const operationsGenerator = createOperationsGenerator({ | ||
keyMap: { | ||
height: 'h', | ||
fit: 'nf_resize', | ||
width: 'w' | ||
}, | ||
valueMap: { | ||
fit: { | ||
fill: 'smartcrop', | ||
contain: 'fit' | ||
} | ||
}, | ||
joinWith: '&', | ||
formatter: (key, value) => `${key}=${value}` | ||
}) | ||
|
||
const isDev = process.env.NODE_ENV === 'development' | ||
|
||
// https://docs.netlify.com/large-media/transform-images/ | ||
|
||
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/' } = {}) => { | ||
if (modifiers.format) { | ||
// Not currently supported | ||
delete modifiers.format | ||
} | ||
const hasTransformation = modifiers.height || modifiers.width | ||
if (!modifiers.fit && hasTransformation) { | ||
// fit is required for resizing images | ||
modifiers.fit = 'contain' | ||
} | ||
if (hasTransformation && modifiers.fit !== 'contain' && !(modifiers.height && modifiers.width)) { | ||
// smartcrop is only supported with both height and width | ||
if (isDev) { | ||
// eslint-disable-next-line | ||
console.warn(`Defaulting to fit=contain as smart cropping is only supported when providing both height and width. Warning originated from \`${src}\`.`) | ||
} | ||
modifiers.fit = 'contain' | ||
} | ||
if (isDev) { | ||
return { url: src } | ||
} | ||
const operations = operationsGenerator(modifiers) | ||
return { | ||
url: joinURL(baseURL, src + (operations ? ('?' + operations) : '')) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters