From 3f789fc504fd88187424c6961bb384c4dbbdd9ec Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Mon, 21 Sep 2020 14:09:38 +0330 Subject: [PATCH] feat: preserve images name on generation --- src/runtime/image.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/runtime/image.ts b/src/runtime/image.ts index cfdeb99e0..8cf278ea5 100644 --- a/src/runtime/image.ts +++ b/src/runtime/image.ts @@ -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) { @@ -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({