Skip to content

Instantly share code, notes, and snippets.

@lazuee
Created September 26, 2024 18:39
Show Gist options
  • Save lazuee/092de0b74d856c8658d1de8874f1678f to your computer and use it in GitHub Desktop.
Save lazuee/092de0b74d856c8658d1de8874f1678f to your computer and use it in GitHub Desktop.
ESM import cache busting
import * as fs from "node:fs";
import * as path from "node:path";
let build = await reimport(path.resolve("build/index.js"));
export async function reimport(path) {
const stat = fs.statSync(path);
// convert build path to URL for Windows compatibility with dynamic `import`
const BUILD_URL = url.pathToFileURL(path).href;
// use a timestamp query parameter to bust the import cache
return import(BUILD_URL + "?t=" + stat.mtimeMs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment