Skip to content

fix: use commit time for cache #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: Introduction
---

test

## Before we begin

> If you're new to Svelte or SvelteKit we recommend checking out the [interactive tutorial](https://learn.svelte.dev).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Svelte is a web UI framework that uses a compiler to turn declarative component
<!--- file: App.svelte --->
<script lang="ts">
let count = $state(0);

// test
function increment() {
count += 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const redirects = new Map([
['event-modifiers', 'svelte/capturing'],
['dom-event-forwarding', 'svelte/spreading-events']
]);

//test
export async function load({ params }) {
if (!params.slug || params.slug === 'svelte') redirect(307, '/tutorial/svelte/welcome-to-svelte');
if (params.slug === 'kit') redirect(307, '/tutorial/kit/introducing-sveltekit');
Expand Down
35 changes: 21 additions & 14 deletions packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { transformerTwoslash } from '@shikijs/twoslash';
import { SHIKI_LANGUAGE_MAP, escape, normalizeSlugify, smart_quotes, transform } from './utils';
import type { Modules } from './index';
import { fileURLToPath } from 'node:url';
import { execSync } from 'node:child_process';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { execSync } from 'node:child_process';
import { execFileSync } from 'node:child_process';


interface SnippetOptions {
file: string | null;
Expand Down Expand Up @@ -174,7 +175,7 @@ export async function render_content_markdown(
}

if (options.copy) {
html += `<button class="copy-to-clipboard raised" title="Copy to clipboard" aria-label="Copy to clipboard"></button>`;
html += `<button class="copy-to-clipboard raised" title="Copyyyy to clipboard" aria-label="Copy to clipboard"></button>`;
}

html += '</div>';
Expand Down Expand Up @@ -510,15 +511,15 @@ function find_nearest_node_modules(file: string): string | null {
}

/**
* Get the `mtime` of the most recently modified file in a dependency graph,
* Get the commit time of the most recently modified file in a dependency graph,
* excluding imports from `node_modules`
*/
function get_mtime(file: string, seen = new Set<string>()) {
if (seen.has(file)) return -1;
function get_commit_time(file: string, seen = new Set<string>()) {
if (seen.has(file)) return 0;
seen.add(file);

let mtime = fs.statSync(file).mtimeMs;
const content = fs.readFileSync(file, 'utf-8');
let latest_commit_time = 0;

for (const [_, source] of content.matchAll(/^import(?:.+?\s+from\s+)?['"](.+)['"];?$/gm)) {
if (source[0] !== '.') continue;
Expand All @@ -528,16 +529,22 @@ function get_mtime(file: string, seen = new Set<string>()) {
if (!fs.existsSync(resolved))
throw new Error(`Could not resolve ${source} relative to ${file}`);

mtime = Math.max(mtime, get_mtime(resolved, seen));
const child_commit_time = get_commit_time(resolved, seen);
if (child_commit_time > latest_commit_time) {
latest_commit_time = child_commit_time;
}
}

return mtime;
const file_commit_time = parseInt(
execSync(`git log -n 1 --pretty=format:%ct -- ${file}`).toString().trim(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use execFileSync instead so we can pass an array of args and don't need to worry about escaping things for the shell.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Untested.)

Suggested change
execSync(`git log -n 1 --pretty=format:%ct -- ${file}`).toString().trim(),
execFileSync('git', ['log', '-n', '1', '--pretty=format:%ct', '--', file]).toString().trim(),

10
);
return latest_commit_time > file_commit_time ? latest_commit_time : file_commit_time;
}

const mtime = Math.max(
get_mtime(fileURLToPath(import.meta.url)),
fs.statSync('node_modules').mtimeMs,
fs.statSync('../../pnpm-lock.yaml').mtimeMs
const commit_time = Math.max(
get_commit_time(fileURLToPath(import.meta.url)),
get_commit_time('../../pnpm-lock.yaml')
);

/**
Expand All @@ -560,7 +567,7 @@ async function create_snippet_cache(should: boolean) {

if (fs.existsSync(directory)) {
for (const dir of fs.readdirSync(directory)) {
if (!fs.statSync(`${directory}/${dir}`).isDirectory() || +dir < mtime) {
if (!fs.statSync(`${directory}/${dir}`).isDirectory() || +dir < commit_time) {
fs.rmSync(`${directory}/${dir}`, { force: true, recursive: true });
}
}
Expand All @@ -569,15 +576,15 @@ async function create_snippet_cache(should: boolean) {
}

try {
fs.mkdirSync(`${directory}/${mtime}`);
fs.mkdirSync(`${directory}/${commit_time}`);
} catch {}

function get_file(source: string) {
const hash = createHash('sha256');
hash.update(source);
const digest = hash.digest().toString('base64').replace(/\//g, '-');

return `${directory}/${mtime}/${digest}.html`;
return `${directory}/${commit_time}/${digest}.html`;
}

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/site-kit/src/lib/markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const escapeReplacements = {
'"': '&quot;',
"'": '&#39;'
};

//test
/**
* @param {string} ch
*/
Expand Down
Loading