Skip to content

Commit a7621d0

Browse files
authored
populate lastModified on update (#1290)
1 parent 90865d8 commit a7621d0

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/client/stdlib/fileAttachment.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ async function remote_fetch(file) {
2323

2424
export class AbstractFile {
2525
constructor(name, mimeType = "application/octet-stream", lastModified) {
26-
Object.defineProperty(this, "name", {value: `${name}`, enumerable: true});
27-
Object.defineProperty(this, "mimeType", {value: `${mimeType}`, enumerable: true});
28-
if (lastModified !== undefined) Object.defineProperty(this, "lastModified", {value: Number(lastModified), enumerable: true}); // prettier-ignore
26+
Object.defineProperties(this, {
27+
name: {value: `${name}`, enumerable: true},
28+
mimeType: {value: `${mimeType}`, enumerable: true},
29+
lastModified: {value: +lastModified, enumerable: true}
30+
});
2931
}
3032
async blob() {
3133
return (await remote_fetch(this)).blob();

src/preview.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {WebSocket} from "ws";
1515
import {WebSocketServer} from "ws";
1616
import type {Config} from "./config.js";
1717
import {readConfig} from "./config.js";
18+
import type {LoaderResolver} from "./dataloader.js";
1819
import {HttpError, isEnoent, isHttpError, isSystemError} from "./error.js";
1920
import {getClientPath} from "./files.js";
2021
import type {FileWatchers} from "./fileWatchers.js";
@@ -23,7 +24,7 @@ import {transpileJavaScript, transpileModule} from "./javascript/transpile.js";
2324
import {parseMarkdown} from "./markdown.js";
2425
import type {MarkdownCode, MarkdownPage} from "./markdown.js";
2526
import {populateNpmCache} from "./npm.js";
26-
import {isPathImport} from "./path.js";
27+
import {isPathImport, resolvePath} from "./path.js";
2728
import {renderPage} from "./render.js";
2829
import type {Resolvers} from "./resolvers.js";
2930
import {getResolvers} from "./resolvers.js";
@@ -346,7 +347,7 @@ function handleWatch(socket: WebSocket, req: IncomingMessage, configPromise: Pro
346347
type: "update",
347348
html: diffHtml(previousHtml, html),
348349
code: diffCode(previousCode, code),
349-
files: diffFiles(previousFiles, files),
350+
files: diffFiles(previousFiles, files, getLastModifiedResolver(loaders, path)),
350351
tables: diffTables(previousTables, tables, previousFiles, files),
351352
stylesheets: diffStylesheets(previousStylesheets, stylesheets),
352353
hash: {previous: previousHash, current: hash}
@@ -467,10 +468,14 @@ function diffCode(oldCode: Map<string, string>, newCode: Map<string, string>): C
467468
return patch;
468469
}
469470

470-
type FileDeclaration = {name: string; mimeType?: string; path: string};
471+
type FileDeclaration = {name: string; mimeType: string; lastModified: number; path: string};
471472
type FilePatch = {removed: string[]; added: FileDeclaration[]};
472473

473-
function diffFiles(oldFiles: Map<string, string>, newFiles: Map<string, string>): FilePatch {
474+
function diffFiles(
475+
oldFiles: Map<string, string>,
476+
newFiles: Map<string, string>,
477+
getLastModified: (name: string) => number | undefined
478+
): FilePatch {
474479
const patch: FilePatch = {removed: [], added: []};
475480
for (const [name, path] of oldFiles) {
476481
if (newFiles.get(name) !== path) {
@@ -479,12 +484,21 @@ function diffFiles(oldFiles: Map<string, string>, newFiles: Map<string, string>)
479484
}
480485
for (const [name, path] of newFiles) {
481486
if (oldFiles.get(name) !== path) {
482-
patch.added.push({name, mimeType: mime.getType(name) ?? undefined, path});
487+
patch.added.push({
488+
name,
489+
mimeType: mime.getType(name) ?? "application/octet-stream",
490+
lastModified: getLastModified(name) ?? NaN,
491+
path
492+
});
483493
}
484494
}
485495
return patch;
486496
}
487497

498+
function getLastModifiedResolver(loaders: LoaderResolver, path: string): (name: string) => number | undefined {
499+
return (name) => loaders.getSourceLastModified(resolvePath(path, name));
500+
}
501+
488502
type TableDeclaration = {name: string; path: string};
489503
type TablePatch = {removed: string[]; added: TableDeclaration[]};
490504

0 commit comments

Comments
 (0)