cache cmds/webapps by epoch in prod

This commit is contained in:
Chris Wanstrath 2025-10-08 13:44:16 -07:00
parent df700ffe91
commit 2b7a7eea1c
5 changed files with 24 additions and 10 deletions

View File

@ -1,5 +1,5 @@
//// ////
// version: c584db7 // version: df700ff
var __defProp = Object.defineProperty; var __defProp = Object.defineProperty;
var __export = (target, all) => { var __export = (target, all) => {
@ -83,8 +83,11 @@ __export(exports_scrollback, {
function randomId() { function randomId() {
return Math.random().toString(36).slice(7); return Math.random().toString(36).slice(7);
} }
async function importUrl(url) { async function importUrl(url, cacheTag) {
url += url.includes("?") ? "&" : "?"; url += url.includes("?") ? "&" : "?";
if (cacheTag)
url += "t=" + cacheTag;
else
url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now()); url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now());
console.log("-> import", url); console.log("-> import", url);
return import(url); return import(url);

View File

@ -6,7 +6,7 @@ import { watch, readFileSync } from "fs"
import { join, basename } from "path" import { join, basename } from "path"
import { isFile } from "./utils" import { isFile } from "./utils"
import { sendAll } from "./websocket" import { sendAll } from "./websocket"
import { expectDir } from "./utils" import { expectDir, mtimeEpoch } from "./utils"
import { importUrl } from "./shared/utils" import { importUrl } from "./shared/utils"
import type { Command, Commands } from "./shared/types" import type { Command, Commands } from "./shared/types"
import { projectBin, projectName } from "./project" import { projectBin, projectName } from "./project"
@ -82,6 +82,10 @@ export async function commandSource(name: string): Promise<string> {
export async function loadCommandModule(cmd: string) { export async function loadCommandModule(cmd: string) {
const path = commandPath(cmd) const path = commandPath(cmd)
if (!path) return if (!path) return
if (path.startsWith(NOSE_DIR))
return await importUrl(path, await mtimeEpoch(path))
else
return await importUrl(path) return await importUrl(path)
} }

View File

@ -40,9 +40,12 @@ export function unique<T>(array: T[]): T[] {
return [...new Set(array)] return [...new Set(array)]
} }
// import a typescript module. caches in production, doesn't in dev. // import a typescript module. caches by sha in production, doesn't cache in dev.
export async function importUrl(url: string) { export async function importUrl(url: string, cacheTag?: string) {
url += url.includes("?") ? "&" : "?" url += url.includes("?") ? "&" : "?"
if (cacheTag)
url += "t=" + cacheTag
else
url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now()) url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now())
console.log("-> import", url) console.log("-> import", url)

View File

@ -63,6 +63,10 @@ export async function mtime(path: string): Promise<Date> {
return mtime return mtime
} }
export async function mtimeEpoch(path: string): Promise<string> {
return (await mtime(path)).getTime().toString()
}
// is the given file binary? // is the given file binary?
export async function isBinaryFile(path: string): Promise<boolean> { export async function isBinaryFile(path: string): Promise<boolean> {
// Create a stream to read just the beginning // Create a stream to read just the beginning

View File

@ -8,7 +8,7 @@ import { readdirSync, watch } from "fs"
import { sendAll } from "./websocket" import { sendAll } from "./websocket"
import { expectDir } from "./utils" import { expectDir } from "./utils"
import { NOSE_DIR } from "./config" import { NOSE_DIR } from "./config"
import { isFile, isDir } from "./utils" import { isFile, isDir, mtimeEpoch } from "./utils"
import { importUrl } from "./shared/utils" import { importUrl } from "./shared/utils"
export type Handler = (r: Context) => string | Child | Response | Promise<Response> export type Handler = (r: Context) => string | Child | Response | Promise<Response>
@ -73,7 +73,7 @@ async function findApp(name: string): Promise<App | undefined> {
async function loadApp(path: string): Promise<App | undefined> { async function loadApp(path: string): Promise<App | undefined> {
if (!await Bun.file(path).exists()) return if (!await Bun.file(path).exists()) return
const mod = await importUrl(path) const mod = await importUrl(path, await mtimeEpoch(path))
if (mod?.default) if (mod?.default)
return mod.default as App return mod.default as App
} }