- 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]>
102 lines
4.3 KiB
JavaScript
102 lines
4.3 KiB
JavaScript
import grabDirNames, {} from "../utils/grab-dir-names";
|
|
import init from "./init";
|
|
import isDevelopment from "../utils/is-development";
|
|
import { log } from "../utils/log";
|
|
import cron from "./server/cron";
|
|
import allPagesESBuildContextBundler from "./bundler/all-pages-esbuild-context-bundler";
|
|
import serverPostBuildFn from "./server/server-post-build-fn";
|
|
import reactModulesBundler from "./bundler/react-modules-bundler";
|
|
import grabConstants from "../utils/grab-constants";
|
|
import watcherEsbuildCTX from "./server/watcher-esbuild-ctx";
|
|
import disposeBundlerContexts from "./bundler/dispose-bundler-contexts";
|
|
import { readFileSync } from "fs";
|
|
import setupDistDir from "./setup-dist-dir";
|
|
export default async function bunextInit(params) {
|
|
// First: everything below reads the artifact paths. A no-op under the
|
|
// CLI, which already resolved it; a custom server gets `dist_dir` from
|
|
// the config here.
|
|
await setupDistDir();
|
|
const dirNames = grabDirNames();
|
|
const { PAGES_DIR, BUNDLER_CTX_MAP_JSON, SSR_BUNDLER_CTX_MAP_JSON } = dirNames;
|
|
const is_dev = isDevelopment();
|
|
if (!is_dev || params?.start) {
|
|
try {
|
|
const bundler_ctx_json = JSON.parse(readFileSync(BUNDLER_CTX_MAP_JSON, "utf-8"));
|
|
if (bundler_ctx_json) {
|
|
global.BUNEXT_BUNDLER_CTX_MAP = bundler_ctx_json;
|
|
}
|
|
}
|
|
catch (error) { }
|
|
try {
|
|
const ssr_bundler_ctx_json = JSON.parse(readFileSync(SSR_BUNDLER_CTX_MAP_JSON, "utf-8"));
|
|
if (ssr_bundler_ctx_json) {
|
|
global.BUNEXT_SSR_BUNDLER_CTX_MAP = ssr_bundler_ctx_json;
|
|
}
|
|
}
|
|
catch (error) { }
|
|
}
|
|
const are_modules_aready_built = global.BUNEXT_BUNDLER_CTX_MAP && global.BUNEXT_SSR_BUNDLER_CTX_MAP;
|
|
global.BUNEXT_HMR_CONTROLLERS = [];
|
|
if (!global.BUNEXT_BUNDLER_CTX_MAP) {
|
|
global.BUNEXT_BUNDLER_CTX_MAP = {};
|
|
}
|
|
if (!global.BUNEXT_SSR_BUNDLER_CTX_MAP) {
|
|
global.BUNEXT_SSR_BUNDLER_CTX_MAP = {};
|
|
}
|
|
// global.BUNEXT_API_ROUTES_BUNDLER_CTX_MAP = {};
|
|
global.BUNEXT_BUNDLER_REBUILDS = 0;
|
|
global.BUNEXT_REBUILD_RETRIES = 0;
|
|
global.BUNEXT_BUILD_FAILED = false;
|
|
global.BUNEXT_PAGE_FILES = [];
|
|
global.BUNEXT_SKIPPED_BROWSER_MODULES = new Set();
|
|
global.BUNEXT_DIR_NAMES = dirNames;
|
|
global.BUNEXT_REACT_IMPORTS_MAP = { imports: {} };
|
|
global.BUNEXT_REACT_DOM_MODULE_CACHE = new Map();
|
|
global.BUNEXT_MAIN_CTX_BUILD_STARTS = 0;
|
|
await init();
|
|
log.banner();
|
|
global.BUNEXT_CONSTANTS = grabConstants();
|
|
await reactModulesBundler();
|
|
const router = new Bun.FileSystemRouter({
|
|
style: "nextjs",
|
|
dir: PAGES_DIR,
|
|
});
|
|
global.BUNEXT_ROUTER = router;
|
|
if (params?.build_only) {
|
|
log.build(`Building Modules ...`);
|
|
await allPagesESBuildContextBundler({ build_only: params.build_only });
|
|
}
|
|
else if (is_dev) {
|
|
log.build(`Building Modules ...`);
|
|
// Do NOT block on the full-site build: `Bun.serve` (or the start
|
|
// server) opens its port immediately and requests await
|
|
// BUNEXT_INITIAL_BUILD_PROMISE, so the site becomes reachable while
|
|
// the one-time compile still runs in the background.
|
|
const initial_build_promise = allPagesESBuildContextBundler({
|
|
post_build_fn: async (params) => {
|
|
await serverPostBuildFn(params);
|
|
},
|
|
});
|
|
global.BUNEXT_INITIAL_BUILD_PROMISE = initial_build_promise;
|
|
// Errors are logged (deduped) by the build pipeline itself; swallow
|
|
// the rejection here so it isn't an unhandled rejection.
|
|
initial_build_promise.catch(() => { });
|
|
// Start watching immediately so edits made during the initial build
|
|
// are not missed (rebuilds coalesce with the in-flight build).
|
|
watcherEsbuildCTX();
|
|
}
|
|
else {
|
|
if (are_modules_aready_built) {
|
|
log.success(`All modules already built.`);
|
|
}
|
|
else {
|
|
log.build(`Building Modules ...`);
|
|
await allPagesESBuildContextBundler({ start: true });
|
|
// The server only needs the artifact maps; don't keep
|
|
// esbuild's build state alive for the rest of the process.
|
|
await disposeBundlerContexts({ stop_service: true });
|
|
}
|
|
cron();
|
|
}
|
|
}
|