UNPKG

next-runtime-sitemap

Version:

Generates a sitemap for nextjs apps at runtime, using a crawl of the local filesystem

70 lines (69 loc) 3.62 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.introspectPages = introspectPages; const node_path_1 = __importDefault(require("node:path")); const promises_1 = require("fs/promises"); const introspect_file_1 = require("./introspect-file"); const types_1 = require("next-dynamic-sitemap/dist/types"); const util_1 = require("next-dynamic-sitemap/dist/util"); const JSON_RE = /(?:\.js\.nft)?\.json$/; const FILE_SUFFIX_RE = /(?:(?:^|\/)?index)?(?:\.js\.nft)?\.json$/; function deLocalizeFiles(files, locale) { const delocalizedFiles = []; const localizedRegex = new RegExp(`^${locale}/`); for (let file of files) { if (localizedRegex.test(file.path)) { delocalizedFiles.push(Object.assign(Object.assign({}, file), { path: file.path.replace(localizedRegex, "") })); } else if (!file.path) { delocalizedFiles.push(Object.assign(Object.assign({}, file), { path: `${locale}/` })); } } return delocalizedFiles; } function introspectPages(cwd, props) { return __awaiter(this, void 0, void 0, function* () { const pagesDir = node_path_1.default.join(cwd, "pages"); const files = yield (0, promises_1.readdir)(pagesDir, { recursive: true }); const allFiles = new Set(files); if ((props === null || props === void 0 ? void 0 : props.defaultLocale) && allFiles.has(`${props.defaultLocale}.html`)) { allFiles.add("index.html"); } const filteredFiles = files .filter((f) => JSON_RE.test(f)) // include everything with a manifest .filter((f) => !f.includes("[")) // exclude dynamic paths .filter((f) => allFiles.has(f.replace(JSON_RE, ".html"))); // ensure we're only including webpages const filesParsed = yield Promise.all(filteredFiles.map((f) => (0, introspect_file_1.introspectFile)(pagesDir, f, FILE_SUFFIX_RE))); const filesStatusFiltered = filesParsed.filter(f => { const value = typeof f.status === "number" && f.status >= 200 && f.status < 400; if (!value) { console.error("not indexing file due to invalid status code", f); } return value; }); if (props === null || props === void 0 ? void 0 : props.defaultLocale) { filesStatusFiltered.push(...deLocalizeFiles(filesStatusFiltered, props.defaultLocale)); } return filesStatusFiltered.map((f) => { const url = { changefreq: types_1.ChangeFreq.HOURLY, lastmod: f.stats && new Date(f.stats.mtime).toISOString(), loc: (0, util_1.generateURL)(f.path), priority: (f.path && 0.8) || 1, }; return url; }); }); }