Skip to content

Commit

Permalink
refactor(build/ssr): extract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Jun 14, 2024
1 parent 9250148 commit 8b9265d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
35 changes: 19 additions & 16 deletions build/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,48 @@ import { readFile, writeFile } from "node:fs/promises";
// @ts-ignore
import { renderHTML } from "../ssr/dist/main.js";
import { HydrationData } from "../libs/types/hydration.js";
import { chunks, formatDuration } from "./utils.js";

export function ssrDocument(context: HydrationData) {
return renderHTML(context);
}

export async function ssrAllDocuments(noDocs = false) {
const api = new fdir()
.withFullPaths()
.withErrors()
.exclude((dirName) => noDocs && dirName === "docs")
.filter(
(filePath) =>
filePath.endsWith("index.json") || filePath.endsWith("404.json")
)
.crawl(BUILD_OUT_ROOT);
const docs = await api.withPromise();
const docs = await findDocuments(noDocs);

const t0 = new Date();

const done = [];
for (let i = 0; i < docs.length; i += 1000) {
const chunk = docs.slice(i, i + 1000);
for (const chunk of chunks(docs, 1000)) {
const out = await Promise.all(chunk.map(ssrSingleDocument).filter(Boolean));
done.push(...out);
}
const t1 = new Date();
const count = done.length;
const seconds = (t1.getTime() - t0.getTime()) / 1000;
const took =
seconds > 60
? `${(seconds / 60).toFixed(1)} minutes`
: `${seconds.toFixed(1)} seconds`;
const took = formatDuration(seconds);

console.log(
`Rendered ${count.toLocaleString()} pages in ${took}, at a rate of ${(
count / seconds
).toFixed(1)} documents per second.`
);
}

async function findDocuments(noDocs: boolean) {
const api = new fdir()
.withFullPaths()
.withErrors()
.exclude((dirName) => noDocs && dirName === "docs")
.filter(
(filePath) =>
filePath.endsWith("index.json") || filePath.endsWith("404.json")
)
.crawl(BUILD_OUT_ROOT);
const docs = await api.withPromise();
return docs;
}

async function ssrSingleDocument(file: string): Promise<string> {
const context: HydrationData = JSON.parse(await readFile(file, "utf-8"));
if (!context?.url) {
Expand Down
12 changes: 12 additions & 0 deletions build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,15 @@ export async function importJSON<T>(jsonPath: string): Promise<T> {

return JSON.parse(json);
}

export function* chunks<T>(array: T[], size: number): Generator<T[]> {
for (let i = 0; i < array.length; i += size) {
yield array.slice(i, i + size);
}
}

export function formatDuration(seconds: number) {
return seconds > 60
? `${(seconds / 60).toFixed(1)} minutes`
: `${seconds.toFixed(1)} seconds`;
}

0 comments on commit 8b9265d

Please sign in to comment.