|
1
|
import { writeFile } from "fs/promises" |
|
2
|
import path from "path" |
|
3
|
import { retrieveMdxPages } from "./retrieve-mdx-pages.js" |
|
4
|
|
|
5
|
type Options = { |
|
6
|
/** |
|
7
|
* The directory to scan for MDX pages. Defaults to the `app` directory of the |
|
8
|
* current project. |
|
9
|
*/ |
|
10
|
basePath?: string |
|
11
|
/** |
|
12
|
* Where to write the generated file. Defaults to `generated/sitemap-urls.mjs` |
|
13
|
* of the current project. |
|
14
|
*/ |
|
15
|
outputPath?: string |
|
16
|
/** |
|
17
|
* Additional URL paths to include in the sitemap that aren't backed by an MDX |
|
18
|
* page (for example, generated reference pages). |
|
19
|
*/ |
|
20
|
extraPaths?: string[] |
|
21
|
} |
|
22
|
|
|
23
|
/** |
|
24
|
* Reads the project's MDX pages at build time and writes their URL paths to a |
|
25
|
* generated `.mjs` file. This lets `app/sitemap.ts` build the sitemap by |
|
26
|
* importing a static array instead of reading the filesystem at request time, |
|
27
|
* which isn't possible in the Cloudflare Workers runtime. |
|
28
|
*/ |
|
29
|
export const generateSitemapUrls = async ({ |
|
30
|
basePath, |
|
31
|
outputPath, |
|
32
|
extraPaths = [], |
|
33
|
}: Options = {}) => { |
|
34
|
const projectBasePath = path.resolve() |
|
35
|
const appPath = basePath || path.join(projectBasePath, "app") |
|
36
|
const generatedFilePath = |
|
37
|
outputPath || path.join(projectBasePath, "generated", "sitemap-urls.mjs") |
|
38
|
|
|
39
|
const paths = retrieveMdxPages({ |
|
40
|
basePath: appPath, |
|
41
|
}) |
|
42
|
|
|
43
|
const urls = [...paths, ...extraPaths] |
|
44
|
|
|
45
|
await writeFile( |
|
46
|
generatedFilePath, |
|
47
|
`export const sitemapUrls = ${JSON.stringify(urls, undefined, 2)}\n`, |
|
48
|
{ |
|
49
|
flag: "w", |
|
50
|
} |
|
51
|
) |
|
52
|
} |