Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): split rendering index.html from building index.json #10953

Merged
merged 21 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7902617
feat(build): split build and ssr in two
fiji-flo Apr 25, 2024
5f96239
rename ssr to build:render and fix ci
fiji-flo Apr 26, 2024
6445128
rename and render non docs only
fiji-flo Apr 26, 2024
2c38db9
Merge branch 'main' into ssr2.1
caugner Jun 14, 2024
4766b81
refactor(build/ssr): extract ssrSingleDocument()
caugner Jun 14, 2024
90ddf37
refactor(build): extract ssr-cli + add yari-render-html bin
caugner Jun 14, 2024
2f40fbe
chore(npm-published-simulation): remove yari-render-html again
caugner Jun 14, 2024
c6df491
fix(ssr): construct canonical URL properly
caugner Jun 14, 2024
3b505f6
refactor: rename {build:render-html => render:html}
caugner Jun 14, 2024
743ed55
chore(build): add deprecation warning
caugner Jun 14, 2024
ff8df1d
refactor(build/blog): inline hyData
caugner Jun 14, 2024
9250148
chore(build/spas): remove unused import
caugner Jun 14, 2024
8b9265d
refactor(build/ssr): extract functions
caugner Jun 14, 2024
e1ea7cc
chore(build/ssr): add warning if document has no url
caugner Jun 14, 2024
d69547d
refactor(build/ssr): use options object
caugner Jun 14, 2024
ff4c085
refactor(build/ssr): rename variables for clarity
caugner Jun 14, 2024
6772507
refactor(build): use Date.now() to keep it simple
caugner Jun 14, 2024
f278bc8
chore(build/spas): revert inlined locale variable
caugner Jun 14, 2024
c8a3a47
chore(build/spas): type context objects
caugner Jun 14, 2024
2ca5053
chore(build/ssr): order imports
caugner Jun 14, 2024
175fee5
refactor(scripts): rename build:{json => docs}
caugner Jun 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/dev-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ jobs:
# Spread the work across 2 processes. Why 2? Because that's what you
# get in the default GitHub hosting Linux runners.
# See https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
yarn build --locale en-us --locale ja --locale fr &
yarn build:docs --locale en-us --locale ja --locale fr &
build1=$!
yarn build --not-locale en-us --not-locale ja --not-locale fr &
yarn build:docs --not-locale en-us --not-locale ja --not-locale fr &
build2=$!

# You must explicitly specify the job you're waiting-on to ensure
Expand All @@ -159,6 +159,9 @@ jobs:
# Generate sitemap index file
yarn build --sitemap-index

# SSR all pages
yarn render:html

# Generate whatsdeployed files.
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/performance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ jobs:
BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID: G-XXXXXXXX
run: |
yarn build:prepare
# BUILD_FOLDERSEARCH=mdn/kitchensink yarn build
BUILD_FOLDERSEARCH=web/javascript/reference/global_objects/array/foreach yarn build
# BUILD_FOLDERSEARCH=mdn/kitchensink yarn build:docs
BUILD_FOLDERSEARCH=web/javascript/reference/global_objects/array/foreach yarn build:docs
yarn render:html

- name: Serve and lhci
env:
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/prod-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ jobs:
# Build using one process per locale.
# Note: We have 4 cores, but 9 processes is a reasonable number.
for locale in en-us es fr ja ko pt-br ru zh-cn zh-tw; do
yarn build --locale $locale 2>&1 | sed "s/^/[$locale] /" &
yarn build:docs --locale $locale 2>&1 | sed "s/^/[$locale] /" &
pids+=($!)
done

Expand All @@ -283,6 +283,9 @@ jobs:
# Generate sitemap index file
yarn build --sitemap-index

# SSR all pages
yarn render:html

# Generate whatsdeployed files.
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/stage-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ jobs:
# Build using one process per locale.
# Note: We have 4 cores, but 9 processes is a reasonable number.
for locale in en-us es fr ja ko pt-br ru zh-cn zh-tw; do
yarn build --locale $locale 2>&1 | sed "s/^/[$locale] /" &
yarn build:docs --locale $locale 2>&1 | sed "s/^/[$locale] /" &
pids+=($!)
done

Expand All @@ -299,6 +299,9 @@ jobs:
# Generate sitemap index file
yarn build --sitemap-index

# SSR all pages
yarn render:html

# Generate whatsdeployed files.
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ jobs:
ENV_FILE: .env.testing
run: |
yarn build:prepare
yarn build
yarn build:docs
yarn render:html

yarn start:static-server > /tmp/stdout.log 2> /tmp/stderr.log &
sleep 1
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/xyz-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ jobs:
# Build using one process per locale.
# Note: We have 4 cores, but 9 processes is a reasonable number.
for locale in en-us es fr ja ko pt-br ru zh-cn zh-tw; do
yarn build --locale $locale 2>&1 | sed "s/^/[$locale] /" &
yarn build:docs --locale $locale 2>&1 | sed "s/^/[$locale] /" &
pids+=($!)
done

Expand All @@ -190,6 +190,9 @@ jobs:
# Generate sitemap index file
yarn build --sitemap-index

# SSR all pages
yarn render:html

# Generate whatsdeployed files.
yarn tool whatsdeployed --output client/build/_whatsdeployed/code.json
yarn tool whatsdeployed $CONTENT_ROOT --output client/build/_whatsdeployed/content.json
Expand Down
28 changes: 11 additions & 17 deletions build/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import {
AuthorFrontmatter,
AuthorMetadata,
} from "../libs/types/blog.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { renderHTML } from "../ssr/dist/main.js";
import {
findPostFileBySlug,
injectLoadingLazyAttributes,
Expand Down Expand Up @@ -240,24 +237,24 @@ export async function buildBlogIndex(options: { verbose?: boolean }) {
const prefix = "blog";
const locale = DEFAULT_LOCALE;

const hyData = {
posts: await allPostFrontmatter(),
const context: HydrationData = {
hyData: {
posts: await allPostFrontmatter(),
},
pageTitle: "MDN Blog",
url: `/${locale}/${prefix}/`,
};
const context = { hyData, pageTitle: "MDN Blog" };

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

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

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

if (options.verbose) {
console.log("Wrote", filePath);
console.log("Wrote", jsonFilePath);
}
}

Expand All @@ -280,8 +277,8 @@ export async function buildBlogPosts(options: {
continue;
}

const url = `/${locale}/blog/${blogMeta.slug}/`;
const renderUrl = `/${locale}/blog/${blogMeta.slug}`;
const url = `/${locale}/${prefix}/${blogMeta.slug}/`;
const renderUrl = `/${locale}/${prefix}/${blogMeta.slug}`;
const renderDoc: BlogPostDoc = {
url: renderUrl,
rawBody: body,
Expand All @@ -303,6 +300,7 @@ export async function buildBlogPosts(options: {
locale,
noIndexing: options.noIndexing,
image: blogMeta.image?.file && `${BASE_URL}${url}${blogMeta.image?.file}`,
url,
};

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

const html = renderHTML(`/${locale}/${prefix}/${blogMeta.slug}/`, context);

const filePath = path.join(outPath, "index.html");
const jsonFilePath = path.join(outPath, "index.json");

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

if (options.verbose) {
console.log("Wrote", filePath);
console.log("Wrote", jsonFilePath);
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions build/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import { DEFAULT_LOCALE, VALID_LOCALES } from "../libs/constants/index.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { renderHTML } from "../ssr/dist/main.js";
import options from "./build-options.js";
import {
buildDocument,
Expand All @@ -32,6 +31,8 @@ import { humanFileSize } from "./utils.js";
import { initSentry } from "./sentry.js";
import { macroRenderTimes } from "../kumascript/src/render.js";
import { fdir } from "fdir";
import { ssrDocument } from "./ssr.js";
import { HydrationData } from "../libs/types/hydration.js";

const { program } = caporal;
const { prompt } = inquirer;
Expand Down Expand Up @@ -240,20 +241,21 @@ async function buildDocuments(
updateBaselineBuildMetadata(builtDocument);
}

const context: HydrationData = {
doc: builtDocument,
url: builtDocument.mdn_url,
};

if (!noHTML) {
fs.writeFileSync(
path.join(outPath, "index.html"),
renderHTML(document.url, { doc: builtDocument })
);
fs.writeFileSync(path.join(outPath, "index.html"), ssrDocument(context));
}

if (plainHTML) {
fs.writeFileSync(path.join(outPath, "plain.html"), plainHTML);
}

// This is exploiting the fact that renderHTML has the side-effect of
// mutating the built document which makes this not great and refactor-worthy.
const docString = JSON.stringify({ doc: builtDocument });
const docString = JSON.stringify(context);
fs.writeFileSync(path.join(outPath, "index.json"), docString);
fs.writeFileSync(
path.join(outPath, "contributors.txt"),
Expand Down Expand Up @@ -472,7 +474,7 @@ program
.option("-i, --interactive", "Ask what to do when encountering flaws", {
default: false,
})
.option("-n, --nohtml", "Do not build index.html", {
.option("-n, --nohtml", "Do not render index.html", {
default: false,
})
.option("-l, --locale <locale...>", "Filtered specific locales", {
Expand All @@ -489,6 +491,12 @@ program
.argument("[files...]", "specific files to build")
.action(async ({ args, options }: BuildArgsAndOptions) => {
try {
if (!options.nohtml) {
console.warn(
"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"
);
}

if (!options.quiet) {
const roots = [
["CONTENT_ROOT", CONTENT_ROOT],
Expand Down
8 changes: 2 additions & 6 deletions build/curriculum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { HydrationData } from "../libs/types/hydration.js";
import { memoize, slugToFolder } from "../content/utils.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { renderHTML } from "../ssr/dist/main.js";
import { CheerioAPI } from "cheerio";
import { buildSitemap } from "./sitemaps.js";

Expand Down Expand Up @@ -368,6 +367,7 @@ export async function buildCurriculum(options: {
pageTitle: meta.title,
locale,
noIndexing: options.noIndexing,
url: `/${locale}/${meta.slug}/`,
};

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

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

const html: string = renderHTML(`/${locale}/${meta.slug}/`, context);

const filePath = path.join(outPath, "index.html");
const jsonFilePath = path.join(outPath, "index.json");

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

if (options.verbose) {
console.log("Wrote", filePath);
console.log("Wrote", jsonFilePath);
}
}
}
Expand Down
Loading
Loading