Skip to content

Commit

Permalink
fix(vercel): fix vercel auto-detection and provide local vercel mode (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored May 3, 2021
1 parent 24fe6bf commit 70fc2ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function detectProvider (userInput?: string) {
return userInput
}

if (process.env.VERCEL) {
if (process.env.VERCEL || process.env.VERCEL_ENV || process.env.NOW_BUILDER) {
return 'vercel'
}

Expand Down
26 changes: 24 additions & 2 deletions src/runtime/providers/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@ import { stringifyQuery } from 'ufo'

// https://vercel.com/docs/more/adding-your-framework#images

export const getImage: ProviderGetImage = (src, { modifiers, baseURL = '/_vercel/image' } = {}) => {
export const getImage: ProviderGetImage = (src, { modifiers, baseURL = '/_vercel/image' } = {}, ctx) => {
const validWidths = Object.values(ctx.options.screens || {}).sort()
const largestWidth = validWidths[validWidths.length - 1]
let width = Number(modifiers?.width || 0)

if (!width) {
width = largestWidth
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line
console.warn(`A defined width should be provided to use the \`vercel\` provider. Defaulting to \`${largestWidth}\`. Warning originated from \`${src}\`.`)
}
} else if (!validWidths.includes(width)) {
width = validWidths.find(validWidth => validWidth > width) || largestWidth
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line
console.warn(`The width being used (\`${modifiers?.width}\`) should be added to \`image.screens\`. Defaulting to \`${width}\`. Warning originated from \`${src}\`.`)
}
}

if (process.env.NODE_ENV === 'development') {
return { url: src }
}

return {
url: baseURL + '?' + stringifyQuery({
url: src,
w: modifiers?.width ? String(modifiers.width) : undefined,
w: String(width),
q: String(modifiers?.quality || '100')
})
}
Expand Down

0 comments on commit 70fc2ee

Please sign in to comment.