From 8428987286f1a04ac3b89b9ccb9bd4db596a06b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Mon, 5 Feb 2024 22:58:06 +0100 Subject: [PATCH] append to the end --- example/hashes.json | 10 ++++++---- index.js | 4 +--- lib/hash.js | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/example/hashes.json b/example/hashes.json index 04806d4..95740eb 100644 --- a/example/hashes.json +++ b/example/hashes.json @@ -1,6 +1,8 @@ { - "index.js": "1f49271e60e53b00-", - "index.html": "8e27d2f424d541b4-", - "index.css": "712708f47c759d37-", - "images/sample.jpg": "4c67e0488b3afa81-" + "index.js": "1f49271e60e53b00", + "index.html": "8e27d2f424d541b4", + "index.css": "712708f47c759d37", + "images/sample.jpg": "4c67e0488b3afa81", + "test.html": "6944c8b16078a588", + "test.css": "b305efc33fe211f7" } \ No newline at end of file diff --git a/index.js b/index.js index 00b3a24..b04363f 100644 --- a/index.js +++ b/index.js @@ -430,10 +430,8 @@ async function fastifyStatic (fastify, opts) { } function getHashedStaticPath (unhashedRelativePath) { - const relativeDirectory = path.posix.dirname(unhashedRelativePath) - const fileName = path.basename(unhashedRelativePath) const hash = fastify[kFileHashes].get(unhashedRelativePath) - return `${prefix}${relativeDirectory === '.' ? '' : `${relativeDirectory}/`}${hash ?? ''}${fileName}` + return `${prefix}${unhashedRelativePath}${hash ? `.${hash}` : ''}` } } diff --git a/lib/hash.js b/lib/hash.js index c8ef4e9..1b2ba36 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -10,7 +10,7 @@ const fastq = require('fastq') async function generateFileHash (filePath) { try { const fileBuffer = await fs.readFile(filePath) - return `${crypto.createHash('md5').update(fileBuffer).digest('hex').slice(0, 16)}-` + return crypto.createHash('md5').update(fileBuffer).digest('hex').slice(0, 16) } catch { return '' }