ReviewOS

stacks/stx

ssg: build cache passes empty deps array — partial/layout edits don't invalidate pages

#1717
Closed glennmichael123 opened this 23 days ago · 0 comments
23 days ago

Summary

buildCache.get() is called with a hardcoded empty [] for dependencies at packages/stx/src/ssg.ts:1023. The cache key is built from filePath + mtimeMs + deps[], so if the deps array is always empty, the hash only changes when the page file itself changes — not when partials, layouts, or other transitive includes change. A change to a footer partial included in 30 pages re-uses the stale cached output for all 30.

Reproducer

  1. bun stx build (SSG mode) a project with pages/index.stx that @include('partials/footer').
  2. Edit partials/footer.stx.
  3. bun stx build again.
  4. Observed: index.html keeps the old footer (cache hit).
  5. Expected: Cache is invalidated; new footer is rendered.

Root cause

packages/stx/src/ssg.ts:1023:

html = buildCache.get(url, route.filePath, [])
//                                          ^^ always empty

getHash at ssg.ts:280 iterates the dependencies but the call site always passes []:

getHash(filePath: string, dependencies: string[]): string {
  const stats = fs.statSync(filePath)
  let hashInput = `${filePath}:${stats.mtimeMs}`

  for (const dep of dependencies) {
    if (fs.existsSync(dep)) {
      const depStats = fs.statSync(dep)
      hashInput += `:${dep}:${depStats.mtimeMs}`
    }
  }
  // ...
}

The deps Set is populated by processDirectives (passed by reference) but never threaded back to the cache lookup.

Impact

High. Any project using SSG + cache + partials/layouts has stale output after partial edits. The workaround is to disable cache (cache: false) — losing the speedup the cache was meant to provide.

Suggested fix

Make renderPage return {html, dependencies} (or capture deps before the cache call) and pass the collected dependencies to buildCache.get/set:

const dependencies = new Set<string>()
// existing render path populates dependencies via processDirectives
const depArray = Array.from(dependencies)
html = buildCache.get(url, route.filePath, depArray)
if (!html) {
  html = await renderPage(/* ... */)
  buildCache.set(url, route.filePath, depArray, html)
}

Same fix applies to the set side at the corresponding location.

Workaround

Set cache: false in the SSG config, or rm -rf .stx/cache between builds.

Environment

  • stx main
  • Bun 1.3.13

Sign in to comment on this issue.