-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathprerender-routes-plugin.ts
More file actions
39 lines (32 loc) · 1.21 KB
/
prerender-routes-plugin.ts
File metadata and controls
39 lines (32 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { inferFullPath } from '@tanstack/router-generator'
import type { GeneratorPlugin, RouteNode } from '@tanstack/router-generator'
/**
* this plugin gets the prerenderable paths and stores it on globalThis
* so that it can be accessed later (e.g. from a vite plugin)
*/
export function prerenderRoutesPlugin(): GeneratorPlugin {
return {
name: 'prerender-routes-plugin',
onRouteTreeChanged: ({ routeNodes }) => {
globalThis.TSS_PRERENDABLE_PATHS = getPrerenderablePaths(routeNodes)
},
}
}
function getPrerenderablePaths(
routeNodes: Array<RouteNode>,
): Array<{ path: string }> {
const paths = new Set<string>(['/'])
for (const route of routeNodes) {
if (!route.routePath) continue
// filter routes that are layout
if (route.isNonPath === true) continue
// filter dynamic routes
// if routePath contains $ it is dynamic
if (route.routePath.includes('$')) continue
// filter routes that do not have a component, i.e api routes
if (!route.createFileRouteProps?.has('component')) continue
const fullPath = inferFullPath(route)
paths.add(fullPath === '/' ? fullPath : fullPath.replace(/\/$/, ''))
}
return Array.from(paths).map((path) => ({ path }))
}