Skip to content

Commit

Permalink
feat: preserve images name on generation
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Sep 21, 2020
1 parent d1f62df commit 3f789fc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ interface CreateImageOptions {
defaultProvider: string
}

function getExtension(url: string) {
return url.split(/[#?]/)[0].split('.').pop().trim();
function getFile(url: string) {
const [name, extension] = url.split(/[\?#]/).shift().split('/').pop().split('.');
return {
name, extension
}
}

function generateUnique(url) {
return hash.sha256().update(url).digest('hex')
return hash.sha256().update(url).digest('hex').substr(0, 6)
}

export function createImage(context, { providers, defaultProvider, presets }: CreateImageOptions) {
Expand Down Expand Up @@ -62,7 +65,8 @@ export function createImage(context, { providers, defaultProvider, presets }: Cr
)

if (!context.isDev && context.isStatic && isStatic) {
const staticUrl = '_image/' + generateUnique(url) + '.' + (modifiers.format || getExtension(src))
const { name, extension } = getFile(src);
const staticUrl = '_image/' + name + '-' + generateUnique(url) + '.' + (modifiers.format || extension)
if (process.server) {
context.ssrContext.staticImages = context.ssrContext.staticImages || []
context.ssrContext.staticImages.push({
Expand Down

0 comments on commit 3f789fc

Please sign in to comment.