Skip to content

Commit

Permalink
feat: add sanity provider (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored May 12, 2021
1 parent 389a12a commit 6483c34
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/content/en/3.providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ There are plenty of image service providers. Nuxt image has a generic way to wor
- [`Fastly`](/providers/fastly)
- [`Imgix`](/providers/imgix)
- [`IPX`](/providers/ipx) (selfhosted)
- [`Sanity`](/providers/sanity)
- [`Twicpics`](/providers/twicpics)
- [`Storyblok`](/providers/storyblok)

Expand Down
22 changes: 22 additions & 0 deletions docs/content/en/4.providers/sanity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
menuTitle: Sanity
title: Sanity Provider
description: 'Nuxt Image has first class integration with Sanity'
category: Providers
---

Integration between [Sanity](https://www.sanity.io/docs/image-urls) and the image module.

To use this provider you just need to specify the `projectId` of your project in Sanity.

```js{}[nuxt.config.js]
export default {
image: {
sanity: {
projectId: 'yourprojectid',
// Defaults to 'production'
// dataset: 'development'
}
}
}
```
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export default <NuxtConfig> {
imagekit: {
baseURL: 'https://ik.imagekit.io/demo'
},
sanity: {
projectId: 'j1o4tmjp'
},
vercel: {
baseURL: 'https://image-component.nextjs.gallery/_next/image'
},
Expand Down
11 changes: 11 additions & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
:operations="{ roundCorner: 'max' }"
/>

<h2>JPEG image on Sanity</h2>
<NuxtImg provider="sanity" src="image-7aa06723bb01a7a79055b6d6f5be80329a0e5b58-780x1170-jpg" />
<NuxtImg provider="sanity" src="image-7aa06723bb01a7a79055b6d6f5be80329a0e5b58-780x1170-jpg" width="200" height="200" fit="crop" />
<NuxtImg
provider="sanity"
src="image-7aa06723bb01a7a79055b6d6f5be80329a0e5b58-780x1170-jpg"
width="200"
height="200"
fit="min"
/>

<h2>JPEG image on TwicPics</h2>
<NuxtImg provider="twicpics" src="/football.jpg" />
<NuxtImg
Expand Down
1 change: 1 addition & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const BuiltInProviders = [
'imagekit',
'imgix',
'ipx',
'sanity',
'static',
'twicpics',
'storyblok',
Expand Down
29 changes: 29 additions & 0 deletions src/runtime/providers/sanity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ProviderGetImage } from 'src'
import { joinURL } from 'ufo'
import { createOperationsGenerator } from '~image'

const sanityCDN = 'https://cdn.sanity.io/images'

const operationsGenerator = createOperationsGenerator({
keyMap: {
background: 'b',
height: 'h',
quality: 'q',
width: 'w'
},
joinWith: '&',
formatter: (key, value) => `${key}=${value}`
})

export const getImage: ProviderGetImage = (src, { modifiers = {}, projectId, dataset = 'production' } = {}) => {
const operations = operationsGenerator(modifiers)

const parts = src.split('-').slice(1)
const format = parts.pop()

const filenameAndQueries = parts.join('-') + '.' + format + (operations ? ('?' + operations) : '')

return {
url: joinURL(sanityCDN, projectId, dataset, filenameAndQueries)
}
}

0 comments on commit 6483c34

Please sign in to comment.