Skip to content

Commit

Permalink
fix: speed up build by ensuring stable cache (sveltejs#569)
Browse files Browse the repository at this point in the history
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 sveltejs#307
  • Loading branch information
dummdidumm authored Oct 23, 2024
1 parent 61b4c0d commit 5d5429a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import MagicString from 'magic-string';
import { createHash, Hash } from 'node:crypto';
import fs from 'node:fs';
import process from 'node:process';
import path from 'node:path';
import ts from 'typescript';
import * as marked from 'marked';
Expand Down Expand Up @@ -28,9 +29,21 @@ const theme = createCssVariablesTheme({
fontStyle: true
});

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

/**
Expand Down

0 comments on commit 5d5429a

Please sign in to comment.