Skip to content

Commit 347d0b2

Browse files
authored
ENG-1798 Fix website docs search (#1081)
* Fix docs search build and cache Pagefind assets * Harden Pagefind build config test
1 parent 5e13351 commit 347d0b2

3 files changed

Lines changed: 87 additions & 3 deletions

File tree

apps/website/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"license": "Apache-2.0",
77
"scripts": {
88
"dev": "next dev",
9-
"build": "next build",
10-
"postbuild": "node ./scripts/build-docs-search-index.mjs",
9+
"build": "next build && node ./scripts/build-docs-search-index.mjs",
10+
"build:search": "node ./scripts/build-docs-search-index.mjs",
1111
"start": "next start",
1212
"lint": "eslint .",
1313
"lint:fix": "eslint . --fix",

apps/website/scripts/build-docs-search-index.test.mjs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,68 @@
11
import test from "node:test";
22
import assert from "node:assert/strict";
3+
import fs from "node:fs";
34
import path from "node:path";
45
import {
56
markdownToSearchText,
67
routePathFromContentFile,
78
searchFiltersFromContentFile,
89
} from "./build-docs-search-index.mjs";
910

11+
/**
12+
* @typedef {{ scripts: { build: string, postbuild?: string } }} WebsitePackageJson
13+
* @typedef {{ tasks: { build: { outputs: string[] } } }} TurboJson
14+
*/
15+
16+
/**
17+
* @param {unknown} value
18+
* @returns {value is Record<string, unknown>}
19+
*/
20+
const isRecord = (value) =>
21+
typeof value === "object" && value !== null && !Array.isArray(value);
22+
23+
/**
24+
* @param {unknown} value
25+
* @returns {value is string[]}
26+
*/
27+
const isStringArray = (value) =>
28+
Array.isArray(value) && value.every((item) => typeof item === "string");
29+
30+
/**
31+
* @param {string} filePath
32+
* @returns {unknown}
33+
*/
34+
const readJsonFile = (filePath) =>
35+
/** @type {unknown} */ (JSON.parse(fs.readFileSync(filePath, "utf8")));
36+
37+
/**
38+
* @param {unknown} value
39+
* @returns {WebsitePackageJson}
40+
*/
41+
const assertWebsitePackageJson = (value) => {
42+
assert.ok(isRecord(value));
43+
assert.ok(isRecord(value.scripts));
44+
assert.equal(typeof value.scripts.build, "string");
45+
assert.ok(
46+
value.scripts.postbuild === undefined ||
47+
typeof value.scripts.postbuild === "string",
48+
);
49+
50+
return /** @type {WebsitePackageJson} */ (value);
51+
};
52+
53+
/**
54+
* @param {unknown} value
55+
* @returns {TurboJson}
56+
*/
57+
const assertTurboJson = (value) => {
58+
assert.ok(isRecord(value));
59+
assert.ok(isRecord(value.tasks));
60+
assert.ok(isRecord(value.tasks.build));
61+
assert.ok(isStringArray(value.tasks.build.outputs));
62+
63+
return /** @type {TurboJson} */ (value);
64+
};
65+
1066
void test("routePathFromContentFile maps content files to canonical docs routes", () => {
1167
assert.equal(
1268
routePathFromContentFile(
@@ -78,3 +134,25 @@ void test("searchFiltersFromContentFile scopes docs records by platform", () =>
78134
undefined,
79135
);
80136
});
137+
138+
void test("build configuration includes generated Pagefind assets", () => {
139+
const packageJson = assertWebsitePackageJson(readJsonFile("package.json"));
140+
assert.match(
141+
packageJson.scripts.build,
142+
/build-docs-search-index\.mjs/u,
143+
"website build must generate the Pagefind index when run by Turbo",
144+
);
145+
assert.doesNotMatch(
146+
packageJson.scripts.postbuild ?? "",
147+
/build-docs-search-index\.mjs/u,
148+
"Turbo does not run package postbuild hooks for this task",
149+
);
150+
151+
const turboJson = assertTurboJson(
152+
readJsonFile(path.join("..", "..", "turbo.json")),
153+
);
154+
assert.ok(
155+
turboJson.tasks.build.outputs.includes("public/_pagefind/**"),
156+
"Turbo must cache and restore the generated Pagefind assets",
157+
);
158+
});

turbo.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848
"env": ["ROAM_BUILD_SCRIPT"],
4949
"dependsOn": ["@repo/database#genenv"],
5050
"inputs": ["$TURBO_DEFAULT$", ".env*"],
51-
"outputs": [".next/**", "!.next/cache/**", "dist/**", "src/dbTypes.ts"]
51+
"outputs": [
52+
".next/**",
53+
"!.next/cache/**",
54+
"dist/**",
55+
"public/_pagefind/**",
56+
"src/dbTypes.ts"
57+
]
5258
},
5359
"build-schema": {
5460
"inputs": ["$TURBO_DEFAULT$", ".env*"],

0 commit comments

Comments
 (0)