@onruntime/next-sitemap 0.8.0 → 0.9.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.
@@ -27,14 +27,55 @@ function _interopNamespace(e) {
27
27
  var childProcess__namespace = /*#__PURE__*/_interopNamespace(childProcess);
28
28
 
29
29
  // src/app/index.ts
30
+ var JS_EXTENSIONS = /* @__PURE__ */ new Set([
31
+ ".js",
32
+ ".cjs",
33
+ ".mjs",
34
+ ".jsx",
35
+ ".ts",
36
+ ".cts",
37
+ ".mts",
38
+ ".tsx",
39
+ ".json",
40
+ ".node"
41
+ ]);
30
42
  var NO_STATIC_PARAMS = "NO_STATIC_PARAMS";
31
43
  var spawnProcess = childProcess__namespace.spawn;
32
44
  var __dirname$1 = path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
33
45
  var paramsCache = /* @__PURE__ */ new Map();
34
46
  var isDev = process.env.NODE_ENV === "development";
47
+ function resolveFilePath(directory, fileKey) {
48
+ if (fileKey.startsWith("./")) {
49
+ return path.join(directory, fileKey.replace("./", ""));
50
+ }
51
+ const basePath = fileKey === "/" ? "/index" : fileKey;
52
+ const jsPath = path.join(directory, basePath + ".js");
53
+ if (fs.existsSync(jsPath)) {
54
+ return jsPath;
55
+ }
56
+ for (const ext of JS_EXTENSIONS) {
57
+ const sourcePath = path.join(directory, basePath + ext);
58
+ if (fs.existsSync(sourcePath)) {
59
+ return sourcePath;
60
+ }
61
+ }
62
+ for (const ext of JS_EXTENSIONS) {
63
+ const indexPath = path.join(directory, basePath, "index" + ext);
64
+ if (fs.existsSync(indexPath)) {
65
+ return indexPath;
66
+ }
67
+ }
68
+ return null;
69
+ }
35
70
  async function executeWorker(directory, fileKey, debug) {
36
- const absolutePath = path.join(directory, fileKey.replace("./", ""));
71
+ const absolutePath = resolveFilePath(directory, fileKey);
37
72
  const projectRoot = process.cwd();
73
+ if (!absolutePath) {
74
+ if (debug) {
75
+ console.warn(`[next-sitemap] Could not resolve file path for ${fileKey} in ${directory}`);
76
+ }
77
+ return null;
78
+ }
38
79
  const distRoot = path.join(__dirname$1, "..");
39
80
  const workerPath = path.join(distRoot, "worker.cjs");
40
81
  const loaderPath = path.join(distRoot, "loader.js");
package/dist/app/index.js CHANGED
@@ -4,14 +4,55 @@ import * as childProcess from 'child_process';
4
4
  import { fileURLToPath } from 'url';
5
5
 
6
6
  // src/app/index.ts
7
+ var JS_EXTENSIONS = /* @__PURE__ */ new Set([
8
+ ".js",
9
+ ".cjs",
10
+ ".mjs",
11
+ ".jsx",
12
+ ".ts",
13
+ ".cts",
14
+ ".mts",
15
+ ".tsx",
16
+ ".json",
17
+ ".node"
18
+ ]);
7
19
  var NO_STATIC_PARAMS = "NO_STATIC_PARAMS";
8
20
  var spawnProcess = childProcess.spawn;
9
21
  var __dirname$1 = dirname(fileURLToPath(import.meta.url));
10
22
  var paramsCache = /* @__PURE__ */ new Map();
11
23
  var isDev = process.env.NODE_ENV === "development";
24
+ function resolveFilePath(directory, fileKey) {
25
+ if (fileKey.startsWith("./")) {
26
+ return join(directory, fileKey.replace("./", ""));
27
+ }
28
+ const basePath = fileKey === "/" ? "/index" : fileKey;
29
+ const jsPath = join(directory, basePath + ".js");
30
+ if (existsSync(jsPath)) {
31
+ return jsPath;
32
+ }
33
+ for (const ext of JS_EXTENSIONS) {
34
+ const sourcePath = join(directory, basePath + ext);
35
+ if (existsSync(sourcePath)) {
36
+ return sourcePath;
37
+ }
38
+ }
39
+ for (const ext of JS_EXTENSIONS) {
40
+ const indexPath = join(directory, basePath, "index" + ext);
41
+ if (existsSync(indexPath)) {
42
+ return indexPath;
43
+ }
44
+ }
45
+ return null;
46
+ }
12
47
  async function executeWorker(directory, fileKey, debug) {
13
- const absolutePath = join(directory, fileKey.replace("./", ""));
48
+ const absolutePath = resolveFilePath(directory, fileKey);
14
49
  const projectRoot = process.cwd();
50
+ if (!absolutePath) {
51
+ if (debug) {
52
+ console.warn(`[next-sitemap] Could not resolve file path for ${fileKey} in ${directory}`);
53
+ }
54
+ return null;
55
+ }
15
56
  const distRoot = join(__dirname$1, "..");
16
57
  const workerPath = join(distRoot, "worker.cjs");
17
58
  const loaderPath = join(distRoot, "loader.js");
package/dist/index.cjs CHANGED
@@ -44,9 +44,38 @@ var spawnProcess = childProcess__namespace.spawn;
44
44
  var __dirname$1 = path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
45
45
  var paramsCache = /* @__PURE__ */ new Map();
46
46
  var isDev = process.env.NODE_ENV === "development";
47
+ function resolveFilePath(directory, fileKey) {
48
+ if (fileKey.startsWith("./")) {
49
+ return path.join(directory, fileKey.replace("./", ""));
50
+ }
51
+ const basePath = fileKey === "/" ? "/index" : fileKey;
52
+ const jsPath = path.join(directory, basePath + ".js");
53
+ if (fs.existsSync(jsPath)) {
54
+ return jsPath;
55
+ }
56
+ for (const ext of JS_EXTENSIONS) {
57
+ const sourcePath = path.join(directory, basePath + ext);
58
+ if (fs.existsSync(sourcePath)) {
59
+ return sourcePath;
60
+ }
61
+ }
62
+ for (const ext of JS_EXTENSIONS) {
63
+ const indexPath = path.join(directory, basePath, "index" + ext);
64
+ if (fs.existsSync(indexPath)) {
65
+ return indexPath;
66
+ }
67
+ }
68
+ return null;
69
+ }
47
70
  async function executeWorker(directory, fileKey, debug) {
48
- const absolutePath = path.join(directory, fileKey.replace("./", ""));
71
+ const absolutePath = resolveFilePath(directory, fileKey);
49
72
  const projectRoot = process.cwd();
73
+ if (!absolutePath) {
74
+ if (debug) {
75
+ console.warn(`[next-sitemap] Could not resolve file path for ${fileKey} in ${directory}`);
76
+ }
77
+ return null;
78
+ }
50
79
  const distRoot = path.join(__dirname$1, "..");
51
80
  const workerPath = path.join(distRoot, "worker.cjs");
52
81
  const loaderPath = path.join(distRoot, "loader.js");
package/dist/index.js CHANGED
@@ -21,9 +21,38 @@ var spawnProcess = childProcess.spawn;
21
21
  var __dirname$1 = dirname(fileURLToPath(import.meta.url));
22
22
  var paramsCache = /* @__PURE__ */ new Map();
23
23
  var isDev = process.env.NODE_ENV === "development";
24
+ function resolveFilePath(directory, fileKey) {
25
+ if (fileKey.startsWith("./")) {
26
+ return join(directory, fileKey.replace("./", ""));
27
+ }
28
+ const basePath = fileKey === "/" ? "/index" : fileKey;
29
+ const jsPath = join(directory, basePath + ".js");
30
+ if (existsSync(jsPath)) {
31
+ return jsPath;
32
+ }
33
+ for (const ext of JS_EXTENSIONS) {
34
+ const sourcePath = join(directory, basePath + ext);
35
+ if (existsSync(sourcePath)) {
36
+ return sourcePath;
37
+ }
38
+ }
39
+ for (const ext of JS_EXTENSIONS) {
40
+ const indexPath = join(directory, basePath, "index" + ext);
41
+ if (existsSync(indexPath)) {
42
+ return indexPath;
43
+ }
44
+ }
45
+ return null;
46
+ }
24
47
  async function executeWorker(directory, fileKey, debug) {
25
- const absolutePath = join(directory, fileKey.replace("./", ""));
48
+ const absolutePath = resolveFilePath(directory, fileKey);
26
49
  const projectRoot = process.cwd();
50
+ if (!absolutePath) {
51
+ if (debug) {
52
+ console.warn(`[next-sitemap] Could not resolve file path for ${fileKey} in ${directory}`);
53
+ }
54
+ return null;
55
+ }
27
56
  const distRoot = join(__dirname$1, "..");
28
57
  const workerPath = join(distRoot, "worker.cjs");
29
58
  const loaderPath = join(distRoot, "loader.js");
@@ -27,14 +27,55 @@ function _interopNamespace(e) {
27
27
  var childProcess__namespace = /*#__PURE__*/_interopNamespace(childProcess);
28
28
 
29
29
  // src/pages/index.ts
30
+ var JS_EXTENSIONS = /* @__PURE__ */ new Set([
31
+ ".js",
32
+ ".cjs",
33
+ ".mjs",
34
+ ".jsx",
35
+ ".ts",
36
+ ".cts",
37
+ ".mts",
38
+ ".tsx",
39
+ ".json",
40
+ ".node"
41
+ ]);
30
42
  var NO_STATIC_PARAMS = "NO_STATIC_PARAMS";
31
43
  var spawnProcess = childProcess__namespace.spawn;
32
44
  var __dirname$1 = path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
33
45
  var paramsCache = /* @__PURE__ */ new Map();
34
46
  var isDev = process.env.NODE_ENV === "development";
47
+ function resolveFilePath(directory, fileKey) {
48
+ if (fileKey.startsWith("./")) {
49
+ return path.join(directory, fileKey.replace("./", ""));
50
+ }
51
+ const basePath = fileKey === "/" ? "/index" : fileKey;
52
+ const jsPath = path.join(directory, basePath + ".js");
53
+ if (fs.existsSync(jsPath)) {
54
+ return jsPath;
55
+ }
56
+ for (const ext of JS_EXTENSIONS) {
57
+ const sourcePath = path.join(directory, basePath + ext);
58
+ if (fs.existsSync(sourcePath)) {
59
+ return sourcePath;
60
+ }
61
+ }
62
+ for (const ext of JS_EXTENSIONS) {
63
+ const indexPath = path.join(directory, basePath, "index" + ext);
64
+ if (fs.existsSync(indexPath)) {
65
+ return indexPath;
66
+ }
67
+ }
68
+ return null;
69
+ }
35
70
  async function executeWorker(directory, fileKey, debug) {
36
- const absolutePath = path.join(directory, fileKey.replace("./", ""));
71
+ const absolutePath = resolveFilePath(directory, fileKey);
37
72
  const projectRoot = process.cwd();
73
+ if (!absolutePath) {
74
+ if (debug) {
75
+ console.warn(`[next-sitemap] Could not resolve file path for ${fileKey} in ${directory}`);
76
+ }
77
+ return null;
78
+ }
38
79
  const distRoot = path.join(__dirname$1, "..");
39
80
  const workerPath = path.join(distRoot, "worker.cjs");
40
81
  const loaderPath = path.join(distRoot, "loader.js");
@@ -334,6 +375,46 @@ function generateRobotsTxt(config) {
334
375
  }
335
376
 
336
377
  // src/pages/index.ts
378
+ function readRoutesManifest(debug) {
379
+ const manifestPath = path.join(process.cwd(), ".next", "routes-manifest.json");
380
+ if (!fs.existsSync(manifestPath)) {
381
+ if (debug) {
382
+ console.log(`[next-sitemap] routes-manifest.json not found at ${manifestPath}`);
383
+ }
384
+ return null;
385
+ }
386
+ try {
387
+ const content = fs.readFileSync(manifestPath, "utf-8");
388
+ return JSON.parse(content);
389
+ } catch (err) {
390
+ if (debug) {
391
+ console.warn(`[next-sitemap] Failed to read routes-manifest.json:`, err);
392
+ }
393
+ return null;
394
+ }
395
+ }
396
+ function extractRoutesFromManifest(manifest) {
397
+ const routes = [];
398
+ for (const route of manifest.staticRoutes) {
399
+ if (route.page.startsWith("/api/")) continue;
400
+ routes.push({
401
+ pathname: route.page,
402
+ dynamicSegments: [],
403
+ fileKey: route.page
404
+ });
405
+ }
406
+ for (const route of manifest.dynamicRoutes) {
407
+ if (route.page.startsWith("/api/")) continue;
408
+ if (route.page.includes("[...")) continue;
409
+ const dynamicSegments = route.page.match(/\[([^\]]+)\]/g)?.map((s) => s.slice(1, -1)) ?? [];
410
+ routes.push({
411
+ pathname: route.page,
412
+ dynamicSegments,
413
+ fileKey: route.page
414
+ });
415
+ }
416
+ return routes;
417
+ }
337
418
  function findPageFiles(dir, baseDir = dir) {
338
419
  const files = [];
339
420
  try {
@@ -359,7 +440,7 @@ function resolvePagesDirectory(pagesDirectory) {
359
440
  const srcPages = path.join(process.cwd(), "src/pages");
360
441
  return fs.existsSync(srcPages) ? srcPages : path.join(process.cwd(), "pages");
361
442
  }
362
- function extractRoutes(pageKeys, localeSegment) {
443
+ function extractRoutesFromFiles(pageKeys, localeSegment) {
363
444
  const routes = [];
364
445
  for (const key of pageKeys) {
365
446
  if (key.includes("[...")) continue;
@@ -380,17 +461,39 @@ function extractRoutes(pageKeys, localeSegment) {
380
461
  }
381
462
  return routes;
382
463
  }
464
+ function getRoutes(pagesDirectory, localeSegment, debug) {
465
+ const manifest = readRoutesManifest(debug);
466
+ if (manifest) {
467
+ if (debug) {
468
+ console.log(`[next-sitemap] Using routes-manifest.json for route discovery`);
469
+ }
470
+ return extractRoutesFromManifest(manifest);
471
+ }
472
+ if (debug) {
473
+ console.log(`[next-sitemap] Falling back to file scanning for route discovery`);
474
+ }
475
+ const pagesDir = resolvePagesDirectory(pagesDirectory);
476
+ const pageKeys = findPageFiles(pagesDir);
477
+ return extractRoutesFromFiles(pageKeys, localeSegment);
478
+ }
479
+ function getWorkerDirectory(pagesDirectory) {
480
+ const compiledDir = path.join(process.cwd(), ".next", "server", "pages");
481
+ if (fs.existsSync(compiledDir)) {
482
+ return compiledDir;
483
+ }
484
+ return resolvePagesDirectory(pagesDirectory);
485
+ }
383
486
  function createSitemapIndexApiHandler(options) {
384
487
  const { urlsPerSitemap = 5e3, additionalSitemaps, exclude, debug = false } = options;
385
488
  const localeSegment = options.localeSegment ?? "";
386
- const pagesDir = resolvePagesDirectory(options.pagesDirectory);
387
- const pageKeys = findPageFiles(pagesDir);
388
- const routes = extractRoutes(pageKeys, localeSegment);
389
489
  return async function handler(_req, res) {
490
+ const routes = getRoutes(options.pagesDirectory, localeSegment, debug);
491
+ const workerDir = getWorkerDirectory(options.pagesDirectory);
390
492
  if (debug) {
391
493
  console.log(`[next-sitemap] Found ${routes.length} routes`);
494
+ console.log(`[next-sitemap] Worker directory: ${workerDir}`);
392
495
  }
393
- const allPaths = await generateAllPaths(routes, pagesDir, debug);
496
+ const allPaths = await generateAllPaths(routes, workerDir, debug);
394
497
  const filteredPaths = allPaths.filter((p) => !shouldExclude(p, exclude));
395
498
  const sitemapCount = Math.max(1, Math.ceil(filteredPaths.length / urlsPerSitemap));
396
499
  res.setHeader("Content-Type", "application/xml");
@@ -402,11 +505,10 @@ function createSitemapIndexApiHandler(options) {
402
505
  function createSitemapApiHandler(options) {
403
506
  const { urlsPerSitemap = 5e3, exclude, debug = false } = options;
404
507
  const localeSegment = options.localeSegment ?? "";
405
- const pagesDir = resolvePagesDirectory(options.pagesDirectory);
406
- const pageKeys = findPageFiles(pagesDir);
407
- const routes = extractRoutes(pageKeys, localeSegment);
408
508
  const getFilteredPaths = async () => {
409
- const allPaths = await generateAllPaths(routes, pagesDir, debug);
509
+ const routes = getRoutes(options.pagesDirectory, localeSegment, debug);
510
+ const workerDir = getWorkerDirectory(options.pagesDirectory);
511
+ const allPaths = await generateAllPaths(routes, workerDir, debug);
410
512
  return allPaths.filter((p) => !shouldExclude(p, exclude));
411
513
  };
412
514
  return async function handler(req, res) {
@@ -422,10 +524,9 @@ function createSitemapApiHandler(options) {
422
524
  async function getSitemapStaticPaths(options) {
423
525
  const { urlsPerSitemap = 5e3, exclude, debug = false } = options;
424
526
  const localeSegment = options.localeSegment ?? "";
425
- const pagesDir = resolvePagesDirectory(options.pagesDirectory);
426
- const pageKeys = findPageFiles(pagesDir);
427
- const routes = extractRoutes(pageKeys, localeSegment);
428
- const allPaths = await generateAllPaths(routes, pagesDir, debug);
527
+ const routes = getRoutes(options.pagesDirectory, localeSegment, debug);
528
+ const workerDir = getWorkerDirectory(options.pagesDirectory);
529
+ const allPaths = await generateAllPaths(routes, workerDir, debug);
429
530
  const filteredPaths = allPaths.filter((pathname) => !shouldExclude(pathname, exclude));
430
531
  const sitemapCount = Math.max(1, Math.ceil(filteredPaths.length / urlsPerSitemap));
431
532
  return {
@@ -94,6 +94,7 @@ interface CreateSitemapApiHandlerOptions extends SitemapConfig {
94
94
  * Path to the pages directory to scan for page files.
95
95
  * Can be absolute or relative to process.cwd().
96
96
  * If not provided, auto-detects src/pages or pages.
97
+ * @deprecated Use routes-manifest.json from .next directory instead
97
98
  */
98
99
  pagesDirectory?: string;
99
100
  }
@@ -94,6 +94,7 @@ interface CreateSitemapApiHandlerOptions extends SitemapConfig {
94
94
  * Path to the pages directory to scan for page files.
95
95
  * Can be absolute or relative to process.cwd().
96
96
  * If not provided, auto-detects src/pages or pages.
97
+ * @deprecated Use routes-manifest.json from .next directory instead
97
98
  */
98
99
  pagesDirectory?: string;
99
100
  }
@@ -1,17 +1,58 @@
1
- import { existsSync, readdirSync } from 'fs';
1
+ import { existsSync, readFileSync, readdirSync } from 'fs';
2
2
  import { dirname, join, isAbsolute, relative, delimiter } from 'path';
3
3
  import * as childProcess from 'child_process';
4
4
  import { fileURLToPath } from 'url';
5
5
 
6
6
  // src/pages/index.ts
7
+ var JS_EXTENSIONS = /* @__PURE__ */ new Set([
8
+ ".js",
9
+ ".cjs",
10
+ ".mjs",
11
+ ".jsx",
12
+ ".ts",
13
+ ".cts",
14
+ ".mts",
15
+ ".tsx",
16
+ ".json",
17
+ ".node"
18
+ ]);
7
19
  var NO_STATIC_PARAMS = "NO_STATIC_PARAMS";
8
20
  var spawnProcess = childProcess.spawn;
9
21
  var __dirname$1 = dirname(fileURLToPath(import.meta.url));
10
22
  var paramsCache = /* @__PURE__ */ new Map();
11
23
  var isDev = process.env.NODE_ENV === "development";
24
+ function resolveFilePath(directory, fileKey) {
25
+ if (fileKey.startsWith("./")) {
26
+ return join(directory, fileKey.replace("./", ""));
27
+ }
28
+ const basePath = fileKey === "/" ? "/index" : fileKey;
29
+ const jsPath = join(directory, basePath + ".js");
30
+ if (existsSync(jsPath)) {
31
+ return jsPath;
32
+ }
33
+ for (const ext of JS_EXTENSIONS) {
34
+ const sourcePath = join(directory, basePath + ext);
35
+ if (existsSync(sourcePath)) {
36
+ return sourcePath;
37
+ }
38
+ }
39
+ for (const ext of JS_EXTENSIONS) {
40
+ const indexPath = join(directory, basePath, "index" + ext);
41
+ if (existsSync(indexPath)) {
42
+ return indexPath;
43
+ }
44
+ }
45
+ return null;
46
+ }
12
47
  async function executeWorker(directory, fileKey, debug) {
13
- const absolutePath = join(directory, fileKey.replace("./", ""));
48
+ const absolutePath = resolveFilePath(directory, fileKey);
14
49
  const projectRoot = process.cwd();
50
+ if (!absolutePath) {
51
+ if (debug) {
52
+ console.warn(`[next-sitemap] Could not resolve file path for ${fileKey} in ${directory}`);
53
+ }
54
+ return null;
55
+ }
15
56
  const distRoot = join(__dirname$1, "..");
16
57
  const workerPath = join(distRoot, "worker.cjs");
17
58
  const loaderPath = join(distRoot, "loader.js");
@@ -311,6 +352,46 @@ function generateRobotsTxt(config) {
311
352
  }
312
353
 
313
354
  // src/pages/index.ts
355
+ function readRoutesManifest(debug) {
356
+ const manifestPath = join(process.cwd(), ".next", "routes-manifest.json");
357
+ if (!existsSync(manifestPath)) {
358
+ if (debug) {
359
+ console.log(`[next-sitemap] routes-manifest.json not found at ${manifestPath}`);
360
+ }
361
+ return null;
362
+ }
363
+ try {
364
+ const content = readFileSync(manifestPath, "utf-8");
365
+ return JSON.parse(content);
366
+ } catch (err) {
367
+ if (debug) {
368
+ console.warn(`[next-sitemap] Failed to read routes-manifest.json:`, err);
369
+ }
370
+ return null;
371
+ }
372
+ }
373
+ function extractRoutesFromManifest(manifest) {
374
+ const routes = [];
375
+ for (const route of manifest.staticRoutes) {
376
+ if (route.page.startsWith("/api/")) continue;
377
+ routes.push({
378
+ pathname: route.page,
379
+ dynamicSegments: [],
380
+ fileKey: route.page
381
+ });
382
+ }
383
+ for (const route of manifest.dynamicRoutes) {
384
+ if (route.page.startsWith("/api/")) continue;
385
+ if (route.page.includes("[...")) continue;
386
+ const dynamicSegments = route.page.match(/\[([^\]]+)\]/g)?.map((s) => s.slice(1, -1)) ?? [];
387
+ routes.push({
388
+ pathname: route.page,
389
+ dynamicSegments,
390
+ fileKey: route.page
391
+ });
392
+ }
393
+ return routes;
394
+ }
314
395
  function findPageFiles(dir, baseDir = dir) {
315
396
  const files = [];
316
397
  try {
@@ -336,7 +417,7 @@ function resolvePagesDirectory(pagesDirectory) {
336
417
  const srcPages = join(process.cwd(), "src/pages");
337
418
  return existsSync(srcPages) ? srcPages : join(process.cwd(), "pages");
338
419
  }
339
- function extractRoutes(pageKeys, localeSegment) {
420
+ function extractRoutesFromFiles(pageKeys, localeSegment) {
340
421
  const routes = [];
341
422
  for (const key of pageKeys) {
342
423
  if (key.includes("[...")) continue;
@@ -357,17 +438,39 @@ function extractRoutes(pageKeys, localeSegment) {
357
438
  }
358
439
  return routes;
359
440
  }
441
+ function getRoutes(pagesDirectory, localeSegment, debug) {
442
+ const manifest = readRoutesManifest(debug);
443
+ if (manifest) {
444
+ if (debug) {
445
+ console.log(`[next-sitemap] Using routes-manifest.json for route discovery`);
446
+ }
447
+ return extractRoutesFromManifest(manifest);
448
+ }
449
+ if (debug) {
450
+ console.log(`[next-sitemap] Falling back to file scanning for route discovery`);
451
+ }
452
+ const pagesDir = resolvePagesDirectory(pagesDirectory);
453
+ const pageKeys = findPageFiles(pagesDir);
454
+ return extractRoutesFromFiles(pageKeys, localeSegment);
455
+ }
456
+ function getWorkerDirectory(pagesDirectory) {
457
+ const compiledDir = join(process.cwd(), ".next", "server", "pages");
458
+ if (existsSync(compiledDir)) {
459
+ return compiledDir;
460
+ }
461
+ return resolvePagesDirectory(pagesDirectory);
462
+ }
360
463
  function createSitemapIndexApiHandler(options) {
361
464
  const { urlsPerSitemap = 5e3, additionalSitemaps, exclude, debug = false } = options;
362
465
  const localeSegment = options.localeSegment ?? "";
363
- const pagesDir = resolvePagesDirectory(options.pagesDirectory);
364
- const pageKeys = findPageFiles(pagesDir);
365
- const routes = extractRoutes(pageKeys, localeSegment);
366
466
  return async function handler(_req, res) {
467
+ const routes = getRoutes(options.pagesDirectory, localeSegment, debug);
468
+ const workerDir = getWorkerDirectory(options.pagesDirectory);
367
469
  if (debug) {
368
470
  console.log(`[next-sitemap] Found ${routes.length} routes`);
471
+ console.log(`[next-sitemap] Worker directory: ${workerDir}`);
369
472
  }
370
- const allPaths = await generateAllPaths(routes, pagesDir, debug);
473
+ const allPaths = await generateAllPaths(routes, workerDir, debug);
371
474
  const filteredPaths = allPaths.filter((p) => !shouldExclude(p, exclude));
372
475
  const sitemapCount = Math.max(1, Math.ceil(filteredPaths.length / urlsPerSitemap));
373
476
  res.setHeader("Content-Type", "application/xml");
@@ -379,11 +482,10 @@ function createSitemapIndexApiHandler(options) {
379
482
  function createSitemapApiHandler(options) {
380
483
  const { urlsPerSitemap = 5e3, exclude, debug = false } = options;
381
484
  const localeSegment = options.localeSegment ?? "";
382
- const pagesDir = resolvePagesDirectory(options.pagesDirectory);
383
- const pageKeys = findPageFiles(pagesDir);
384
- const routes = extractRoutes(pageKeys, localeSegment);
385
485
  const getFilteredPaths = async () => {
386
- const allPaths = await generateAllPaths(routes, pagesDir, debug);
486
+ const routes = getRoutes(options.pagesDirectory, localeSegment, debug);
487
+ const workerDir = getWorkerDirectory(options.pagesDirectory);
488
+ const allPaths = await generateAllPaths(routes, workerDir, debug);
387
489
  return allPaths.filter((p) => !shouldExclude(p, exclude));
388
490
  };
389
491
  return async function handler(req, res) {
@@ -399,10 +501,9 @@ function createSitemapApiHandler(options) {
399
501
  async function getSitemapStaticPaths(options) {
400
502
  const { urlsPerSitemap = 5e3, exclude, debug = false } = options;
401
503
  const localeSegment = options.localeSegment ?? "";
402
- const pagesDir = resolvePagesDirectory(options.pagesDirectory);
403
- const pageKeys = findPageFiles(pagesDir);
404
- const routes = extractRoutes(pageKeys, localeSegment);
405
- const allPaths = await generateAllPaths(routes, pagesDir, debug);
504
+ const routes = getRoutes(options.pagesDirectory, localeSegment, debug);
505
+ const workerDir = getWorkerDirectory(options.pagesDirectory);
506
+ const allPaths = await generateAllPaths(routes, workerDir, debug);
406
507
  const filteredPaths = allPaths.filter((pathname) => !shouldExclude(pathname, exclude));
407
508
  const sitemapCount = Math.max(1, Math.ceil(filteredPaths.length / urlsPerSitemap));
408
509
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onruntime/next-sitemap",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Dynamic sitemap generation for Next.js with automatic route discovery",
5
5
  "author": "onRuntime Studio <contact@onruntime.com>",
6
6
  "repository": {