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: support Netlify Image CDN #1234

Merged
merged 21 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ sw.*
.idea
.vercel
.output

# Local Netlify folder
.netlify
2 changes: 1 addition & 1 deletion docs/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ useSeoMeta({
const source = ref('npm i @nuxt/image')
const { copy, copied } = useClipboard({ source })

const providers = ['caisy', 'bunny', 'cloudflare', 'cloudimage', 'cloudinary', 'directus', 'edgio', 'fastly', 'glide', 'gumlet', 'hygraph', 'imageengine', 'imagekit', 'imgix', 'ipx', 'netlify', 'prepr', 'prismic', 'sanity', 'storyblok', 'strapi', 'twicpics', 'unsplash', 'uploadcare', 'vercel', 'weserv']
const providers = ['caisy', 'bunny', 'cloudflare', 'cloudimage', 'cloudinary', 'directus', 'edgio', 'fastly', 'glide', 'gumlet', 'hygraph', 'imageengine', 'imagekit', 'imgix', 'ipx', 'netlify', 'netlifyImageCdn', 'netlifyLargeMedia', 'prepr', 'prismic', 'sanity', 'storyblok', 'strapi', 'twicpics', 'unsplash', 'uploadcare', 'vercel', 'weserv']
// Disabling because svg to png does not work now with SSG
// Related issue: https://github.com/unjs/ipx/issues/160
// const img = useImage()
Expand Down
3 changes: 2 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export default defineNuxtConfig({
imagekit: {
baseURL: 'https://ik.imagekit.io/demo'
},
netlify: {
netlify: {},
netlifyLargeMedia: {
baseURL: 'https://netlify-photo-gallery.netlify.app'
},
layer0: {
Expand Down
23 changes: 19 additions & 4 deletions playground/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,32 @@ export const providers: Provider[] = [
}
]
},
// Netlify
{
name: 'netlify',
name: 'netlifyImageCdn',
samples: [
{
src: '/images/apple.jpg',
src: '/images/colors.jpg',
width: 100,
height: 100,
fit: 'cover'
},
{
src: '/images/colors.jpg',
width: 400,
height: 300
}
]
},
{
name: 'netlifyLargeMedia',
samples: [
{
src: '/images/colors.jpg',
width: 101,
fit: 'contain'
},
{
src: '/images/apple.jpg',
src: '/images/colors.jpg',
width: 200,
height: 200,
fit: 'fill'
Expand Down
15 changes: 14 additions & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const BuiltInProviders = [
'ipxStatic',
'layer0',
'netlify',
'netlifyLargeMedia',
'netlifyImageCdn',
'prepr',
'none',
'prismic',
Expand Down Expand Up @@ -118,6 +120,10 @@ export async function resolveProvider (_nuxt: any, key: string, input: InputProv
input.provider = input.name
}

if (input.provider in normalizableProviders) {
input.provider = normalizableProviders[input.provider]!()
}

const resolver = createResolver(import.meta.url)
input.provider = BuiltInProviders.includes(input.provider as ImageProviderName)
? await resolver.resolve('./runtime/providers/' + input.provider)
Expand All @@ -136,7 +142,14 @@ export async function resolveProvider (_nuxt: any, key: string, input: InputProv

const autodetectableProviders: Partial<Record<ProviderName, ImageProviderName>> = {
vercel: 'vercel',
aws_amplify: 'awsAmplify'
aws_amplify: 'awsAmplify',
netlify: 'netlify'
Copy link
Member

Choose a reason for hiding this comment

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

Adding a comment that we may change the logic for these providers in a subsequent PR. I'm discussing with @pi0 whether it's a breaking change to enable runtime support when users are performing fully static builds (e.g. nuxt generate).

context: #1224

}

const normalizableProviders: Partial<Record<string, () => ImageProviderName>> = {
netlify: () => {
return process.env.NETLIFY_LFS_ORIGIN_URL ? 'netlifyLargeMedia' : 'netlifyImageCdn'
}
}
Comment on lines +149 to 153
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added this as a new way for providers to resolve at build time. It alows an alias or detected platform to provide logic to resolve to one or other defined provider types.

Copy link
Member

Choose a reason for hiding this comment

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

Seems good to me, and it's internal so we can always refactor in future.


export function detectProvider (userInput: string = '') {
Expand Down
52 changes: 52 additions & 0 deletions src/runtime/providers/netlifyImageCdn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { ProviderGetImage } from '../../types'
import { createOperationsGenerator } from '#image'

// https://docs.netlify.com/image-cdn/overview/
export const operationsGenerator = createOperationsGenerator({
keyMap: {
width: 'w',
height: 'h',
format: 'fm',
quality: 'q',
position: 'position',
fit: 'fit'
},
valueMap: {
fit: {
fill: 'fill',
cover: 'cover',
contain: 'contain'
},
format: {
avif: 'avif',
gif: 'gif',
jpg: 'jpg',
png: 'png',
webp: 'webp'
},
position: {
top: 'top',
right: 'right',
bottom: 'bottom',
left: 'left',
center: 'center'
}
},
joinWith: '&',
formatter: (key: string, value: string) => `${key}=${value ?? ''}`
})

export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL } = {}) => {
const mods: Record<string, string> = { ...modifiers }
mods.url = src
if (modifiers.width) {
mods.width = modifiers.width.toString()
}
if (modifiers.height) {
mods.height = modifiers.height.toString()
}
const operations = operationsGenerator(mods)
return {
url: `${baseURL || '/.netlify/images'}?${operations}`
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const operationsGenerator = createOperationsGenerator({
}
},
joinWith: '&',
formatter: (key, value) => `${key}=${value}`
formatter: (key: string, value: string) => `${key}=${value}`
})

const isDev = process.env.NODE_ENV === 'development'
Expand Down
24 changes: 18 additions & 6 deletions test/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const images = [
imageengine: { url: '/test.png' },
unsplash: { url: '/test.png' },
imagekit: { url: '/test.png' },
netlify: { url: '/test.png' },
netlify: { url: '/.netlify/images?url=/test.png' },
netlifyImageCdn: { url: '/.netlify/images?url=/test.png' },
netlifyLargeMedia: { url: '/test.png' },
prepr: { url: 'https://projectName.stream.prepr.io/image-test-300x450-png' },
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=100&h=100' },
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?auto=format' },
Expand Down Expand Up @@ -49,7 +51,9 @@ export const images = [
imageengine: { url: '/test.png?imgeng=/w_200' },
unsplash: { url: '/test.png?w=200' },
imagekit: { url: '/test.png?tr=w-200' },
netlify: { url: '/test.png?w=200&nf_resize=fit' },
netlify: { url: '/.netlify/images?w=200&url=/test.png' },
netlifyImageCdn: { url: '/.netlify/images?w=200&url=/test.png' },
netlifyLargeMedia: { url: '/test.png?w=200&nf_resize=fit' },
prepr: { url: 'https://projectName.stream.prepr.io/w_200/image-test-300x450-png' },
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=100' },
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&auto=format' },
Expand Down Expand Up @@ -83,7 +87,9 @@ export const images = [
imageengine: { url: '/test.png?imgeng=/h_200' },
unsplash: { url: '/test.png?h=200' },
imagekit: { url: '/test.png?tr=h-200' },
netlify: { url: '/test.png?h=200&nf_resize=fit' },
netlify: { url: '/.netlify/images?h=200&url=/test.png' },
netlifyImageCdn: { url: '/.netlify/images?h=200&url=/test.png' },
netlifyLargeMedia: { url: '/test.png?h=200&nf_resize=fit' },
prepr: { url: 'https://projectName.stream.prepr.io/h_200/image-test-300x450-png' },
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=100&h=200' },
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?h=200&auto=format' },
Expand Down Expand Up @@ -117,7 +123,9 @@ export const images = [
imageengine: { url: '/test.png?imgeng=/w_200/h_200' },
unsplash: { url: '/test.png?w=200&h=200' },
imagekit: { url: '/test.png?tr=w-200,h-200' },
netlify: { url: '/test.png?w=200&h=200&nf_resize=fit' },
netlify: { url: '/.netlify/images?w=200&h=200&url=/test.png' },
netlifyImageCdn: { url: '/.netlify/images?w=200&h=200&url=/test.png' },
netlifyLargeMedia: { url: '/test.png?w=200&h=200&nf_resize=fit' },
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200' },
prepr: { url: 'https://projectName.stream.prepr.io/w_200,h_200/image-test-300x450-png' },
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&h=200&auto=format' },
Expand Down Expand Up @@ -151,7 +159,9 @@ export const images = [
imageengine: { url: '/test.png?imgeng=/w_200/h_200/m_letterbox' },
unsplash: { url: '/test.png?w=200&h=200&fit=fill' },
imagekit: { url: '/test.png?tr=w-200,h-200,cm-pad_resize' },
netlify: { url: '/test.png?w=200&h=200&nf_resize=fit' },
netlify: { url: '/.netlify/images?w=200&h=200&fit=contain&url=/test.png' },
netlifyImageCdn: { url: '/.netlify/images?w=200&h=200&fit=contain&url=/test.png' },
netlifyLargeMedia: { url: '/test.png?w=200&h=200&nf_resize=fit' },
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200&fit=fill' },
prepr: { url: 'https://projectName.stream.prepr.io/w_200,h_200,fit_contain/image-test-300x450-png' },
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&h=200&fit=fill&auto=format&bg=ffffff' },
Expand Down Expand Up @@ -185,7 +195,9 @@ export const images = [
imageengine: { url: '/test.png?imgeng=/w_200/h_200/m_letterbox/f_jpg' },
unsplash: { url: '/test.png?w=200&h=200&fit=fill&fm=jpeg' },
imagekit: { url: '/test.png?tr=w-200,h-200,cm-pad_resize,f-jpeg' },
netlify: { url: '/test.png?w=200&h=200&nf_resize=fit' },
netlify: { url: '/.netlify/images?w=200&h=200&fit=contain&fm=jpeg&url=/test.png' },
netlifyImageCdn: { url: '/.netlify/images?w=200&h=200&fit=contain&fm=jpeg&url=/test.png' },
netlifyLargeMedia: { url: '/test.png?w=200&h=200&nf_resize=fit' },
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200&fit=fill&fm=jpeg' },
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&h=200&fit=fill&fm=jpg&bg=ffffff' },
prepr: { url: 'https://projectName.stream.prepr.io/w_200,h_200,fit_contain,format_jpg/image-test-300x450-png' },
Expand Down
Loading