forked from nuxt/image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cloudflare provider (nuxt#359)
Co-authored-by: pooya parsa <[email protected]>
- Loading branch information
1 parent
ad73348
commit 59ac32c
Showing
8 changed files
with
121 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,38 @@ | ||
--- | ||
title: Cloudflare Provider | ||
description: 'Nuxt Image has first class integration with Cloudflare' | ||
navigation: | ||
title: Cloudflare | ||
--- | ||
|
||
Integration between [Cloudflare](https://developers.cloudflare.com/images/) and the image module. | ||
|
||
:::alert{type="info"} | ||
Availability only for Business and Enterprise Customers. Normally Cloudflare [pages](https://pages.cloudflare.com/) users can already benefit from build-time image optimization. | ||
::: | ||
|
||
To use this provider you just need to specify the base url (zone) of your service: | ||
|
||
```js{}[nuxt.config.js] | ||
export default { | ||
image: { | ||
baseURL: 'https://that-test.site' | ||
} | ||
} | ||
``` | ||
|
||
**Example:** | ||
|
||
```vue | ||
<nuxt-img provider="cloudflare" src="/burger.jpeg" height="300" :modifiers="{ fit: 'contain' }" /> | ||
``` | ||
|
||
## Options | ||
|
||
### `baseURL` | ||
|
||
Default: `/` | ||
|
||
Your deployment's domain (zone). | ||
|
||
**Note:** `/cdn-cgi/image/` will be automatically appended for generating URLs. |
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
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,49 @@ | ||
import { joinURL, encodeQueryItem } from 'ufo' | ||
import { ProviderGetImage } from 'src' | ||
import { createOperationsGenerator } from '~image' | ||
|
||
const operationsGenerator = createOperationsGenerator({ | ||
keyMap: { | ||
width: 'w', | ||
height: 'h', | ||
dpr: 'dpr', | ||
fit: 'fit', | ||
gravity: 'g', | ||
quality: 'q', | ||
format: 'f', | ||
sharpen: 'sharpen' | ||
}, | ||
valueMap: { | ||
fit: { | ||
cover: 'cover', | ||
contain: 'contain', | ||
fill: 'scale-down', | ||
outside: 'crop', | ||
inside: 'pad' | ||
}, | ||
gravity: { | ||
auto: 'auto', | ||
side: 'side' | ||
} | ||
}, | ||
joinWith: ',', | ||
formatter: (key, val) => encodeQueryItem(key, val) | ||
}) | ||
|
||
const defaultModifiers = {} | ||
|
||
// https://developers.cloudflare.com/images/image-resizing/url-format/ | ||
export const getImage: ProviderGetImage = (src, { | ||
modifiers = {}, | ||
baseURL = '/' | ||
} = {}) => { | ||
const mergeModifiers = { ...defaultModifiers, ...modifiers } | ||
const operations = operationsGenerator(mergeModifiers as any) | ||
|
||
// https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE> | ||
const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : joinURL(baseURL, src) | ||
|
||
return { | ||
url | ||
} | ||
} |
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