Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sanity provider #164

Merged
merged 3 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity seems have a demo project zp7mbokg : https://www.sanity.io/docs/presenting-images

},
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)
}
}