swift-rust 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/build.mjs +47 -1
- package/bin/dev-server.mjs +760 -24
- package/bin/runtime/hmr-client.js +102 -0
- package/dist/router.d.ts +103 -0
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +58 -0
- package/dist/router.js.map +1 -1
- package/package.json +9 -8
package/bin/dev-server.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { existsSync, statSync, readFileSync, readdirSync, watch as fsWatch } from "node:fs";
|
|
2
|
+
import { existsSync, statSync, readFileSync, readdirSync, writeFileSync, unlinkSync, watch as fsWatch } from "node:fs";
|
|
3
3
|
import { join, resolve, extname, relative, dirname, sep } from "node:path";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
5
|
import { performance } from "node:perf_hooks";
|
|
@@ -42,7 +42,14 @@ const PUBLIC_DIR = resolve(cwd, "public");
|
|
|
42
42
|
const SWIFT_RUST_CONFIG = resolve(cwd, "swift-rust.config.json");
|
|
43
43
|
|
|
44
44
|
const PAGE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js"];
|
|
45
|
-
const SPECIAL_FILES = new Set([
|
|
45
|
+
const SPECIAL_FILES = new Set([
|
|
46
|
+
"layout", "loading", "error", "not-found", "template", "default", "route",
|
|
47
|
+
// RFC 0001 routing files
|
|
48
|
+
"guard", "loader", "action", "config", "schema", "proxy", "pending",
|
|
49
|
+
"revalidate", "shell", "fragment", "transition", "fallback", "prefetch",
|
|
50
|
+
"error-recovery", "i18n", "rpc", "stream", "edge", "worker", "query",
|
|
51
|
+
"state", "seo", "variant", "global-error",
|
|
52
|
+
]);
|
|
46
53
|
|
|
47
54
|
const moduleCache = new Map();
|
|
48
55
|
const compileTimings = new Map();
|
|
@@ -148,22 +155,22 @@ function findFile(dir, basename) {
|
|
|
148
155
|
function resolvePageRoute(segments) {
|
|
149
156
|
if (segments.length === 0) {
|
|
150
157
|
const file = findFile(APP_DIR, "page");
|
|
151
|
-
if (file) return { file, params: {}, segments: [] };
|
|
158
|
+
if (file) return { file, params: {}, segments: [], dirChain: [APP_DIR] };
|
|
152
159
|
return null;
|
|
153
160
|
}
|
|
154
|
-
return resolveRoute(APP_DIR, segments, 0, {});
|
|
161
|
+
return resolveRoute(APP_DIR, segments, 0, {}, [APP_DIR]);
|
|
155
162
|
}
|
|
156
163
|
|
|
157
|
-
function resolveRoute(dir, segments, idx, params) {
|
|
164
|
+
function resolveRoute(dir, segments, idx, params, chain) {
|
|
158
165
|
if (idx === segments.length) {
|
|
159
166
|
const file = findFile(dir, "page");
|
|
160
|
-
if (file) return { file, params, segments: segments.slice(0, idx) };
|
|
167
|
+
if (file) return { file, params, segments: segments.slice(0, idx), dirChain: chain };
|
|
161
168
|
return null;
|
|
162
169
|
}
|
|
163
170
|
const seg = segments[idx];
|
|
164
171
|
const directDir = join(dir, seg);
|
|
165
172
|
if (existsSync(directDir) && statSync(directDir).isDirectory()) {
|
|
166
|
-
const result = resolveRoute(directDir, segments, idx + 1, params);
|
|
173
|
+
const result = resolveRoute(directDir, segments, idx + 1, params, [...chain, directDir]);
|
|
167
174
|
if (result) return result;
|
|
168
175
|
}
|
|
169
176
|
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
|
@@ -175,7 +182,7 @@ function resolveRoute(dir, segments, idx, params) {
|
|
|
175
182
|
if (paramName.startsWith("...")) continue;
|
|
176
183
|
const paramDir = join(dir, e.name);
|
|
177
184
|
const newParams = { ...params, [paramName]: seg };
|
|
178
|
-
const result = resolveRoute(paramDir, segments, idx + 1, newParams);
|
|
185
|
+
const result = resolveRoute(paramDir, segments, idx + 1, newParams, [...chain, paramDir]);
|
|
179
186
|
if (result) return result;
|
|
180
187
|
}
|
|
181
188
|
}
|
|
@@ -183,6 +190,42 @@ function resolveRoute(dir, segments, idx, params) {
|
|
|
183
190
|
return null;
|
|
184
191
|
}
|
|
185
192
|
|
|
193
|
+
/** Resolve the leaf directory for URL segments (incl. dynamic), even when the
|
|
194
|
+
* segment has no page.tsx — used for stream.ts / rpc.ts handlers. */
|
|
195
|
+
function resolveLeafDir(segments) {
|
|
196
|
+
return walkLeafDir(APP_DIR, segments, 0, {});
|
|
197
|
+
}
|
|
198
|
+
function walkLeafDir(dir, segments, idx, params) {
|
|
199
|
+
if (idx === segments.length) return { dir, params };
|
|
200
|
+
const seg = segments[idx];
|
|
201
|
+
const directDir = join(dir, seg);
|
|
202
|
+
if (existsSync(directDir) && statSync(directDir).isDirectory()) {
|
|
203
|
+
const r = walkLeafDir(directDir, segments, idx + 1, params);
|
|
204
|
+
if (r) return r;
|
|
205
|
+
}
|
|
206
|
+
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
|
207
|
+
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
208
|
+
if (e.isDirectory() && e.name.startsWith("[") && e.name.endsWith("]") && !e.name.includes("...")) {
|
|
209
|
+
const pn = e.name.slice(1, -1);
|
|
210
|
+
const r = walkLeafDir(join(dir, e.name), segments, idx + 1, { ...params, [pn]: seg });
|
|
211
|
+
if (r) return r;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Collect a routing file (guard/loader/action/config/schema/…) along the
|
|
219
|
+
* matched directory chain, outermost → innermost. */
|
|
220
|
+
function collectRouteFiles(dirChain, basename) {
|
|
221
|
+
const out = [];
|
|
222
|
+
for (const dir of dirChain || []) {
|
|
223
|
+
const f = findFile(dir, basename);
|
|
224
|
+
if (f) out.push({ file: f, dir });
|
|
225
|
+
}
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
|
|
186
229
|
function resolveApiRoute(segments) {
|
|
187
230
|
if (segments.length === 0 || segments[0] !== "api") return null;
|
|
188
231
|
const apiDir = join(APP_DIR, "api");
|
|
@@ -256,6 +299,16 @@ function findLoading(segments) {
|
|
|
256
299
|
return findFile(APP_DIR, "loading");
|
|
257
300
|
}
|
|
258
301
|
|
|
302
|
+
/** Generic: find the nearest `<basename>` file walking the URL segments up. */
|
|
303
|
+
function findRouteFileUp(segments, basename) {
|
|
304
|
+
for (let i = segments?.length ?? 0; i >= 0; i--) {
|
|
305
|
+
const dir = i === 0 ? APP_DIR : join(APP_DIR, ...segments.slice(0, i));
|
|
306
|
+
const file = findFile(dir, basename);
|
|
307
|
+
if (file) return file;
|
|
308
|
+
}
|
|
309
|
+
return findFile(APP_DIR, basename);
|
|
310
|
+
}
|
|
311
|
+
|
|
259
312
|
function findRouteSegmentsForDir(dir) {
|
|
260
313
|
const rel = relative(APP_DIR, dir);
|
|
261
314
|
if (rel === "" || rel === ".") return [];
|
|
@@ -271,6 +324,57 @@ function bustCache(file) {
|
|
|
271
324
|
}
|
|
272
325
|
}
|
|
273
326
|
|
|
327
|
+
// Bumped on every file change. Used to invalidate the per-render SSR bundles
|
|
328
|
+
// below so that edits to *transitively imported* modules (components/, lib/)
|
|
329
|
+
// are picked up — Bun's module cache is path-keyed and ignores ?query busting,
|
|
330
|
+
// so a plain re-import of the page would still serve stale child modules.
|
|
331
|
+
let buildGeneration = 1;
|
|
332
|
+
|
|
333
|
+
// Externalize everything that isn't the app's own source (relative imports and
|
|
334
|
+
// the @/ alias). React and the framework stay shared/cached; only app code is
|
|
335
|
+
// re-bundled, so every render reflects the latest component/lib edits.
|
|
336
|
+
const externalizeDepsPlugin = {
|
|
337
|
+
name: "sr-externalize-deps",
|
|
338
|
+
setup(build) {
|
|
339
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
340
|
+
const p = args.path;
|
|
341
|
+
// App's own source (relative, absolute, or the @/ alias) → bundle fresh.
|
|
342
|
+
if (p.startsWith(".") || p.startsWith("/") || p.startsWith("@/")) return undefined;
|
|
343
|
+
// Everything else (react, swift-rust, node:*, @scope/*) → keep external.
|
|
344
|
+
return { path: p, external: true };
|
|
345
|
+
});
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const ssrBundleCache = new Map(); // file -> { gen, mod }
|
|
350
|
+
|
|
351
|
+
async function loadModuleFresh(filePath) {
|
|
352
|
+
if (typeof Bun === "undefined" || typeof Bun.build !== "function") {
|
|
353
|
+
return loadModule(filePath, { bust: true });
|
|
354
|
+
}
|
|
355
|
+
const cached = ssrBundleCache.get(filePath);
|
|
356
|
+
if (cached && cached.gen === buildGeneration) return cached.mod;
|
|
357
|
+
|
|
358
|
+
const result = await Bun.build({
|
|
359
|
+
entrypoints: [filePath],
|
|
360
|
+
target: "bun",
|
|
361
|
+
plugins: [externalizeDepsPlugin],
|
|
362
|
+
});
|
|
363
|
+
if (!result.success) {
|
|
364
|
+
throw new Error(result.logs.map((l) => l.message).join("\n"));
|
|
365
|
+
}
|
|
366
|
+
const code = await result.outputs[0].text();
|
|
367
|
+
const tmp = join(dirname(filePath), `.__sr_ssr_${buildGeneration}_${Math.random().toString(36).slice(2)}.mjs`);
|
|
368
|
+
writeFileSync(tmp, code);
|
|
369
|
+
try {
|
|
370
|
+
const mod = await import(pathToFileURL(tmp).href);
|
|
371
|
+
ssrBundleCache.set(filePath, { gen: buildGeneration, mod });
|
|
372
|
+
return mod;
|
|
373
|
+
} finally {
|
|
374
|
+
try { unlinkSync(tmp); } catch {}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
274
378
|
async function loadModule(filePath, { bust = false } = {}) {
|
|
275
379
|
const baseUrl = pathToFileURL(filePath).href;
|
|
276
380
|
const url = bust ? `${baseUrl}?t=${Date.now()}-${Math.random().toString(36).slice(2)}` : baseUrl;
|
|
@@ -605,7 +709,393 @@ async function renderToStringCompat(tree) {
|
|
|
605
709
|
return renderToString(tree);
|
|
606
710
|
}
|
|
607
711
|
|
|
608
|
-
|
|
712
|
+
// ── Client islands: page-level "use client" hydration ──────────────────────
|
|
713
|
+
// A page whose first meaningful line is `"use client"` is bundled for the
|
|
714
|
+
// browser and hydrated, so interactive components (e.g. the Pdf viewer) run.
|
|
715
|
+
// Static pages are untouched and ship zero JS.
|
|
716
|
+
|
|
717
|
+
const islandBundleCache = new Map(); // pageFile -> { mtime, code }
|
|
718
|
+
|
|
719
|
+
export function isClientPage(file) {
|
|
720
|
+
try {
|
|
721
|
+
for (const raw of readFileSync(file, "utf8").split("\n")) {
|
|
722
|
+
const t = raw.trim();
|
|
723
|
+
if (!t || t.startsWith("//") || t.startsWith("/*") || t.startsWith("*")) continue;
|
|
724
|
+
return /^["']use client["'];?$/.test(t);
|
|
725
|
+
}
|
|
726
|
+
} catch {}
|
|
727
|
+
return false;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
async function buildIslandBundle(pageFile) {
|
|
731
|
+
if (typeof Bun === "undefined" || typeof Bun.build !== "function") {
|
|
732
|
+
throw new Error("client islands require the Bun runtime");
|
|
733
|
+
}
|
|
734
|
+
const cached = islandBundleCache.get(pageFile);
|
|
735
|
+
if (cached && cached.gen === buildGeneration) return cached.code;
|
|
736
|
+
|
|
737
|
+
// Temp hydration entry, written next to the page so module resolution works.
|
|
738
|
+
const entryPath = join(dirname(pageFile), `.__sr_island_${process.pid}_${Date.now()}.js`);
|
|
739
|
+
const entry = `import { createRoot } from "react-dom/client";
|
|
740
|
+
import { createElement } from "react";
|
|
741
|
+
import Page from ${JSON.stringify(pageFile)};
|
|
742
|
+
const el = document.getElementById("__sr_island_root");
|
|
743
|
+
if (el) {
|
|
744
|
+
let params = {};
|
|
745
|
+
try { params = JSON.parse(el.getAttribute("data-sr-params") || "{}"); } catch {}
|
|
746
|
+
// Client-render (not hydrate): "use client" pages contain client-only
|
|
747
|
+
// widgets (e.g. the pdf.js viewer) whose SSR output intentionally differs
|
|
748
|
+
// from the first client render, so hydration would mismatch.
|
|
749
|
+
el.innerHTML = "";
|
|
750
|
+
createRoot(el).render(createElement(Page, { params }));
|
|
751
|
+
}
|
|
752
|
+
`;
|
|
753
|
+
writeFileSync(entryPath, entry);
|
|
754
|
+
try {
|
|
755
|
+
const result = await Bun.build({
|
|
756
|
+
entrypoints: [entryPath],
|
|
757
|
+
target: "browser",
|
|
758
|
+
minify: true,
|
|
759
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
760
|
+
});
|
|
761
|
+
if (!result.success) {
|
|
762
|
+
throw new Error(result.logs.map((l) => l.message).join("\n"));
|
|
763
|
+
}
|
|
764
|
+
const code = await result.outputs[0].text();
|
|
765
|
+
islandBundleCache.set(pageFile, { gen: buildGeneration, code });
|
|
766
|
+
return code;
|
|
767
|
+
} finally {
|
|
768
|
+
try { unlinkSync(entryPath); } catch {}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// ── Routing-file error handling & convention validation ─────────────────────
|
|
773
|
+
|
|
774
|
+
class RoutingFileError extends Error {
|
|
775
|
+
constructor(kind, file, cause) {
|
|
776
|
+
super(`Error in ${kind} (${relative(cwd, file)}): ${cause?.message ?? cause}`);
|
|
777
|
+
this.name = "RoutingFileError";
|
|
778
|
+
this.kind = kind;
|
|
779
|
+
this.file = file;
|
|
780
|
+
this.cause = cause;
|
|
781
|
+
if (cause?.stack) this.stack = cause.stack;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
const warnedRouting = new Set();
|
|
786
|
+
function warnRoutingFile(file, msg) {
|
|
787
|
+
const key = `${file}::${msg}`;
|
|
788
|
+
if (warnedRouting.has(key)) return;
|
|
789
|
+
warnedRouting.add(key);
|
|
790
|
+
process.stderr.write(` ${paint("yellow", "⚠")} ${paint("dim", "[routing]")} ${msg}\n`);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/** Warn when routing files are placed where the router will never find them
|
|
794
|
+
* (e.g. proxy.ts at the project root, or routing files in app/ when the app
|
|
795
|
+
* uses an app/src/ directory). Helps catch a very common mistake early. */
|
|
796
|
+
function validateRoutingConventions() {
|
|
797
|
+
const names = [...SPECIAL_FILES, "page"];
|
|
798
|
+
const appRel = relative(cwd, APP_DIR) || "app";
|
|
799
|
+
const usesSrc = APP_DIR.endsWith(`${sep}src`);
|
|
800
|
+
const scan = (dir, label) => {
|
|
801
|
+
if (!existsSync(dir) || resolve(dir) === resolve(APP_DIR)) return;
|
|
802
|
+
for (const name of names) {
|
|
803
|
+
const f = findFile(dir, name);
|
|
804
|
+
if (f) {
|
|
805
|
+
warnRoutingFile(
|
|
806
|
+
f,
|
|
807
|
+
`"${name}" found in ${label} — routing files belong under "${appRel}/". ` +
|
|
808
|
+
`Move ${relative(cwd, f)} into ${appRel}/ (the router only scans there).`,
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
scan(cwd, "the project root");
|
|
814
|
+
if (usesSrc) scan(dirname(APP_DIR), `"app/" (outside "src/")`);
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
// ── RFC 0001 route pipeline: config → schema → guard → loader/action ────────
|
|
818
|
+
|
|
819
|
+
let routerRuntimePromise = null;
|
|
820
|
+
function routerRuntime() {
|
|
821
|
+
if (!routerRuntimePromise) routerRuntimePromise = import("swift-rust/router").catch(() => null);
|
|
822
|
+
return routerRuntimePromise;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
function parseCookieHeader(header) {
|
|
826
|
+
const map = new Map();
|
|
827
|
+
for (const part of (header || "").split(";")) {
|
|
828
|
+
const i = part.indexOf("=");
|
|
829
|
+
if (i < 0) continue;
|
|
830
|
+
const k = part.slice(0, i).trim();
|
|
831
|
+
if (k) map.set(k, decodeURIComponent(part.slice(i + 1).trim()));
|
|
832
|
+
}
|
|
833
|
+
return map;
|
|
834
|
+
}
|
|
835
|
+
function serializeCookie(name, value, opts = {}) {
|
|
836
|
+
let s = `${name}=${encodeURIComponent(value)}`;
|
|
837
|
+
if (opts.maxAge != null) s += `; Max-Age=${opts.maxAge}`;
|
|
838
|
+
if (opts.path) s += `; Path=${opts.path}`;
|
|
839
|
+
else s += "; Path=/";
|
|
840
|
+
if (opts.domain) s += `; Domain=${opts.domain}`;
|
|
841
|
+
if (opts.httpOnly) s += "; HttpOnly";
|
|
842
|
+
if (opts.secure) s += "; Secure";
|
|
843
|
+
if (opts.sameSite) s += `; SameSite=${opts.sameSite[0].toUpperCase()}${opts.sameSite.slice(1)}`;
|
|
844
|
+
return s;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
function buildRouteCtx(req, url, params, searchParams) {
|
|
848
|
+
const cookieMap = parseCookieHeader(req?.headers?.get?.("cookie") || "");
|
|
849
|
+
const setCookies = [];
|
|
850
|
+
const cookies = {
|
|
851
|
+
get: (n) => cookieMap.get(n),
|
|
852
|
+
set: (n, v, o) => { cookieMap.set(n, v); setCookies.push(serializeCookie(n, v, o)); },
|
|
853
|
+
delete: (n, o) => { cookieMap.delete(n); setCookies.push(serializeCookie(n, "", { ...o, maxAge: 0 })); },
|
|
854
|
+
all: () => cookieMap,
|
|
855
|
+
};
|
|
856
|
+
const localsMap = new Map();
|
|
857
|
+
return {
|
|
858
|
+
url,
|
|
859
|
+
method: req?.method || "GET",
|
|
860
|
+
headers: req?.headers || new Headers(),
|
|
861
|
+
cookies,
|
|
862
|
+
params,
|
|
863
|
+
searchParams,
|
|
864
|
+
runtime: "node",
|
|
865
|
+
locals: { get: (k) => localsMap.get(k), set: (k, v) => localsMap.set(k, v) },
|
|
866
|
+
request: req,
|
|
867
|
+
__setCookies: setCookies,
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// Turn a returned RouteControl object into a thrown control the catch handles.
|
|
872
|
+
function applyControl(c) {
|
|
873
|
+
if (!c || typeof c !== "object" || !c.kind) return;
|
|
874
|
+
if (c.kind === "next") return;
|
|
875
|
+
const e = new Error(`route control: ${c.kind}`);
|
|
876
|
+
if (c.kind === "redirect") e.digest = `REDIRECT;${c.status || 307};${c.to}`;
|
|
877
|
+
else if (c.kind === "rewrite") e.digest = `REWRITE;${c.to}`;
|
|
878
|
+
else if (c.kind === "notFound") e.digest = "NOT_FOUND";
|
|
879
|
+
else if (c.kind === "response") e.__response = c.response;
|
|
880
|
+
else if (c.kind === "error") { throw c.error ?? new Error("route error"); }
|
|
881
|
+
throw e;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/** Merge config.ts along the chain (inner overrides outer). */
|
|
885
|
+
async function readMergedConfig(chain) {
|
|
886
|
+
let config = {};
|
|
887
|
+
for (const { file } of collectRouteFiles(chain, "config")) {
|
|
888
|
+
const mod = await loadModuleFresh(file);
|
|
889
|
+
const c = mod.config ?? mod.default;
|
|
890
|
+
if (c && typeof c === "object") config = { ...config, ...c, headers: { ...config.headers, ...c.headers } };
|
|
891
|
+
}
|
|
892
|
+
// edge.ts / worker.ts force a runtime.
|
|
893
|
+
const edge = await collectFirst(chain, "edge");
|
|
894
|
+
if (edge && (edge.edge || edge.default)) config.runtime = "edge";
|
|
895
|
+
const worker = await collectFirst(chain, "worker");
|
|
896
|
+
if (worker && (worker.default || worker.bindings)) config.runtime = "worker";
|
|
897
|
+
return config;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
async function runRoutePipeline(route, ctx) {
|
|
901
|
+
const chain = route.dirChain || [];
|
|
902
|
+
|
|
903
|
+
// proxy.ts — phase 1, outer → inner. Cheap, data-free interception
|
|
904
|
+
// (Next.js calls this "proxy"; "middleware" is accepted with a warning).
|
|
905
|
+
for (const { file } of collectRouteFiles(chain, "proxy")) {
|
|
906
|
+
const mod = await loadModuleFresh(file);
|
|
907
|
+
const fn = mod.default ?? mod.proxy;
|
|
908
|
+
if (typeof fn === "function") {
|
|
909
|
+
const matcher = mod.matcher;
|
|
910
|
+
const matched = !matcher || matchesMatcher(ctx.url.pathname, matcher);
|
|
911
|
+
if (matched) {
|
|
912
|
+
try {
|
|
913
|
+
applyControl(await fn(ctx));
|
|
914
|
+
} catch (e) {
|
|
915
|
+
if (e?.digest || e?.__response) throw e; // control flow, re-throw
|
|
916
|
+
throw new RoutingFileError("proxy", file, e);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
// Back-compat: warn if the old name is used.
|
|
922
|
+
for (const { file } of collectRouteFiles(chain, "middleware")) {
|
|
923
|
+
warnRoutingFile(file, `"middleware.ts" was renamed to "proxy.ts" — rename ${relative(cwd, file)}.`);
|
|
924
|
+
const mod = await loadModuleFresh(file);
|
|
925
|
+
const fn = mod.default ?? mod.middleware;
|
|
926
|
+
if (typeof fn === "function") {
|
|
927
|
+
const matcher = mod.matcher;
|
|
928
|
+
if (!matcher || matchesMatcher(ctx.url.pathname, matcher)) applyControl(await fn(ctx));
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// config.ts — merged, applied to the response by the caller.
|
|
933
|
+
const config = await readMergedConfig(chain);
|
|
934
|
+
|
|
935
|
+
// i18n.ts — resolve the active locale into locals (cookie/header/default).
|
|
936
|
+
const i18nMod = await collectFirst(chain, "i18n");
|
|
937
|
+
const i18nCfg = i18nMod?.i18n ?? i18nMod?.default;
|
|
938
|
+
if (i18nCfg && Array.isArray(i18nCfg.locales)) {
|
|
939
|
+
let locale = i18nCfg.defaultLocale ?? i18nCfg.locales[0];
|
|
940
|
+
if (typeof i18nCfg.resolve === "function") locale = (await i18nCfg.resolve(ctx)) || locale;
|
|
941
|
+
else if (i18nCfg.strategy === "cookie") locale = ctx.cookies.get("locale") || locale;
|
|
942
|
+
else if (i18nCfg.strategy === "header") {
|
|
943
|
+
const al = ctx.headers.get?.("accept-language")?.split(",")[0]?.split("-")[0];
|
|
944
|
+
if (al && i18nCfg.locales.includes(al)) locale = al;
|
|
945
|
+
}
|
|
946
|
+
ctx.locals.set("locale", locale);
|
|
947
|
+
ctx.__locale = locale;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
// schema.ts — validate/brand params + searchParams (Standard-Schema/Zod-like)
|
|
951
|
+
for (const { file } of collectRouteFiles(chain, "schema")) {
|
|
952
|
+
const mod = await loadModuleFresh(file);
|
|
953
|
+
if (mod.params?.safeParse) {
|
|
954
|
+
const r = mod.params.safeParse(ctx.params);
|
|
955
|
+
if (!r.success) { const e = new Error("Invalid params"); e.__response = jsonResponse({ error: "Invalid params", issues: r.error?.issues ?? r.error }, 400); throw e; }
|
|
956
|
+
Object.assign(ctx.params, r.data);
|
|
957
|
+
}
|
|
958
|
+
const querySpec = (await collectFirst(chain, "query"))?.query;
|
|
959
|
+
const searchSchema = querySpec?.parse ?? mod.searchParams;
|
|
960
|
+
if (searchSchema?.safeParse) {
|
|
961
|
+
const r = searchSchema.safeParse(ctx.searchParams);
|
|
962
|
+
if (r.success) Object.assign(ctx.searchParams, r.data);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// guard.ts — outer → inner
|
|
967
|
+
for (const { file } of collectRouteFiles(chain, "guard")) {
|
|
968
|
+
const mod = await loadModuleFresh(file);
|
|
969
|
+
const fn = mod.default ?? mod.guard;
|
|
970
|
+
if (typeof fn === "function") {
|
|
971
|
+
try {
|
|
972
|
+
applyControl(await fn(ctx));
|
|
973
|
+
} catch (e) {
|
|
974
|
+
if (e?.digest || e?.__response) throw e;
|
|
975
|
+
throw new RoutingFileError("guard", file, e);
|
|
976
|
+
}
|
|
977
|
+
} else if (mod.default !== undefined || mod.guard !== undefined) {
|
|
978
|
+
warnRoutingFile(file, `guard.ts must export a function (default export). ${relative(cwd, file)}`);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
let actionData;
|
|
983
|
+
// action.ts on mutating requests (leaf only)
|
|
984
|
+
if (ctx.method !== "GET" && ctx.method !== "HEAD") {
|
|
985
|
+
const actions = collectRouteFiles(chain, "action");
|
|
986
|
+
const leaf = actions[actions.length - 1];
|
|
987
|
+
if (leaf) {
|
|
988
|
+
const mod = await loadModuleFresh(leaf.file);
|
|
989
|
+
const fn = mod.default ?? mod.action;
|
|
990
|
+
if (typeof fn === "function") {
|
|
991
|
+
const actx = Object.assign({}, ctx, {
|
|
992
|
+
formData: () => ctx.request.formData(),
|
|
993
|
+
json: () => ctx.request.json(),
|
|
994
|
+
});
|
|
995
|
+
try {
|
|
996
|
+
const result = await fn(actx);
|
|
997
|
+
applyControl(result);
|
|
998
|
+
actionData = result;
|
|
999
|
+
} catch (e) {
|
|
1000
|
+
if (e?.digest || e?.__response) throw e;
|
|
1001
|
+
throw new RoutingFileError("action", leaf.file, e);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// loader.ts — run in parallel along the chain
|
|
1008
|
+
const loaders = collectRouteFiles(chain, "loader");
|
|
1009
|
+
const loaded = await Promise.all(
|
|
1010
|
+
loaders.map(async ({ file, dir }) => {
|
|
1011
|
+
const mod = await loadModuleFresh(file);
|
|
1012
|
+
const fn = mod.default ?? mod.loader;
|
|
1013
|
+
if (typeof fn !== "function") return [dir, undefined];
|
|
1014
|
+
const lctx = Object.assign({}, ctx, { parent: () => undefined });
|
|
1015
|
+
try {
|
|
1016
|
+
return [dir, await fn(lctx)];
|
|
1017
|
+
} catch (e) {
|
|
1018
|
+
if (e?.digest || e?.__response) throw e;
|
|
1019
|
+
throw new RoutingFileError("loader", file, e);
|
|
1020
|
+
}
|
|
1021
|
+
}),
|
|
1022
|
+
);
|
|
1023
|
+
const loadersMap = {};
|
|
1024
|
+
for (const [dir, data] of loaded) loadersMap[relative(cwd, dir)] = data;
|
|
1025
|
+
const loaderData = loaded.length ? loaded[loaded.length - 1][1] : undefined;
|
|
1026
|
+
|
|
1027
|
+
// state.ts — server-side state to hydrate a client store.
|
|
1028
|
+
let serverState;
|
|
1029
|
+
const stateMod = await collectFirst(chain, "state");
|
|
1030
|
+
const stateFn = stateMod?.default ?? stateMod?.state;
|
|
1031
|
+
if (typeof stateFn === "function") serverState = await stateFn(ctx);
|
|
1032
|
+
|
|
1033
|
+
// seo.tsx — structured data / head injection (has loader data).
|
|
1034
|
+
let seoHead = "";
|
|
1035
|
+
const seoMod = await collectFirst(chain, "seo");
|
|
1036
|
+
const seoFn = seoMod?.default ?? seoMod?.seo;
|
|
1037
|
+
if (typeof seoFn === "function") {
|
|
1038
|
+
seoHead = buildSeoHead(await seoFn(Object.assign({}, ctx, { data: loaderData })));
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
// revalidate.ts — leaf decides cache TTL / tags.
|
|
1042
|
+
let revalidatePlan = config.revalidate != null ? { ttl: config.revalidate } : undefined;
|
|
1043
|
+
const rev = await collectFirst(chain, "revalidate");
|
|
1044
|
+
const revFn = rev?.default ?? rev?.revalidate;
|
|
1045
|
+
if (typeof revFn === "function") {
|
|
1046
|
+
const plan = await revFn(Object.assign({}, ctx, { data: loaderData, afterAction: ctx.method !== "GET" }));
|
|
1047
|
+
if (plan && typeof plan === "object") revalidatePlan = { ...revalidatePlan, ...plan };
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
return { actionData, loaderData, loaders: loadersMap, setCookies: ctx.__setCookies, config, revalidatePlan, serverState, seoHead };
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
function escAttr(s) {
|
|
1054
|
+
return String(s).replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
1055
|
+
}
|
|
1056
|
+
function buildSeoHead(r) {
|
|
1057
|
+
if (!r || typeof r !== "object") return "";
|
|
1058
|
+
const out = [];
|
|
1059
|
+
if (r.title) out.push(`<title>${escAttr(r.title)}</title>`);
|
|
1060
|
+
if (r.description) out.push(`<meta name="description" content="${escAttr(r.description)}" />`);
|
|
1061
|
+
if (r.canonical) out.push(`<link rel="canonical" href="${escAttr(r.canonical)}" />`);
|
|
1062
|
+
if (r.robots) out.push(`<meta name="robots" content="${escAttr(r.robots)}" />`);
|
|
1063
|
+
for (const [k, v] of Object.entries(r.openGraph || {})) out.push(`<meta property="og:${escAttr(k)}" content="${escAttr(v)}" />`);
|
|
1064
|
+
for (const a of r.alternates || []) out.push(`<link rel="alternate" hreflang="${escAttr(a.hreflang)}" href="${escAttr(a.href)}" />`);
|
|
1065
|
+
const jsonLd = r.jsonLd ? (Array.isArray(r.jsonLd) ? r.jsonLd : [r.jsonLd]) : [];
|
|
1066
|
+
for (const ld of jsonLd) out.push(`<script type="application/ld+json">${JSON.stringify(ld).replace(/</g, "\\u003c")}</script>`);
|
|
1067
|
+
return out.join("\n");
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
async function collectFirst(chain, basename) {
|
|
1071
|
+
const files = collectRouteFiles(chain, basename);
|
|
1072
|
+
if (files.length === 0) return null;
|
|
1073
|
+
return await loadModuleFresh(files[files.length - 1].file);
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
function matchesMatcher(pathname, matcher) {
|
|
1077
|
+
const list = Array.isArray(matcher) ? matcher : [matcher];
|
|
1078
|
+
return list.some((m) => {
|
|
1079
|
+
if (typeof m !== "string") return false;
|
|
1080
|
+
// simple glob: * → [^/]*, ** → .*
|
|
1081
|
+
const re = new RegExp("^" + m.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "::").replace(/\*/g, "[^/]*").replace(/::/g, ".*") + "$");
|
|
1082
|
+
return re.test(pathname);
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
function jsonResponse(obj, status = 200) {
|
|
1087
|
+
return new Response(JSON.stringify(obj), { status, headers: { "Content-Type": "application/json" } });
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
function cacheControlFromPlan(plan) {
|
|
1091
|
+
if (!plan) return null;
|
|
1092
|
+
if (plan.ttl === false) return "public, max-age=31536000, immutable";
|
|
1093
|
+
if (plan.ttl === 0) return "no-store";
|
|
1094
|
+
if (typeof plan.ttl === "number") return `public, max-age=0, s-maxage=${plan.ttl}, stale-while-revalidate`;
|
|
1095
|
+
return null;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
async function renderRoute(urlPath, req) {
|
|
609
1099
|
const segments = urlToRouteSegments(urlPath);
|
|
610
1100
|
const route = resolvePageRoute(segments);
|
|
611
1101
|
if (!route) {
|
|
@@ -618,8 +1108,48 @@ async function renderRoute(urlPath) {
|
|
|
618
1108
|
|
|
619
1109
|
try {
|
|
620
1110
|
const React = await import("react");
|
|
621
|
-
const
|
|
622
|
-
const
|
|
1111
|
+
const url = req ? new URL(req.url) : new URL(urlPath, "http://localhost");
|
|
1112
|
+
const searchParams = Object.fromEntries(url.searchParams.entries());
|
|
1113
|
+
const ctx = buildRouteCtx(req, url, { ...route.params }, searchParams);
|
|
1114
|
+
|
|
1115
|
+
// Run the RFC 0001 pipeline (config/schema/guard/loader/action).
|
|
1116
|
+
const pipeline = await runRoutePipeline(route, ctx);
|
|
1117
|
+
route.params = ctx.params; // validated/branded params flow to the page
|
|
1118
|
+
|
|
1119
|
+
const runtime = await routerRuntime();
|
|
1120
|
+
if (runtime?.__setRouteContext) {
|
|
1121
|
+
runtime.__setRouteContext({
|
|
1122
|
+
request: ctx,
|
|
1123
|
+
loaderData: pipeline.loaderData,
|
|
1124
|
+
actionData: pipeline.actionData,
|
|
1125
|
+
loaders: pipeline.loaders,
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
// variant.tsx — pick an A/B variant component (bucket from middleware/
|
|
1130
|
+
// cookie/assign), else fall back to page.tsx.
|
|
1131
|
+
let Page;
|
|
1132
|
+
const leafDir = (route.dirChain || [])[(route.dirChain || []).length - 1];
|
|
1133
|
+
const variantFile = leafDir && findFile(leafDir, "variant");
|
|
1134
|
+
if (variantFile) {
|
|
1135
|
+
const vmod = await loadModuleFresh(variantFile);
|
|
1136
|
+
if (vmod.variants && typeof vmod.variants === "object") {
|
|
1137
|
+
const bucket =
|
|
1138
|
+
(typeof vmod.assign === "function" ? vmod.assign(ctx) : null) ||
|
|
1139
|
+
ctx.locals.get("bucket") ||
|
|
1140
|
+
ctx.cookies.get("bucket") ||
|
|
1141
|
+
Object.keys(vmod.variants)[0];
|
|
1142
|
+
const loader = vmod.variants[bucket];
|
|
1143
|
+
if (typeof loader === "function") {
|
|
1144
|
+
const m = await loader();
|
|
1145
|
+
Page = m.default ?? m;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
if (!Page) {
|
|
1150
|
+
const pageMod = await loadModuleFresh(route.file);
|
|
1151
|
+
Page = pageMod.default ?? pageMod.Page ?? pageMod.page;
|
|
1152
|
+
}
|
|
623
1153
|
if (!Page) {
|
|
624
1154
|
return { status: 500, html: null, error: new Error(`page ${route.file} has no default export`) };
|
|
625
1155
|
}
|
|
@@ -628,19 +1158,44 @@ async function renderRoute(urlPath) {
|
|
|
628
1158
|
get: (t, k) => (k in t ? t[k] : dynamicParams.get(String(k))),
|
|
629
1159
|
});
|
|
630
1160
|
|
|
1161
|
+
const clientPage = isClientPage(route.file);
|
|
631
1162
|
let tree = React.createElement(Page, { params: paramsProxy });
|
|
1163
|
+
if (clientPage) {
|
|
1164
|
+
// Wrap in a hydration root so the client bundle can mount into it.
|
|
1165
|
+
tree = React.createElement(
|
|
1166
|
+
"div",
|
|
1167
|
+
{ id: "__sr_island_root", "data-sr-params": JSON.stringify(route.params || {}) },
|
|
1168
|
+
tree,
|
|
1169
|
+
);
|
|
1170
|
+
}
|
|
632
1171
|
for (let i = layouts.length - 1; i >= 0; i--) {
|
|
633
|
-
const layoutMod = await
|
|
1172
|
+
const layoutMod = await loadModuleFresh(layouts[i].file);
|
|
634
1173
|
const Layout = layoutMod.default ?? layoutMod.Layout ?? layoutMod.layout;
|
|
635
1174
|
if (Layout) tree = React.createElement(Layout, null, tree);
|
|
636
1175
|
}
|
|
637
1176
|
const html = await renderToStringCompat(tree);
|
|
1177
|
+
if (runtime?.__setRouteContext) runtime.__setRouteContext(null);
|
|
638
1178
|
const metadata = await resolveMetadata(layouts.map((l) => l.file), route.file, route.params, segments);
|
|
639
|
-
return { status: 200, html, metadata, error: null, pageFile: route.file, layoutFiles: layouts.map((l) => l.file), notFoundFile, errorFile, loadingFile, segments };
|
|
1179
|
+
return { status: 200, html, metadata, error: null, clientPage, pageFile: route.file, layoutFiles: layouts.map((l) => l.file), notFoundFile, errorFile, loadingFile, segments, setCookies: pipeline.setCookies, actionData: pipeline.actionData, config: pipeline.config, revalidatePlan: pipeline.revalidatePlan, seoHead: pipeline.seoHead, serverState: pipeline.serverState };
|
|
640
1180
|
} catch (err) {
|
|
1181
|
+
const rt = await routerRuntime();
|
|
1182
|
+
if (rt?.__setRouteContext) rt.__setRouteContext(null);
|
|
1183
|
+
if (err?.__response instanceof Response) {
|
|
1184
|
+
return { status: err.__response.status, rawResponse: err.__response, html: null, error: null, segments };
|
|
1185
|
+
}
|
|
641
1186
|
if (err?.digest === "NOT_FOUND" || err?.name === "NotFoundError") {
|
|
642
1187
|
return await renderNotFound(segments);
|
|
643
1188
|
}
|
|
1189
|
+
if (err?.digest === "FORBIDDEN" || err?.name === "ForbiddenError") {
|
|
1190
|
+
return { status: 403, html: null, error: null, segments, rawResponse: new Response("Forbidden", { status: 403, headers: { "Content-Type": "text/plain" } }) };
|
|
1191
|
+
}
|
|
1192
|
+
if (err?.digest === "UNAUTHORIZED" || err?.name === "UnauthorizedError") {
|
|
1193
|
+
return { status: 401, html: null, error: null, segments, rawResponse: new Response("Unauthorized", { status: 401, headers: { "Content-Type": "text/plain" } }) };
|
|
1194
|
+
}
|
|
1195
|
+
if (err?.digest && String(err.digest).startsWith("REWRITE")) {
|
|
1196
|
+
const to = String(err.digest).slice("REWRITE;".length);
|
|
1197
|
+
return await renderRoute(to, req);
|
|
1198
|
+
}
|
|
644
1199
|
if (err?.digest && String(err.digest).startsWith("REDIRECT")) {
|
|
645
1200
|
const [, statusStr, ...rest] = String(err.digest).split(";");
|
|
646
1201
|
return { status: parseInt(statusStr, 10) || 307, redirect: rest.join(";"), html: null, error: null, segments };
|
|
@@ -656,6 +1211,21 @@ async function renderRoute(urlPath) {
|
|
|
656
1211
|
}
|
|
657
1212
|
} catch {}
|
|
658
1213
|
}
|
|
1214
|
+
// error-recovery.tsx — richer boundary with retry/reset (SSR: stub callbacks).
|
|
1215
|
+
const recoveryFile = findRouteFileUp(segments, "error-recovery");
|
|
1216
|
+
if (recoveryFile) {
|
|
1217
|
+
try {
|
|
1218
|
+
const React = await import("react");
|
|
1219
|
+
const mod = await loadModule(recoveryFile, { bust: true });
|
|
1220
|
+
const Recovery = mod.default ?? mod.ErrorRecovery;
|
|
1221
|
+
if (Recovery) {
|
|
1222
|
+
const html = await renderToStringCompat(
|
|
1223
|
+
React.createElement(Recovery, { error: err, attempt: 1, retry: () => {}, reset: () => {} }),
|
|
1224
|
+
);
|
|
1225
|
+
return { status: 500, html, metadata: null, error: err, segments, pageFile: route.file };
|
|
1226
|
+
}
|
|
1227
|
+
} catch {}
|
|
1228
|
+
}
|
|
659
1229
|
return { status: 500, html: null, error: err, segments, pageFile: route.file };
|
|
660
1230
|
}
|
|
661
1231
|
}
|
|
@@ -690,11 +1260,38 @@ function errorOverlayHTML(message, stack, extra) {
|
|
|
690
1260
|
return renderErrorOverlay({ message, stack, ...(extra || {}) });
|
|
691
1261
|
}
|
|
692
1262
|
|
|
1263
|
+
// Metadata icon files looked up at the root of the app directory (app/ or
|
|
1264
|
+
// app/src/), Next.js App Router style. Found files are served from the site
|
|
1265
|
+
// root and auto-linked in <head>.
|
|
1266
|
+
const APP_ICON_FILES = ["favicon.ico", "favicon.svg", "icon.svg", "icon.png", "apple-icon.png"];
|
|
1267
|
+
|
|
1268
|
+
function discoverAppIcons() {
|
|
1269
|
+
const icons = [];
|
|
1270
|
+
for (const name of APP_ICON_FILES) {
|
|
1271
|
+
const file = join(APP_DIR, name);
|
|
1272
|
+
if (existsSync(file) && statSync(file).isFile()) icons.push({ name, file });
|
|
1273
|
+
}
|
|
1274
|
+
return icons;
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
function buildAppIconsLinkTags() {
|
|
1278
|
+
return discoverAppIcons()
|
|
1279
|
+
.map(({ name }) => {
|
|
1280
|
+
const ext = extname(name).toLowerCase();
|
|
1281
|
+
if (name.startsWith("apple-icon")) return `<link rel="apple-touch-icon" href="/${name}" />`;
|
|
1282
|
+
if (ext === ".ico") return `<link rel="icon" href="/${name}" sizes="any" />`;
|
|
1283
|
+
if (ext === ".svg") return `<link rel="icon" href="/${name}" type="image/svg+xml" />`;
|
|
1284
|
+
return `<link rel="icon" href="/${name}" />`;
|
|
1285
|
+
})
|
|
1286
|
+
.join("\n");
|
|
1287
|
+
}
|
|
1288
|
+
|
|
693
1289
|
async function buildHead(head) {
|
|
694
1290
|
const css = await getProcessedGlobalsCss();
|
|
695
1291
|
const fontLink = buildGoogleFontsLinkTag();
|
|
696
1292
|
return [
|
|
697
1293
|
head || "",
|
|
1294
|
+
buildAppIconsLinkTags(),
|
|
698
1295
|
fontLink,
|
|
699
1296
|
css ? `<style data-swift-rust-globals>${escapeForStyleTag(css)}</style>` : "",
|
|
700
1297
|
].filter(Boolean).join("\n");
|
|
@@ -773,6 +1370,7 @@ function setupWatcher() {
|
|
|
773
1370
|
}
|
|
774
1371
|
} catch {}
|
|
775
1372
|
logEvent("change", full);
|
|
1373
|
+
buildGeneration++;
|
|
776
1374
|
bustCache(full);
|
|
777
1375
|
if (full.includes(`${sep}globals.${"css"}`)) {
|
|
778
1376
|
globalsCssCache = { file: null, mtime: 0, css: "" };
|
|
@@ -796,6 +1394,12 @@ function setupWatcher() {
|
|
|
796
1394
|
}
|
|
797
1395
|
}
|
|
798
1396
|
walk(APP_DIR);
|
|
1397
|
+
// Also watch sibling source dirs that pages/layouts import from — without
|
|
1398
|
+
// this, edits to components/, lib/, etc. never trigger a reload.
|
|
1399
|
+
for (const extra of ["components", "lib", "app"]) {
|
|
1400
|
+
const p = resolve(cwd, extra);
|
|
1401
|
+
if (existsSync(p)) walk(p);
|
|
1402
|
+
}
|
|
799
1403
|
}
|
|
800
1404
|
|
|
801
1405
|
const networkUrls = [];
|
|
@@ -954,6 +1558,22 @@ async function handleRequest(req, res) {
|
|
|
954
1558
|
return;
|
|
955
1559
|
}
|
|
956
1560
|
|
|
1561
|
+
// App-directory metadata icons (app/favicon.ico, app/icon.svg, …) served
|
|
1562
|
+
// from the site root, Next.js style.
|
|
1563
|
+
{
|
|
1564
|
+
const iconName = pathname.replace(/^\/+/, "");
|
|
1565
|
+
if (APP_ICON_FILES.includes(iconName)) {
|
|
1566
|
+
const candidate = join(APP_DIR, iconName);
|
|
1567
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
1568
|
+
const ext = extname(candidate).toLowerCase();
|
|
1569
|
+
const mime = { ".ico": "image/x-icon", ".svg": "image/svg+xml", ".png": "image/png" }[ext] || "application/octet-stream";
|
|
1570
|
+
res.writeHead(200, { "Content-Type": mime });
|
|
1571
|
+
res.end(readFileSync(candidate));
|
|
1572
|
+
return;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
|
|
957
1577
|
if (existsSync(PUBLIC_DIR)) {
|
|
958
1578
|
const safe = pathname.replace(/\.\.+/g, "").replace(/^\/+/, "");
|
|
959
1579
|
const candidate = join(PUBLIC_DIR, safe);
|
|
@@ -1007,7 +1627,7 @@ async function handleRequest(req, res) {
|
|
|
1007
1627
|
}
|
|
1008
1628
|
|
|
1009
1629
|
const renderStart = performance.now();
|
|
1010
|
-
const renderResult = await renderRoute(pathname);
|
|
1630
|
+
const renderResult = await renderRoute(pathname, req);
|
|
1011
1631
|
const renderMs = performance.now() - renderStart;
|
|
1012
1632
|
const total = performance.now() - reqStart;
|
|
1013
1633
|
|
|
@@ -1122,10 +1742,48 @@ async function handleFetch(req) {
|
|
|
1122
1742
|
return new Response("Image not found", { status: 404 });
|
|
1123
1743
|
}
|
|
1124
1744
|
|
|
1745
|
+
// Client-island hydration bundle for a "use client" page.
|
|
1746
|
+
if (pathname === "/_swift-rust/island.js") {
|
|
1747
|
+
const p = url.searchParams.get("p");
|
|
1748
|
+
if (p && existsSync(p)) {
|
|
1749
|
+
try {
|
|
1750
|
+
const code = await buildIslandBundle(p);
|
|
1751
|
+
return new Response(code, {
|
|
1752
|
+
headers: { "Content-Type": "application/javascript; charset=utf-8", "Cache-Control": "no-cache" },
|
|
1753
|
+
});
|
|
1754
|
+
} catch (e) {
|
|
1755
|
+
logError(e, `island bundle failed for ${p}`);
|
|
1756
|
+
return new Response(`/* island build error: ${String(e?.message || e).replace(/\*\//g, "* /")} */`, {
|
|
1757
|
+
status: 500,
|
|
1758
|
+
headers: { "Content-Type": "application/javascript; charset=utf-8" },
|
|
1759
|
+
});
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
return new Response("// island not found", { status: 404, headers: { "Content-Type": "application/javascript" } });
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1125
1765
|
if (pathname.startsWith("/_swift-rust/")) {
|
|
1126
1766
|
return new Response("Not found", { status: 404 });
|
|
1127
1767
|
}
|
|
1128
1768
|
|
|
1769
|
+
// App-directory metadata icons (app/favicon.ico, app/favicon.svg, …) served
|
|
1770
|
+
// from the site root, Next.js style.
|
|
1771
|
+
{
|
|
1772
|
+
const iconName = pathname.replace(/^\/+/, "");
|
|
1773
|
+
if (APP_ICON_FILES.includes(iconName)) {
|
|
1774
|
+
const candidate = join(APP_DIR, iconName);
|
|
1775
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
1776
|
+
const ext = extname(candidate).toLowerCase();
|
|
1777
|
+
const mime =
|
|
1778
|
+
{ ".ico": "image/x-icon", ".svg": "image/svg+xml", ".png": "image/png" }[ext] ||
|
|
1779
|
+
"application/octet-stream";
|
|
1780
|
+
const file = Bun?.file ? Bun.file(candidate) : readFileSync(candidate);
|
|
1781
|
+
logRequest({ method, url: pathname, status: 200, duration: performance.now() - reqStart, compileMs: 0 });
|
|
1782
|
+
return new Response(file, { headers: { "Content-Type": mime } });
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1129
1787
|
if (existsSync(PUBLIC_DIR)) {
|
|
1130
1788
|
const safe = pathname.replace(/\.\.+/g, "").replace(/^\/+/, "");
|
|
1131
1789
|
const candidate = join(PUBLIC_DIR, safe);
|
|
@@ -1144,7 +1802,59 @@ async function handleFetch(req) {
|
|
|
1144
1802
|
return await handleApiRoute(req, segments, method, reqStart);
|
|
1145
1803
|
}
|
|
1146
1804
|
|
|
1147
|
-
|
|
1805
|
+
// rpc.ts / stream.ts handlers (leaf, route.ts-like).
|
|
1806
|
+
{
|
|
1807
|
+
const leaf = resolveLeafDir(segments);
|
|
1808
|
+
if (leaf) {
|
|
1809
|
+
const sp = Object.fromEntries(url.searchParams.entries());
|
|
1810
|
+
const baseCtx = buildRouteCtx(req, url, leaf.params, sp);
|
|
1811
|
+
const rpcFile = findFile(leaf.dir, "rpc");
|
|
1812
|
+
if (rpcFile) {
|
|
1813
|
+
try {
|
|
1814
|
+
const mod = await loadModuleFresh(rpcFile);
|
|
1815
|
+
const procs = mod.procedures ?? mod.default;
|
|
1816
|
+
if (procs && typeof procs === "object") {
|
|
1817
|
+
if (method === "POST") {
|
|
1818
|
+
const body = await req.json().catch(() => ({}));
|
|
1819
|
+
const proc = procs[body.procedure];
|
|
1820
|
+
if (!proc) return jsonResponse({ error: `Unknown procedure: ${body.procedure}` }, 404);
|
|
1821
|
+
let input = body.input;
|
|
1822
|
+
if (proc.input?.safeParse) {
|
|
1823
|
+
const r = proc.input.safeParse(input);
|
|
1824
|
+
if (!r.success) return jsonResponse({ error: "Invalid input", issues: r.error?.issues ?? r.error }, 400);
|
|
1825
|
+
input = r.data;
|
|
1826
|
+
}
|
|
1827
|
+
return jsonResponse({ data: await proc.handler(input, baseCtx) });
|
|
1828
|
+
}
|
|
1829
|
+
if (method === "GET") return jsonResponse({ procedures: Object.keys(procs) });
|
|
1830
|
+
}
|
|
1831
|
+
} catch (e) {
|
|
1832
|
+
return jsonResponse({ error: String(e?.message || e) }, 500);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
const streamFile = findFile(leaf.dir, "stream");
|
|
1836
|
+
if (streamFile && !findFile(leaf.dir, "page")) {
|
|
1837
|
+
try {
|
|
1838
|
+
const mod = await loadModuleFresh(streamFile);
|
|
1839
|
+
const fn = mod.default ?? mod.stream;
|
|
1840
|
+
if (typeof fn === "function") {
|
|
1841
|
+
const result = await fn(baseCtx);
|
|
1842
|
+
if (result instanceof Response) return result;
|
|
1843
|
+
return new Response(result, {
|
|
1844
|
+
headers: { "Content-Type": mod.contentType || "text/event-stream", "Cache-Control": "no-cache" },
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
} catch (e) {
|
|
1848
|
+
return new Response(String(e?.message || e), { status: 500 });
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// Non-GET requests are allowed to reach the page render so action.ts can run;
|
|
1855
|
+
// pages without an action simply ignore the body.
|
|
1856
|
+
const ALLOWED_METHODS = new Set(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"]);
|
|
1857
|
+
if (!ALLOWED_METHODS.has(method)) {
|
|
1148
1858
|
return new Response("Method not allowed", { status: 405 });
|
|
1149
1859
|
}
|
|
1150
1860
|
|
|
@@ -1189,7 +1899,7 @@ async function handleFetch(req) {
|
|
|
1189
1899
|
}
|
|
1190
1900
|
|
|
1191
1901
|
const renderStart = performance.now();
|
|
1192
|
-
const renderResult = await renderRoute(pathname);
|
|
1902
|
+
const renderResult = await renderRoute(pathname, req);
|
|
1193
1903
|
const renderMs = performance.now() - renderStart;
|
|
1194
1904
|
const total = performance.now() - reqStart;
|
|
1195
1905
|
|
|
@@ -1221,18 +1931,43 @@ async function handleFetch(req) {
|
|
|
1221
1931
|
|
|
1222
1932
|
if (renderResult.redirect) {
|
|
1223
1933
|
logRequest({ method, url: pathname, status: renderResult.status, duration: total, compileMs });
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1934
|
+
const rh = new Headers({ Location: renderResult.redirect });
|
|
1935
|
+
for (const c of renderResult.setCookies || []) rh.append("Set-Cookie", c);
|
|
1936
|
+
return new Response(null, { status: renderResult.status, headers: rh });
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
// Raw Response from a guard/action (RouteControl response, 401, 403, …).
|
|
1940
|
+
if (renderResult.rawResponse instanceof Response) {
|
|
1941
|
+
logRequest({ method, url: pathname, status: renderResult.rawResponse.status, duration: total, compileMs });
|
|
1942
|
+
const r = renderResult.rawResponse;
|
|
1943
|
+
for (const c of renderResult.setCookies || []) r.headers.append("Set-Cookie", c);
|
|
1944
|
+
return r;
|
|
1228
1945
|
}
|
|
1229
1946
|
|
|
1230
1947
|
logRequest({ method, url: pathname, status: 200, duration: total, compileMs });
|
|
1231
1948
|
await scanFontsFromLayouts();
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1949
|
+
// seo.tsx head + metadata head
|
|
1950
|
+
const headExtra = [metadataToHead(renderResult.metadata), renderResult.seoHead || ""].filter(Boolean).join("\n");
|
|
1951
|
+
let doc = await wrapInDocumentAsync({ head: headExtra, body: renderResult.html || "" });
|
|
1952
|
+
// state.ts → window.__SR_STATE__ for client stores
|
|
1953
|
+
if (renderResult.serverState !== undefined) {
|
|
1954
|
+
const json = JSON.stringify(renderResult.serverState).replace(/</g, "\\u003c");
|
|
1955
|
+
doc = doc.replace("</body>", `<script>window.__SR_STATE__=${json}</script>\n</body>`);
|
|
1956
|
+
}
|
|
1957
|
+
if (renderResult.clientPage && renderResult.pageFile) {
|
|
1958
|
+
const src = `/_swift-rust/island.js?p=${encodeURIComponent(renderResult.pageFile)}`;
|
|
1959
|
+
doc = doc.replace("</body>", `<script type="module" src="${src}"></script>\n</body>`);
|
|
1960
|
+
}
|
|
1961
|
+
const headers = new Headers({ "Content-Type": "text/html; charset=utf-8" });
|
|
1962
|
+
for (const c of renderResult.setCookies || []) headers.append("Set-Cookie", c);
|
|
1963
|
+
// config.ts headers
|
|
1964
|
+
for (const [k, v] of Object.entries(renderResult.config?.headers || {})) headers.set(k, String(v));
|
|
1965
|
+
// revalidate.ts → Cache-Control + cache tags
|
|
1966
|
+
const cc = cacheControlFromPlan(renderResult.revalidatePlan);
|
|
1967
|
+
if (cc) headers.set("Cache-Control", cc);
|
|
1968
|
+
const tags = renderResult.revalidatePlan?.tags || renderResult.revalidatePlan?.invalidate;
|
|
1969
|
+
if (Array.isArray(tags) && tags.length) headers.set("x-vercel-cache-tags", tags.join(","));
|
|
1970
|
+
return new Response(doc, { status: 200, headers });
|
|
1236
1971
|
}
|
|
1237
1972
|
|
|
1238
1973
|
async function handleApiRoute(req, segments, method, reqStart) {
|
|
@@ -1309,6 +2044,7 @@ const MIME = {
|
|
|
1309
2044
|
};
|
|
1310
2045
|
|
|
1311
2046
|
await checkAppDir();
|
|
2047
|
+
try { validateRoutingConventions(); } catch {}
|
|
1312
2048
|
await detectNetworkUrls();
|
|
1313
2049
|
logStartupBanner(`http://localhost:${port}`, networkUrls);
|
|
1314
2050
|
logLine([` ${paint("dim", "› setupWatcher…")}`]);
|