Skip to content

Commit 5d5429a

Browse files
authored
fix: speed up build by ensuring stable cache (#569)
Our builds got slower because the cache hash was always different. This happened because the dependencies were calculated based on the generated code after Vite compiled it, i.e. it would check stuff in .svelte-kit/output/server/chunks/...js. This seems to have worked fine for a while, but now Vite mashes things up such that code from svelte.dev/src/lib/server/content.ts is mixed in, which has loads of Vite hashes in it, and so the file content is always different. That in turn means our hash is always different, and so the snippet cache never hits. The pragmatic solution is to hard-code the path to the original file and add a sanity check to ensure it's still accurate. related to #307
1 parent 61b4c0d commit 5d5429a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import MagicString from 'magic-string';
22
import { createHash, Hash } from 'node:crypto';
33
import fs from 'node:fs';
4+
import process from 'node:process';
45
import path from 'node:path';
56
import ts from 'typescript';
67
import * as marked from 'marked';
@@ -28,9 +29,21 @@ const theme = createCssVariablesTheme({
2829
fontStyle: true
2930
});
3031

32+
// Hash the contents of this file and its dependencies so that we get a new cache in case we have changed
33+
// how the markdown is rendered (whose logic live here). This is to avoid serving stale code snippets.
3134
const hash = createHash('sha256');
3235
hash.update(fs.readFileSync('../../pnpm-lock.yaml', 'utf-8'));
33-
hash_graph(hash, fileURLToPath(import.meta.url));
36+
// CAREFUL: update this URL in case you ever move this file or start the dev/build process from another directory
37+
const original_file = '../../packages/site-kit/src/lib/markdown/renderer.ts';
38+
if (!fs.existsSync(original_file)) {
39+
throw new Error(
40+
'Update the path to the markdown renderer code. Current value: ' +
41+
original_file +
42+
' | Current cwd: ' +
43+
process.cwd()
44+
);
45+
}
46+
hash_graph(hash, original_file);
3447
const digest = hash.digest().toString('base64').replace(/\//g, '-');
3548

3649
/**

0 commit comments

Comments
 (0)