Skip to content

Commit c31dbf3

Browse files
jackyzha0MasssiveJuice08
authored andcommitted
fix: unmemoize explorer on rebuild (closes #1077)
1 parent dd77451 commit c31dbf3

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

quartz/build.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ type BuildData = {
3838

3939
type FileEvent = "add" | "change" | "delete"
4040

41+
function newBuildId() {
42+
return new Date().toISOString()
43+
}
44+
4145
async function buildQuartz(argv: Argv, mut: Mutex, clientRefresh: () => void) {
4246
const ctx: BuildCtx = {
47+
buildId: newBuildId(),
4348
argv,
4449
cfg,
4550
allSlugs: [],
@@ -167,6 +172,7 @@ async function partialRebuildFromEntrypoint(
167172

168173
const perf = new PerfTimer()
169174
console.log(chalk.yellow("Detected change, rebuilding..."))
175+
ctx.buildId = newBuildId()
170176

171177
// UPDATE DEP GRAPH
172178
const fp = joinSegments(argv.directory, toPosixPath(filepath)) as FilePath
@@ -363,14 +369,10 @@ async function rebuildFromEntrypoint(
363369

364370
const perf = new PerfTimer()
365371
console.log(chalk.yellow("Detected change, rebuilding..."))
372+
ctx.buildId = newBuildId()
373+
366374
try {
367375
const filesToRebuild = [...toRebuild].filter((fp) => !toRemove.has(fp))
368-
369-
const trackedSlugs = [...new Set([...contentMap.keys(), ...toRebuild, ...trackedAssets])]
370-
.filter((fp) => !toRemove.has(fp))
371-
.map((fp) => slugifyFilePath(path.posix.relative(argv.directory, fp) as FilePath))
372-
373-
ctx.allSlugs = [...new Set([...initialSlugs, ...trackedSlugs])]
374376
const parsedContent = await parseMarkdown(ctx, filesToRebuild)
375377
for (const content of parsedContent) {
376378
const [_tree, vfile] = content
@@ -384,6 +386,13 @@ async function rebuildFromEntrypoint(
384386
const parsedFiles = [...contentMap.values()]
385387
const filteredContent = filterContent(ctx, parsedFiles)
386388

389+
// re-update slugs
390+
const trackedSlugs = [...new Set([...contentMap.keys(), ...toRebuild, ...trackedAssets])]
391+
.filter((fp) => !toRemove.has(fp))
392+
.map((fp) => slugifyFilePath(path.posix.relative(argv.directory, fp) as FilePath))
393+
394+
ctx.allSlugs = [...new Set([...initialSlugs, ...trackedSlugs])]
395+
387396
// TODO: we can probably traverse the link graph to figure out what's safe to delete here
388397
// instead of just deleting everything
389398
await rimraf(path.join(argv.output, ".*"), { glob: true })

quartz/components/Explorer.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ export default ((userOpts?: Partial<Options>) => {
4444
// memoized
4545
let fileTree: FileNode
4646
let jsonTree: string
47+
let lastBuildId: string = ""
4748

4849
function constructFileTree(allFiles: QuartzPluginData[]) {
49-
if (fileTree) {
50-
return
51-
}
52-
5350
// Construct tree from allFiles
5451
fileTree = new FileNode("")
5552
allFiles.forEach((file) => fileTree.add(file))
@@ -76,12 +73,17 @@ export default ((userOpts?: Partial<Options>) => {
7673
}
7774

7875
const Explorer: QuartzComponent = ({
76+
ctx,
7977
cfg,
8078
allFiles,
8179
displayClass,
8280
fileData,
8381
}: QuartzComponentProps) => {
84-
constructFileTree(allFiles)
82+
if (ctx.buildId !== lastBuildId) {
83+
lastBuildId = ctx.buildId
84+
constructFileTree(allFiles)
85+
}
86+
8587
return (
8688
<div class={classNames(displayClass, "explorer")}>
8789
<button

quartz/util/ctx.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface Argv {
1414
}
1515

1616
export interface BuildCtx {
17+
buildId: string
1718
argv: Argv
1819
cfg: QuartzConfig
1920
allSlugs: FullSlug[]

0 commit comments

Comments
 (0)