Files
bunext/dist/utils/import-fresh.js
tbenandClaude Opus 5.5 96e392f556 1.4.0: not_found, server-only response options, compression
- Server functions can return not_found: true to render the 404 page
  with status 404.
- Page components and window.__PAGE_PROPS__ (and HMR updates) only get
  props, query, url and error. response_options, redirect, cache settings
  and res_transform stay on the server, so cookies and private headers no
  longer end up in the HTML. BunextPageProps is narrowed to match
  (breaking for code that read server-only fields from component props).
- response_options headers go through a Headers object, so repeated
  headers such as several Set-Cookie survive.
- Redirects: status_code wins over permanent; response_options headers
  and res_transform apply to redirects too.
- Gzip for compressible text responses; compress: false to turn it off.
- Files the router matches but never bundles (.server.ts, __root,
  excluded paths and directories) return 404 instead of looping through
  dev rebuild retries.
- Missing public files return 404 rather than 403, so dotted page paths
  like /users/john.doe fall through to pages again.
- importFresh detects rewrites within the filesystem's mtime granularity.
- README: self-hosted React (not esm.sh), snake_case option names, static
  file serving, cache headers, the new options.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-09 04:14:56 +01:00

68 lines
2.2 KiB
JavaScript

import { existsSync, statSync } from "fs";
import path from "path";
import isDevelopment from "./is-development";
/**
* File path -> version (mtime, size, inode) of the module currently held
* in Bun's module registry (dev only). mtime alone misses rewrites within
* the filesystem's timestamp granularity (several ms on ext4).
*/
const loaded_versions = new Map();
/**
* Imports a module by its stable path.
*
* Dev: when the file changed since it was last imported, its registry
* entry is evicted first so the new version is evaluated and the old one
* can be garbage collected. A cache-busting `?t=` query would instead
* create a new, never-evicted module record on every call.
*
* Prod: plain import, so each module is loaded (and kept) once.
*/
export default async function importFresh(file_path) {
if (!isDevelopment()) {
return import(file_path);
}
let version;
try {
const stat = statSync(file_path);
version = `${stat.mtimeMs}:${stat.size}:${stat.ino}`;
}
catch { }
if (loaded_versions.has(file_path) &&
loaded_versions.get(file_path) !== version) {
evictModule(file_path);
}
try {
const mod = await import(file_path);
loaded_versions.set(file_path, version);
return mod;
}
catch (error) {
// Don't keep a failed evaluation around: the next request retries.
evictModule(file_path);
throw error;
}
}
/**
* Drops a module from Bun's registry so its memory can be reclaimed and the
* next import re-evaluates it. Only the module itself is evicted, not the
* modules it imports.
*/
export function evictModule(file_path) {
delete require.cache[file_path];
loaded_versions.delete(file_path);
}
/**
* Evicts every registry entry under `dir` whose file no longer exists. Bundle
* outputs (entries and shared chunks) are content-hashed and esbuild deletes
* the superseded ones on rebuild, so a missing file means no current module
* imports it anymore.
*/
export function evictDeletedModules(dir) {
const prefix = dir.endsWith(path.sep) ? dir : dir + path.sep;
for (const key of Object.keys(require.cache)) {
if (key.startsWith(prefix) && !existsSync(key)) {
evictModule(key);
}
}
}