-
Notifications
You must be signed in to change notification settings - Fork 287
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
Changes from 4 commits
1687024
405c451
46b1225
186d67d
8b3bbd0
5f67712
d5868a0
0bf0a70
546a86f
c71378e
573e42e
ce84424
8a61fe6
d222e5c
21f1860
c59a9d3
10006eb
10a26f9
f41ce38
6a7becd
a5d9212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ sw.* | |
.idea | ||
.vercel | ||
.output | ||
|
||
# Local Netlify folder | ||
.netlify |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ const BuiltInProviders = [ | |
'ipxStatic', | ||
'layer0', | ||
'netlify', | ||
'netlifyLargeMedia', | ||
'netlifyImageCdn', | ||
'prepr', | ||
'none', | ||
'prismic', | ||
|
@@ -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) | ||
|
@@ -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' | ||
} | ||
|
||
const normalizableProviders: Partial<Record<string, () => ImageProviderName>> = { | ||
netlify: () => { | ||
return process.env.NETLIFY_LFS_ORIGIN_URL ? 'netlifyLargeMedia' : 'netlifyImageCdn' | ||
} | ||
} | ||
Comment on lines
+149
to
153
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = '') { | ||
|
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}` | ||
} | ||
} |
There was a problem hiding this comment.
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