Last active
May 16, 2022 20:49
-
-
Save leefroese/c71e853aeca957561d73cc2867595868 to your computer and use it in GitHub Desktop.
Nuxt/Image Shopify Provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
image: { | |
providers: { | |
shopify: { | |
provider: '~/providers/shopify', | |
options: {} | |
} | |
} | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@nuxt/image - custom Shopify provider | |
*/ | |
import { joinURL } from 'ufo' | |
export function getImage(src, { modifiers, baseURL } = {}, { options, nuxtContext, $img }) { | |
const { width, height, format, fit, ...providerModifiers } = modifiers | |
// base URL | |
const urlStart = src.substring(0, src.lastIndexOf("/") + 1); | |
const urlEnd = src.substring(src.lastIndexOf("/") + 1, src.length); | |
let fileName = urlEnd.split('.')[0] | |
// modify URL | |
if( 'width' in modifiers && 'height' in modifiers ) { | |
fileName = `${fileName}_${modifiers.width}x${modifiers.height}` | |
} | |
if( 'fit' in modifiers && modifiers.fit == 'crop' ) { | |
fileName = `${fileName}_crop_center` | |
} | |
if( 'format' in modifiers ) { | |
fileName = `${fileName}.${urlEnd.split('.')[1]}.${modifiers.format}` | |
} | |
// final URL | |
return { | |
url: joinURL(urlStart, fileName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment