Open History to see the last commit
1import { existsSync, promises as fsp, rmSync } from 'node:fs'
2import { pathToFileURL } from 'node:url'
3import os from 'node:os'
4import path from 'pathe'
5import { joinURL, withBase, withoutBase } from 'ufo'
6import { VITE_ENVIRONMENT_NAMES } from './constants'
7import { createLogger } from './utils'
8import { Queue } from './queue'
9import type { Rollup, ViteBuilder } from 'vite'
10import type { Page, TanStackStartOutputConfig } from './schema'
11β
12export async function prerender({
13 startConfig,
14 builder,
15 serverBundle,
16}: {
17 startConfig: TanStackStartOutputConfig
18 builder: ViteBuilder
19 serverBundle: Rollup.OutputBundle
20}) {
21 const logger = createLogger('prerender')
22 logger.info('Prerendering pages...')
23β
24 // If prerender is enabled but no pages are provided, default to prerendering the root page
25 if (startConfig.prerender?.enabled && !startConfig.pages.length) {
26 startConfig.pages = [
27 {
28 path: '/',
29 },
30 ]
31 }
32β
33 const serverEnv = builder.environments[VITE_ENVIRONMENT_NAMES.server]
34β
35 if (!serverEnv) {
36 throw new Error(
37 `Vite's "${VITE_ENVIRONMENT_NAMES.server}" environment not found`,
38 )
39 }
40β
41 const clientEnv = builder.environments[VITE_ENVIRONMENT_NAMES.client]
42 if (!clientEnv) {
43 throw new Error(
44 `Vite's "${VITE_ENVIRONMENT_NAMES.client}" environment not found`,
45 )
46 }
47β
48 const outputDir = clientEnv.config.build.outDir
49β
50 const entryFile = findEntryFileInBundle(serverBundle)
51 let fullEntryFilePath = path.join(serverEnv.config.build.outDir, entryFile)
52 process.env.TSS_PRERENDERING = 'true'
53β
54 if (!existsSync(fullEntryFilePath)) {
55 // if the file does not exist, we need to write the bundle to a temporary directory
56 // this can happen e.g. with nitro that postprocesses the bundle and thus does not write SSR build to disk
57 const bundleOutputDir = path.resolve(
58 serverEnv.config.root,
59 '.tanstack',
60 'start',
61 'prerender',
62 )
63 rmSync(bundleOutputDir, { recursive: true, force: true })
64 await writeBundleToDisk({ bundle: serverBundle, outDir: bundleOutputDir })
65 fullEntryFilePath = path.join(bundleOutputDir, entryFile)
66 }
67β
68 const { default: serverEntrypoint } = await import(
69 pathToFileURL(fullEntryFilePath).toString()
70 )
71β
72 function localFetch(path: string, options?: RequestInit): Promise<Response> {
73 const url = new URL(`http://localhost${path}`)
74 return serverEntrypoint.fetch(new Request(url, options))
75 }
76β
77 try {
78 // Crawl all pages
79 const pages = await prerenderPages({ outputDir })
80β
81 logger.info(`Prerendered ${pages.length} pages:`)
82 pages.forEach((page) => {
83 logger.info(`- ${page}`)
84 })
85 } catch (error) {
86 logger.error(error)
87 }
88β
89 function extractLinks(html: string): Array<string> {
90 const linkRegex = /<a[^>]+href=["']([^"']+)["'][^>]*>/g
91 const links: Array<string> = []
92 let match
93β
94 while ((match = linkRegex.exec(html)) !== null) {
95 const href = match[1]
96 if (href && (href.startsWith('/') || href.startsWith('./'))) {
97 links.push(href)
98 }
99 }
100β
101 return links
102 }
103β
104 async function prerenderPages({ outputDir }: { outputDir: string }) {
105 const seen = new Set<string>()
106 const retriesByPath = new Map<string, number>()
107 const concurrency = startConfig.prerender?.concurrency ?? os.cpus().length
108 logger.info(`Concurrency: ${concurrency}`)
109 const queue = new Queue({ concurrency })
110β
111 startConfig.pages.forEach((page) => addCrawlPageTask(page))
112β
113 await queue.start()
114β
115 return Array.from(seen)
116β
117 function addCrawlPageTask(page: Page) {
118 // Was the page already seen?
119 if (seen.has(page.path)) return
120β
121 // Add the page to the seen set
122 seen.add(page.path)
123β
124 if (page.fromCrawl) {
125 startConfig.pages.push(page)
126 }
127β
128 // If not enabled, skip
129 if (!(page.prerender?.enabled ?? true)) return
130β
131 // If there is a filter link, check if the page should be prerendered
132 if (startConfig.prerender?.filter && !startConfig.prerender.filter(page))
133 return
134β
135 // Resolve the merged default and page-specific prerender options
136 const prerenderOptions = {
137 ...startConfig.prerender,
138 ...page.prerender,
139 }
140β
141 // Add the task
142 queue.add(async () => {
143 logger.info(`Crawling: ${page.path}`)
144 const retries = retriesByPath.get(page.path) || 0
145 try {
146 // Fetch the route
147 const encodedRoute = encodeURI(page.path)
148β
149 const res = await localFetch(withBase(encodedRoute, TSS_APP_BASE), {
150 headers: {
151 ...prerenderOptions.headers,
152 },
153 })
154β
155 if (!res.ok) {
156 throw new Error(`Failed to fetch ${page.path}: ${res.statusText}`, {
157 cause: res,
158 })
159 }
160β
161 const cleanPagePath = (
162 prerenderOptions.outputPath || page.path
163 ).split(/[?#]/)[0]!
164β
165 // Guess route type and populate fileName
166 const contentType = res.headers.get('content-type') || ''
167 const isImplicitHTML =
168 !cleanPagePath.endsWith('.html') && contentType.includes('html')
169 // &&
170 // !JsonSigRx.test(dataBuff.subarray(0, 32).toString('utf8'))
171 const routeWithIndex = cleanPagePath.endsWith('/')
172 ? cleanPagePath + 'index'
173 : cleanPagePath
174β
175 const htmlPath =
176 cleanPagePath.endsWith('/') || prerenderOptions.autoSubfolderIndex
177 ? joinURL(cleanPagePath, 'index.html')
178 : cleanPagePath + '.html'
179β
180 const filename = withoutBase(
181 isImplicitHTML ? htmlPath : routeWithIndex,
182 TSS_APP_BASE,
183 )
184β
185 const html = await res.text()
186β
187 const filepath = path.join(outputDir, filename)
188β
189 await fsp.mkdir(path.dirname(filepath), {
190 recursive: true,
191 })
192β
193 await fsp.writeFile(filepath, html)
194β
195 const newPage = await prerenderOptions.onSuccess?.({ page, html })
196β
197 if (newPage) {
198 Object.assign(page, newPage)
199 }
200β
201 // Find new links
202 if (prerenderOptions.crawlLinks ?? true) {
203 const links = extractLinks(html)
204 for (const link of links) {
205 addCrawlPageTask({ path: link, fromCrawl: true })
206 }
207 }
208 } catch (error) {
209 if (retries < (prerenderOptions.retryCount ?? 0)) {
210 logger.warn(`Encountered error, retrying: ${page.path} in 500ms`)
211 await new Promise((resolve) =>
212 setTimeout(resolve, prerenderOptions.retryDelay),
213 )
214 retriesByPath.set(page.path, retries + 1)
215 addCrawlPageTask(page)
216 } else {
217 if (prerenderOptions.failOnError ?? true) {
218 throw error
219 }
220 }
221 }
222 })
223 }
224 }
225}
226β
227function findEntryFileInBundle(bundle: Rollup.OutputBundle): string {
228 let entryFile: string | undefined
229β
230 for (const [_name, file] of Object.entries(bundle)) {
231 if (file.type === 'chunk') {
232 if (file.isEntry) {
233 if (entryFile !== undefined) {
234 throw new Error(
235 `Multiple entry points found. Only one entry point is allowed.`,
236 )
237 }
238 entryFile = file.fileName
239 }
240 }
241 }
242 if (entryFile === undefined) {
243 throw new Error(`No entry point found in the bundle.`)
244 }
245 return entryFile
246}
247β
248export async function writeBundleToDisk({
249 bundle,
250 outDir,
251}: {
252 bundle: Rollup.OutputBundle
253 outDir: string
254}) {
255 for (const [fileName, asset] of Object.entries(bundle)) {
256 const fullPath = path.join(outDir, fileName)
257 const content = asset.type === 'asset' ? asset.source : asset.code
258 await fsp.mkdir(path.dirname(fullPath), { recursive: true })
259 await fsp.writeFile(fullPath, content)
260 }
261}