Skip to content

Commit acc0ede

Browse files
fiji-flocaugner
andauthored
feat(build): split rendering index.html from building index.json (#10953)
DEPRECATION: Rendering index.html files as part of the build command is now deprecated, and will no longer be supported in Yari v3. To resolve the deprecation warning, add the `-n` (`--nohtml`) option. Problem: Currently, building the `index.json` files and rendering the `index.html` files happens in a single step via the `yarn build` command. Although it's possible to only build the `index.json` files via `yarn build -n`, it is not possible to only render the `index.html` files. Solution: 1. Split rendering the `index.html` files into a separate command (`yarn render:html`). 2. Introduce a new explicit command for building the JSON files for docs (`yarn build:docs`). 3. Reuse the `render:html` command to render static pages (aka "SPAs"), by ensuring the corresponding builds produce `index.json`s (that just contain a `pageTitle` and a `url` for now). 4. Show a deprecation warning (see above) when `yarn build` is called **without** the `-n` flag. Co-authored-by: Claas Augner <[email protected]>
1 parent b68d1ac commit acc0ede

22 files changed

+207
-115
lines changed

Diff for: .github/workflows/dev-build.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ jobs:
141141
# Spread the work across 2 processes. Why 2? Because that's what you
142142
# get in the default GitHub hosting Linux runners.
143143
# See https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
144-
yarn build --locale en-us --locale ja --locale fr &
144+
yarn build:docs --locale en-us --locale ja --locale fr &
145145
build1=$!
146-
yarn build --not-locale en-us --not-locale ja --not-locale fr &
146+
yarn build:docs --not-locale en-us --not-locale ja --not-locale fr &
147147
build2=$!
148148
149149
# You must explicitly specify the job you're waiting-on to ensure
@@ -159,6 +159,9 @@ jobs:
159159
# Generate sitemap index file
160160
yarn build --sitemap-index
161161
162+
# SSR all pages
163+
yarn render:html
164+
162165
# Generate whatsdeployed files.
163166
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
164167
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json

Diff for: .github/workflows/performance.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ jobs:
5353
BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID: G-XXXXXXXX
5454
run: |
5555
yarn build:prepare
56-
# BUILD_FOLDERSEARCH=mdn/kitchensink yarn build
57-
BUILD_FOLDERSEARCH=web/javascript/reference/global_objects/array/foreach yarn build
56+
# BUILD_FOLDERSEARCH=mdn/kitchensink yarn build:docs
57+
BUILD_FOLDERSEARCH=web/javascript/reference/global_objects/array/foreach yarn build:docs
58+
yarn render:html
5859
5960
- name: Serve and lhci
6061
env:

Diff for: .github/workflows/prod-build.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ jobs:
264264
# Build using one process per locale.
265265
# Note: We have 4 cores, but 9 processes is a reasonable number.
266266
for locale in en-us es fr ja ko pt-br ru zh-cn zh-tw; do
267-
yarn build --locale $locale 2>&1 | sed "s/^/[$locale] /" &
267+
yarn build:docs --locale $locale 2>&1 | sed "s/^/[$locale] /" &
268268
pids+=($!)
269269
done
270270
@@ -283,6 +283,9 @@ jobs:
283283
# Generate sitemap index file
284284
yarn build --sitemap-index
285285
286+
# SSR all pages
287+
yarn render:html
288+
286289
# Generate whatsdeployed files.
287290
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
288291
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json

Diff for: .github/workflows/stage-build.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ jobs:
280280
# Build using one process per locale.
281281
# Note: We have 4 cores, but 9 processes is a reasonable number.
282282
for locale in en-us es fr ja ko pt-br ru zh-cn zh-tw; do
283-
yarn build --locale $locale 2>&1 | sed "s/^/[$locale] /" &
283+
yarn build:docs --locale $locale 2>&1 | sed "s/^/[$locale] /" &
284284
pids+=($!)
285285
done
286286
@@ -299,6 +299,9 @@ jobs:
299299
# Generate sitemap index file
300300
yarn build --sitemap-index
301301
302+
# SSR all pages
303+
yarn render:html
304+
302305
# Generate whatsdeployed files.
303306
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
304307
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json

Diff for: .github/workflows/testing.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ jobs:
9090
ENV_FILE: .env.testing
9191
run: |
9292
yarn build:prepare
93-
yarn build
93+
yarn build:docs
94+
yarn render:html
9495
9596
yarn start:static-server > /tmp/stdout.log 2> /tmp/stderr.log &
9697
sleep 1

Diff for: .github/workflows/xyz-build.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ jobs:
171171
# Build using one process per locale.
172172
# Note: We have 4 cores, but 9 processes is a reasonable number.
173173
for locale in en-us es fr ja ko pt-br ru zh-cn zh-tw; do
174-
yarn build --locale $locale 2>&1 | sed "s/^/[$locale] /" &
174+
yarn build:docs --locale $locale 2>&1 | sed "s/^/[$locale] /" &
175175
pids+=($!)
176176
done
177177
@@ -190,6 +190,9 @@ jobs:
190190
# Generate sitemap index file
191191
yarn build --sitemap-index
192192
193+
# SSR all pages
194+
yarn render:html
195+
193196
# Generate whatsdeployed files.
194197
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
195198
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json

Diff for: build/blog.ts

+11-17
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import {
1818
AuthorFrontmatter,
1919
AuthorMetadata,
2020
} from "../libs/types/blog.js";
21-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
22-
// @ts-ignore
23-
import { renderHTML } from "../ssr/dist/main.js";
2421
import {
2522
findPostFileBySlug,
2623
injectLoadingLazyAttributes,
@@ -240,24 +237,24 @@ export async function buildBlogIndex(options: { verbose?: boolean }) {
240237
const prefix = "blog";
241238
const locale = DEFAULT_LOCALE;
242239

243-
const hyData = {
244-
posts: await allPostFrontmatter(),
240+
const context: HydrationData = {
241+
hyData: {
242+
posts: await allPostFrontmatter(),
243+
},
244+
pageTitle: "MDN Blog",
245+
url: `/${locale}/${prefix}/`,
245246
};
246-
const context = { hyData, pageTitle: "MDN Blog" };
247247

248-
const html = renderHTML(`/${locale}/${prefix}/`, context);
249248
const outPath = path.join(BUILD_OUT_ROOT, locale.toLowerCase(), `${prefix}`);
250249

251250
await fs.mkdir(outPath, { recursive: true });
252-
const filePath = path.join(outPath, "index.html");
253251
const jsonFilePath = path.join(outPath, "index.json");
254252

255253
await fs.mkdir(outPath, { recursive: true });
256-
await fs.writeFile(filePath, html);
257254
await fs.writeFile(jsonFilePath, JSON.stringify(context));
258255

259256
if (options.verbose) {
260-
console.log("Wrote", filePath);
257+
console.log("Wrote", jsonFilePath);
261258
}
262259
}
263260

@@ -280,8 +277,8 @@ export async function buildBlogPosts(options: {
280277
continue;
281278
}
282279

283-
const url = `/${locale}/blog/${blogMeta.slug}/`;
284-
const renderUrl = `/${locale}/blog/${blogMeta.slug}`;
280+
const url = `/${locale}/${prefix}/${blogMeta.slug}/`;
281+
const renderUrl = `/${locale}/${prefix}/${blogMeta.slug}`;
285282
const renderDoc: BlogPostDoc = {
286283
url: renderUrl,
287284
rawBody: body,
@@ -303,6 +300,7 @@ export async function buildBlogPosts(options: {
303300
locale,
304301
noIndexing: options.noIndexing,
305302
image: blogMeta.image?.file && `${BASE_URL}${url}${blogMeta.image?.file}`,
303+
url,
306304
};
307305

308306
const outPath = path.join(
@@ -330,17 +328,13 @@ export async function buildBlogPosts(options: {
330328
await fs.copyFile(from, to);
331329
}
332330

333-
const html = renderHTML(`/${locale}/${prefix}/${blogMeta.slug}/`, context);
334-
335-
const filePath = path.join(outPath, "index.html");
336331
const jsonFilePath = path.join(outPath, "index.json");
337332

338333
await fs.mkdir(outPath, { recursive: true });
339-
await fs.writeFile(filePath, html);
340334
await fs.writeFile(jsonFilePath, JSON.stringify(context));
341335

342336
if (options.verbose) {
343-
console.log("Wrote", filePath);
337+
console.log("Wrote", jsonFilePath);
344338
}
345339
}
346340
}

Diff for: build/cli.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import { DEFAULT_LOCALE, VALID_LOCALES } from "../libs/constants/index.js";
1919
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2020
// @ts-ignore
21-
import { renderHTML } from "../ssr/dist/main.js";
2221
import options from "./build-options.js";
2322
import {
2423
buildDocument,
@@ -32,6 +31,8 @@ import { humanFileSize } from "./utils.js";
3231
import { initSentry } from "./sentry.js";
3332
import { macroRenderTimes } from "../kumascript/src/render.js";
3433
import { fdir } from "fdir";
34+
import { ssrDocument } from "./ssr.js";
35+
import { HydrationData } from "../libs/types/hydration.js";
3536

3637
const { program } = caporal;
3738
const { prompt } = inquirer;
@@ -240,20 +241,21 @@ async function buildDocuments(
240241
updateBaselineBuildMetadata(builtDocument);
241242
}
242243

244+
const context: HydrationData = {
245+
doc: builtDocument,
246+
url: builtDocument.mdn_url,
247+
};
248+
243249
if (!noHTML) {
244-
fs.writeFileSync(
245-
path.join(outPath, "index.html"),
246-
renderHTML(document.url, { doc: builtDocument })
247-
);
250+
fs.writeFileSync(path.join(outPath, "index.html"), ssrDocument(context));
248251
}
249-
250252
if (plainHTML) {
251253
fs.writeFileSync(path.join(outPath, "plain.html"), plainHTML);
252254
}
253255

254256
// This is exploiting the fact that renderHTML has the side-effect of
255257
// mutating the built document which makes this not great and refactor-worthy.
256-
const docString = JSON.stringify({ doc: builtDocument });
258+
const docString = JSON.stringify(context);
257259
fs.writeFileSync(path.join(outPath, "index.json"), docString);
258260
fs.writeFileSync(
259261
path.join(outPath, "contributors.txt"),
@@ -472,7 +474,7 @@ program
472474
.option("-i, --interactive", "Ask what to do when encountering flaws", {
473475
default: false,
474476
})
475-
.option("-n, --nohtml", "Do not build index.html", {
477+
.option("-n, --nohtml", "Do not render index.html", {
476478
default: false,
477479
})
478480
.option("-l, --locale <locale...>", "Filtered specific locales", {
@@ -489,6 +491,12 @@ program
489491
.argument("[files...]", "specific files to build")
490492
.action(async ({ args, options }: BuildArgsAndOptions) => {
491493
try {
494+
if (!options.nohtml) {
495+
console.warn(
496+
"WARNING: Rendering index.html files as part of the build command is now DEPRECATED, and will no longer be supported in Yari v3. To resolve this warning, add the `-n` (`--nohtml`) option. For details, see: https://github.com/mdn/yari/pull/10953"
497+
);
498+
}
499+
492500
if (!options.quiet) {
493501
const roots = [
494502
["CONTENT_ROOT", CONTENT_ROOT],

Diff for: build/curriculum.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { HydrationData } from "../libs/types/hydration.js";
3737
import { memoize, slugToFolder } from "../content/utils.js";
3838
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3939
// @ts-ignore
40-
import { renderHTML } from "../ssr/dist/main.js";
4140
import { CheerioAPI } from "cheerio";
4241
import { buildSitemap } from "./sitemaps.js";
4342

@@ -368,6 +367,7 @@ export async function buildCurriculum(options: {
368367
pageTitle: meta.title,
369368
locale,
370369
noIndexing: options.noIndexing,
370+
url: `/${locale}/${meta.slug}/`,
371371
};
372372

373373
const outPath = path.join(
@@ -378,17 +378,13 @@ export async function buildCurriculum(options: {
378378

379379
await fs.mkdir(outPath, { recursive: true });
380380

381-
const html: string = renderHTML(`/${locale}/${meta.slug}/`, context);
382-
383-
const filePath = path.join(outPath, "index.html");
384381
const jsonFilePath = path.join(outPath, "index.json");
385382

386383
await fs.mkdir(outPath, { recursive: true });
387-
await fs.writeFile(filePath, html);
388384
await fs.writeFile(jsonFilePath, JSON.stringify(context));
389385

390386
if (options.verbose) {
391-
console.log("Wrote", filePath);
387+
console.log("Wrote", jsonFilePath);
392388
}
393389
}
394390
}

0 commit comments

Comments
 (0)