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 supabase provider #686

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/content/5.providers/supabase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Supabase

Optimize images with Supabase's storage transformation service

Supabase offers storage image transformation for all JPEG, PNG, and GIF files you have set to be tracked with [Supabase Storage](https://supabase.com/docs/guides/storage/image-transformations/).


## Modifiers

In addition to `height` and `width`, the Supabase provider supports the following modifiers:

### `fit`

* **Default**: `cover`
* **Valid options**: `cover` (equivalent to `resize=cover`), `contain` (equivalent to `resize=contain`) and `fill` (equivalent to `resize=fill`)


## Limits

- Width and height must be an integer value between 1-2500.
- The image size cannot exceed 25MB.
- The image resolution cannot exceed 50MP.
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export default defineNuxtConfig({
netlify: {
baseURL: 'https://netlify-photo-gallery.netlify.app'
},
supabase: {
baseURL: 'https://demo.supabase.co/storage/v1/render/image/public/bucket/'
},
layer0: {},
prismic: {},
sanity: {
Expand Down
17 changes: 17 additions & 0 deletions playground/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,23 @@ export const providers: Provider[] = [
}
]
},
// Supabase
{
name: 'supabase',
samples: [
{
src: '/images/apple.jpg',
width: 101,
fit: 'contain'
},
{
src: '/images/apple.jpg',
width: 200,
height: 200,
fit: 'fill'
}
]
},
// Layer0
{
name: 'layer0',
Expand Down
1 change: 1 addition & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const BuiltInProviders = [
'imgix',
'ipx',
'netlify',
'supabase',
'layer0',
'prismic',
'sanity',
Expand Down
48 changes: 48 additions & 0 deletions src/runtime/providers/supabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { joinURL, encodeQueryItem } from 'ufo'
import type { ProviderGetImage } from '../../types'
import { createOperationsGenerator } from '#image'

const operationsGenerator = createOperationsGenerator({
keyMap: {
width: 'width',
height: 'height',
fit: 'resize'
},
valueMap: {
fit: {
cover: 'cover',
contain: 'contain',
fill: 'fill'
}
},
joinWith: ',',
formatter: (key, val) => encodeQueryItem(key, val)
})

const defaultModifiers = {}

const isDev = process.env.NODE_ENV === 'development'

// https://supabase.com/blog/storage-image-resizing-smart-cdn
export const getImage: ProviderGetImage = (src, {
modifiers = {},
baseURL = '/'
} = {}) => {
if (modifiers.format) {
// Not currently supported
if (isDev) {
// eslint-disable-next-line
console.warn(`Delete format is not supported in this provider. Warning originated from \`${src}\`.`)
}
delete modifiers.format
}
const mergeModifiers = { ...defaultModifiers, ...modifiers }
const operations = operationsGenerator(mergeModifiers as any)
// GET https://project_id.supabase.co/storage/v1/render/image/public/bucket/image.jpg?width=500&height=600
// https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE>
const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : joinURL(baseURL, src)

return {
url
}
}
1 change: 1 addition & 0 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface ImageProviders {
twicpics?: any
storyblok?: any,
strapi?: any,
supabase?: any,
imageengine?: any,
ipx?: Partial<IPXOptions>
static?: Partial<IPXOptions>
Expand Down