({ outputDir }: { outputDir: string })
| 127 | } |
| 128 | |
| 129 | async function prerenderPages({ outputDir }: { outputDir: string }) { |
| 130 | const seen = new Set<string>() |
| 131 | const prerendered = new Set<string>() |
| 132 | const retriesByPath = new Map<string, number>() |
| 133 | const concurrency = startConfig.prerender?.concurrency ?? os.cpus().length |
| 134 | logger.info(`Concurrency: ${concurrency}`) |
| 135 | const queue = new Queue({ concurrency }) |
| 136 | const routerBasePath = joinURL('/', startConfig.router.basepath ?? '') |
| 137 | |
| 138 | // Normalize discovered pages and enforce path-only entries |
| 139 | const routerBaseUrl = new URL(routerBasePath, 'http://localhost') |
| 140 | startConfig.pages = validateAndNormalizePrerenderPages( |
| 141 | startConfig.pages, |
| 142 | routerBaseUrl, |
| 143 | ) |
| 144 | |
| 145 | startConfig.pages.forEach((page) => addCrawlPageTask(page)) |
| 146 | |
| 147 | if (queue.isSettled()) { |
| 148 | logger.info('No pages matched prerender filter; skipping.') |
| 149 | return Array.from(prerendered) |
| 150 | } |
| 151 | |
| 152 | await queue.start() |
| 153 | |
| 154 | return Array.from(prerendered) |
| 155 | |
| 156 | function addCrawlPageTask(page: Page) { |
| 157 | // Was the page already seen? |
| 158 | if (seen.has(page.path)) return |
| 159 | |
| 160 | // Add the page to the seen set |
| 161 | seen.add(page.path) |
| 162 | |
| 163 | if (page.fromCrawl) { |
| 164 | startConfig.pages.push(page) |
| 165 | } |
| 166 | |
| 167 | // If not enabled, skip |
| 168 | if (!(page.prerender?.enabled ?? true)) return |
| 169 | |
| 170 | // If there is a filter link, check if the page should be prerendered |
| 171 | if (startConfig.prerender?.filter && !startConfig.prerender.filter(page)) |
| 172 | return |
| 173 | |
| 174 | // Resolve the merged default and page-specific prerender options |
| 175 | const prerenderOptions = { |
| 176 | ...startConfig.prerender, |
| 177 | ...page.prerender, |
| 178 | } |
| 179 | |
| 180 | // Add the task |
| 181 | queue.add(async () => { |
| 182 | logger.info(`Crawling: ${page.path}`) |
| 183 | const retries = retriesByPath.get(page.path) || 0 |
| 184 | try { |
| 185 | // Fetch the route |
| 186 |
no test coverage detected