diff --git a/package.json b/package.json index ec91ef35..0bd93503 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "@easyops-cn/docusaurus-search-local", "version": "0.9.3", + "description": "An offline/local search plugin for Docusaurus v2.", "repository": "https://github.com/easyops-cn/docusaurus-search-local", "homepage": "https://github.com/easyops-cn/docusaurus-search-local", "scripts": { diff --git a/src/server/utils/getIndexHash.spec.ts b/src/server/utils/getIndexHash.spec.ts index 6f1273b5..ee4478fe 100644 --- a/src/server/utils/getIndexHash.spec.ts +++ b/src/server/utils/getIndexHash.spec.ts @@ -9,6 +9,16 @@ const mockConsoleWarn = jest .spyOn(console, "warn") .mockImplementation(() => void 0); +jest.mock( + "../../../../package.json", + () => ({ + version: "0.0.0", + }), + { + virtual: true, + } +); + (klawSync as jest.MockedFunction).mockImplementation( (root, options) => { let files: string[] = []; @@ -47,7 +57,7 @@ const mockConsoleWarn = jest describe("getIndexHash", () => { test.each<[Partial, string | null, number]>([ [{ hashed: false }, null, 0], - [{ hashed: true, indexDocs: true, docsDir: "/tmp/docs" }, "a387bd69", 0], + [{ hashed: true, indexDocs: true, docsDir: "/tmp/docs" }, "87def35c", 0], [{ hashed: true, indexBlog: true, blogDir: "/tmp/blog" }, null, 0], [ { hashed: true, indexDocs: true, docsDir: "/does-not-exist/docs" }, diff --git a/src/server/utils/getIndexHash.ts b/src/server/utils/getIndexHash.ts index 26d9d88b..4a1b5340 100644 --- a/src/server/utils/getIndexHash.ts +++ b/src/server/utils/getIndexHash.ts @@ -1,7 +1,9 @@ import fs from "fs"; +import path from "path"; import crypto from "crypto"; import klawSync from "klaw-sync"; import { ProcessedPluginOptions } from "../../shared/interfaces"; +import { debugInfo } from "./debug"; export function getIndexHash(config: ProcessedPluginOptions): string | null { if (!config.hashed) { @@ -35,10 +37,25 @@ export function getIndexHash(config: ProcessedPluginOptions): string | null { if (files.length > 0) { const md5sum = crypto.createHash("md5"); + + // The version of this plugin should be counted in hash, + // since the index maybe changed between versions. + + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pluginVersion = require(path.resolve( + __dirname, + "../../../../package.json" + )).version; + debugInfo("using @easyops-cn/docusaurus-search-local v%s", pluginVersion); + md5sum.update(pluginVersion, "utf8"); + for (const item of files) { md5sum.update(fs.readFileSync(item.path)); } - return md5sum.digest("hex").substring(0, 8); + + const indexHash = md5sum.digest("hex").substring(0, 8); + debugInfo("the index hash is %j", indexHash); + return indexHash; } return null; }