almostnode 0.2.2 → 0.2.4

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/dist/index.mjs CHANGED
@@ -12554,6 +12554,12 @@ export default function Head({ children }) {
12554
12554
  if (pathname.startsWith("/_next/shims/")) {
12555
12555
  return this.serveNextShim(pathname);
12556
12556
  }
12557
+ if (pathname.startsWith("/_next/pages/")) {
12558
+ return this.servePageComponent(pathname);
12559
+ }
12560
+ if (pathname.startsWith("/_next/app/")) {
12561
+ return this.serveAppComponent(pathname);
12562
+ }
12557
12563
  if (pathname.startsWith("/_next/static/")) {
12558
12564
  return this.serveStaticAsset(pathname);
12559
12565
  }
@@ -12610,6 +12616,30 @@ export default function Head({ children }) {
12610
12616
  }
12611
12617
  return this.notFound(pathname);
12612
12618
  }
12619
+ async servePageComponent(pathname) {
12620
+ const route = pathname.replace("/_next/pages", "").replace(/\.js$/, "");
12621
+ const pageFile = this.resolvePageFile(route);
12622
+ if (!pageFile) {
12623
+ return this.notFound(pathname);
12624
+ }
12625
+ return this.transformAndServe(pageFile, pageFile);
12626
+ }
12627
+ async serveAppComponent(pathname) {
12628
+ const filePath = pathname.replace("/_next/app", "").replace(/\.js$/, "");
12629
+ const extensions = [
12630
+ ".tsx",
12631
+ ".jsx",
12632
+ ".ts",
12633
+ ".js"
12634
+ ];
12635
+ for (const ext of extensions) {
12636
+ const fullPath = filePath + ext;
12637
+ if (this.exists(fullPath)) {
12638
+ return this.transformAndServe(fullPath, fullPath);
12639
+ }
12640
+ }
12641
+ return this.notFound(pathname);
12642
+ }
12613
12643
  async handleApiRoute(method, pathname, headers, body) {
12614
12644
  const apiFile = this.resolveApiFile(pathname);
12615
12645
  if (!apiFile) {
@@ -13166,11 +13196,9 @@ export default function Head({ children }) {
13166
13196
  globalCssLinks.push(`<link rel="stylesheet" href="${virtualPrefix}${cssPath}">`);
13167
13197
  }
13168
13198
  }
13169
- const pageModulePath = virtualPrefix + route.page;
13170
- const layoutImports = route.layouts.map((layout, i) => `import Layout${i} from '${virtualPrefix}${layout}';`).join("\n ");
13171
- let nestedJsx = "React.createElement(Page)";
13199
+ virtualPrefix + route.page;
13200
+ route.layouts.map((layout, i) => `import Layout${i} from '${virtualPrefix}${layout}';`).join("\n ");
13172
13201
  for (let i = route.layouts.length - 1; i >= 0; i--) {
13173
- nestedJsx = `React.createElement(Layout${i}, null, ${nestedJsx})`;
13174
13202
  }
13175
13203
  const envScript = this.generateEnvScript();
13176
13204
  return `<!DOCTYPE html>
@@ -13214,17 +13242,117 @@ export default function Head({ children }) {
13214
13242
  <script type="module">
13215
13243
  import React from 'react';
13216
13244
  import ReactDOM from 'react-dom/client';
13217
- import Page from '${pageModulePath}';
13218
- ${layoutImports}
13219
13245
 
13220
- function App() {
13221
- return ${nestedJsx};
13246
+ const virtualBase = '${virtualPrefix}';
13247
+
13248
+ // Convert URL path to app router page module path
13249
+ function getAppPageModulePath(pathname) {
13250
+ let route = pathname;
13251
+ if (route.startsWith(virtualBase)) {
13252
+ route = route.slice(virtualBase.length);
13253
+ }
13254
+ route = route.replace(/^\\/+/, '/') || '/';
13255
+ // App Router: / -> /app/page, /about -> /app/about/page
13256
+ const pagePath = route === '/' ? '/app/page' : '/app' + route + '/page';
13257
+ return virtualBase + '/_next/app' + pagePath + '.js';
13258
+ }
13259
+
13260
+ // Get layout paths for a route
13261
+ function getLayoutPaths(pathname) {
13262
+ let route = pathname;
13263
+ if (route.startsWith(virtualBase)) {
13264
+ route = route.slice(virtualBase.length);
13265
+ }
13266
+ route = route.replace(/^\\/+/, '/') || '/';
13267
+
13268
+ // Build layout paths from root to current route
13269
+ const layouts = [virtualBase + '/_next/app/app/layout.js'];
13270
+ if (route !== '/') {
13271
+ const segments = route.split('/').filter(Boolean);
13272
+ let currentPath = '/app';
13273
+ for (const segment of segments) {
13274
+ currentPath += '/' + segment;
13275
+ layouts.push(virtualBase + '/_next/app' + currentPath + '/layout.js');
13276
+ }
13277
+ }
13278
+ return layouts;
13279
+ }
13280
+
13281
+ // Dynamic page loader
13282
+ async function loadPage(pathname) {
13283
+ const modulePath = getAppPageModulePath(pathname);
13284
+ try {
13285
+ const module = await import(/* @vite-ignore */ modulePath);
13286
+ return module.default;
13287
+ } catch (e) {
13288
+ console.error('[Navigation] Failed to load page:', modulePath, e);
13289
+ return null;
13290
+ }
13291
+ }
13292
+
13293
+ // Load layouts (with caching)
13294
+ const layoutCache = new Map();
13295
+ async function loadLayouts(pathname) {
13296
+ const layoutPaths = getLayoutPaths(pathname);
13297
+ const layouts = [];
13298
+ for (const path of layoutPaths) {
13299
+ if (layoutCache.has(path)) {
13300
+ layouts.push(layoutCache.get(path));
13301
+ } else {
13302
+ try {
13303
+ const module = await import(/* @vite-ignore */ path);
13304
+ layoutCache.set(path, module.default);
13305
+ layouts.push(module.default);
13306
+ } catch (e) {
13307
+ // Layout might not exist for this segment, skip
13308
+ }
13309
+ }
13310
+ }
13311
+ return layouts;
13312
+ }
13313
+
13314
+ // Router component
13315
+ function Router() {
13316
+ const [Page, setPage] = React.useState(null);
13317
+ const [layouts, setLayouts] = React.useState([]);
13318
+ const [path, setPath] = React.useState(window.location.pathname);
13319
+
13320
+ React.useEffect(() => {
13321
+ Promise.all([loadPage(path), loadLayouts(path)]).then(([P, L]) => {
13322
+ if (P) setPage(() => P);
13323
+ setLayouts(L);
13324
+ });
13325
+ }, []);
13326
+
13327
+ React.useEffect(() => {
13328
+ const handleNavigation = async () => {
13329
+ const newPath = window.location.pathname;
13330
+ if (newPath !== path) {
13331
+ setPath(newPath);
13332
+ const [P, L] = await Promise.all([loadPage(newPath), loadLayouts(newPath)]);
13333
+ if (P) setPage(() => P);
13334
+ setLayouts(L);
13335
+ }
13336
+ };
13337
+ window.addEventListener('popstate', handleNavigation);
13338
+ return () => window.removeEventListener('popstate', handleNavigation);
13339
+ }, [path]);
13340
+
13341
+ if (!Page) return null;
13342
+
13343
+ // Build nested layout structure
13344
+ let content = React.createElement(Page);
13345
+ for (let i = layouts.length - 1; i >= 0; i--) {
13346
+ content = React.createElement(layouts[i], null, content);
13347
+ }
13348
+ return content;
13222
13349
  }
13223
13350
 
13351
+ // Mark that we've initialized (for testing no-reload)
13352
+ window.__NEXT_INITIALIZED__ = Date.now();
13353
+
13224
13354
  ReactDOM.createRoot(document.getElementById('__next')).render(
13225
- React.createElement(React.StrictMode, null,
13226
- React.createElement(App)
13227
- )
13355
+ React.createElement(React.StrictMode, null, React.createElement(Router))
13228
13356
  );
13229
13357
  <\/script>
13230
13358
  </body>
@@ -13323,7 +13451,6 @@ export default function Head({ children }) {
13323
13451
  }
13324
13452
  async generatePageHtml(pageFile, pathname) {
13325
13453
  const virtualPrefix = `/__virtual__/${this.port}`;
13326
- const pageModulePath = virtualPrefix + pageFile;
13327
13454
  const globalCssLinks = [];
13328
13455
  const cssLocations = [
13329
13456
  "/styles/globals.css",
@@ -13369,30 +13496,63 @@ export default function Head({ children }) {
13369
13496
  <script type="module">
13370
13497
  import React from 'react';
13371
13498
  import ReactDOM from 'react-dom/client';
13372
- import Page from '${pageModulePath}';
13373
13499
 
13374
- // Handle client-side navigation
13375
- function App() {
13376
- const [currentPath, setCurrentPath] = React.useState(window.location.pathname);
13500
+ const virtualBase = '${virtualPrefix}';
13377
13501
 
13378
- React.useEffect(() => {
13379
- const handlePopState = () => {
13380
- setCurrentPath(window.location.pathname);
13381
- // Defer reload outside React's update cycle
13382
- setTimeout(() => window.location.reload(), 0);
13383
- };
13502
+ // Convert URL path to page module path
13503
+ function getPageModulePath(pathname) {
13504
+ let route = pathname;
13505
+ if (route.startsWith(virtualBase)) {
13506
+ route = route.slice(virtualBase.length);
13507
+ }
13508
+ route = route.replace(/^\\/+/, '/') || '/';
13509
+ const modulePath = route === '/' ? '/index' : route;
13510
+ return virtualBase + '/_next/pages' + modulePath + '.js';
13511
+ }
13384
13512
 
13385
- window.addEventListener('popstate', handlePopState);
13386
- return () => window.removeEventListener('popstate', handlePopState);
13513
+ // Dynamic page loader
13514
+ async function loadPage(pathname) {
13515
+ const modulePath = getPageModulePath(pathname);
13516
+ try {
13517
+ const module = await import(/* @vite-ignore */ modulePath);
13518
+ return module.default;
13519
+ } catch (e) {
13520
+ console.error('[Navigation] Failed to load:', modulePath, e);
13521
+ return null;
13522
+ }
13523
+ }
13524
+
13525
+ // Router component
13526
+ function Router() {
13527
+ const [Page, setPage] = React.useState(null);
13528
+ const [path, setPath] = React.useState(window.location.pathname);
13529
+
13530
+ React.useEffect(() => {
13531
+ loadPage(path).then(C => C && setPage(() => C));
13387
13532
  }, []);
13388
13533
 
13534
+ React.useEffect(() => {
13535
+ const handleNavigation = async () => {
13536
+ const newPath = window.location.pathname;
13537
+ if (newPath !== path) {
13538
+ setPath(newPath);
13539
+ const C = await loadPage(newPath);
13540
+ if (C) setPage(() => C);
13541
+ }
13542
+ };
13543
+ window.addEventListener('popstate', handleNavigation);
13544
+ return () => window.removeEventListener('popstate', handleNavigation);
13545
+ }, [path]);
13546
+
13547
+ if (!Page) return null;
13389
13548
  return React.createElement(Page);
13390
13549
  }
13391
13550
 
13551
+ // Mark that we've initialized (for testing no-reload)
13552
+ window.__NEXT_INITIALIZED__ = Date.now();
13553
+
13392
13554
  ReactDOM.createRoot(document.getElementById('__next')).render(
13393
- React.createElement(React.StrictMode, null,
13394
- React.createElement(App)
13395
- )
13555
+ React.createElement(React.StrictMode, null, React.createElement(Router))
13396
13556
  );
13397
13557
  <\/script>
13398
13558
  </body>