Skip to content

Commit

Permalink
fix: cache key should consider file path
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 8, 2022
1 parent bb41a9f commit c778d31
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"fast-glob": "^3.2.7",
"fs-extra": "^10.0.0",
"gray-matter": "^4.0.3",
"is-plain-object": "^5.0.0",
"lint-staged": "^11.0.0",
"lru-cache": "^6.0.0",
"markdown-it": "^12.3.2",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { deeplyParseHeader } from './utils/parseHeader'
import { getGitTimestamp } from './utils/getGitTimestamp'
import { createMarkdownRenderer, MarkdownOptions } from './markdown/markdown'
import _debug from 'debug'
import { isPlainObject } from 'is-plain-object'

const debug = _debug('vitepress:md')
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 })
Expand All @@ -21,6 +22,22 @@ export interface MarkdownCompileResult {
includes: string[]
}

/**
* copied from https://github.com/TanStack/query/blob/cca1bbfb9a/src/core/utils.ts
*/
export function hashObject(queryKey: any): string {
return JSON.stringify(queryKey, (_, val) =>
isPlainObject(val)
? Object.keys(val)
.sort()
.reduce((result, key) => {
result[key] = val[key]
return result
}, {} as any)
: val
)
}

export async function createMarkdownToVueRenderFn(
srcDir: string,
options: MarkdownOptions = {},
Expand All @@ -43,8 +60,9 @@ export async function createMarkdownToVueRenderFn(
): Promise<MarkdownCompileResult> => {
const relativePath = slash(path.relative(srcDir, file))
const dir = path.dirname(file)
const cacheKey = hashObject({ src, file, publicDir })

const cached = cache.get(src)
const cached = cache.get(cacheKey)
if (cached) {
debug(`[cache hit] ${relativePath}`)
return cached
Expand Down Expand Up @@ -148,7 +166,8 @@ export async function createMarkdownToVueRenderFn(
deadLinks,
includes
}
cache.set(src, result)

cache.set(cacheKey, result)
return result
}
}
Expand Down

0 comments on commit c778d31

Please sign in to comment.