-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
388 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import fs from "node:fs/promises"; | ||
import path from "path"; | ||
|
||
export class FileWritingResponse { | ||
private buffer = ""; | ||
private dontSave = false; | ||
private resolve = () => {}; | ||
private resolved = new Promise<void>((res) => (this.resolve = res)); | ||
|
||
set() {} | ||
status(status: number) { | ||
if (status >= 300) { | ||
this.dontSave = true; | ||
} | ||
} | ||
on() {} | ||
|
||
constructor(private readonly fileName: string) {} | ||
|
||
redirect() { | ||
this.buffer = "redirected"; | ||
this.dontSave = true; | ||
this.resolve(); | ||
} | ||
|
||
send = async (chunk: string) => { | ||
this.write(chunk); | ||
await this.end(); | ||
}; | ||
|
||
write(chunk: string, _encoding?: string) { | ||
this.buffer += chunk; | ||
} | ||
|
||
async end(chunk = "") { | ||
if (!this.dontSave) { | ||
await fs.mkdir(path.dirname(this.fileName), { recursive: true }); | ||
await fs.writeFile(this.fileName, this.buffer + chunk); | ||
} | ||
this.resolve(); | ||
} | ||
|
||
isSent() { | ||
return this.resolved; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import path from "node:path"; | ||
import { pathToFileURL } from "node:url"; | ||
import PiscinaImport from "piscina"; | ||
import type { getRoutesByConfig } from "../../app/main.js"; | ||
import { type ZudokuConfig } from "../../config/validators/validate.js"; | ||
import { generateSitemap } from "../sitemap.js"; | ||
import { type WorkerData } from "./worker.js"; | ||
|
||
const Piscina = PiscinaImport as unknown as typeof PiscinaImport.default; | ||
|
||
const routesToPaths = (routes: ReturnType<typeof getRoutesByConfig>) => { | ||
const paths: string[] = []; | ||
const addPaths = (routes: ReturnType<typeof getRoutesByConfig>) => { | ||
for (const route of routes) { | ||
// skip catch-all routes | ||
if (route.path?.includes("*")) { | ||
continue; | ||
} | ||
|
||
if (route.path) { | ||
paths.push(route.path.startsWith("/") ? route.path : `/${route.path}`); | ||
} | ||
if (route.children) { | ||
addPaths(route.children); | ||
} | ||
} | ||
}; | ||
addPaths(routes); | ||
return paths; | ||
}; | ||
|
||
export const prerender = async ({ | ||
html, | ||
dir, | ||
basePath = "", | ||
serverConfigFilename, | ||
}: { | ||
html: string; | ||
dir: string; | ||
basePath?: string; | ||
serverConfigFilename: string; | ||
}) => { | ||
// eslint-disable-next-line no-console | ||
console.log("Prerendering..."); | ||
const distDir = path.join(dir, "dist", basePath); | ||
const config: ZudokuConfig = await import( | ||
pathToFileURL(path.join(distDir, "server", serverConfigFilename)).href | ||
).then((m) => m.default); | ||
|
||
const module = await import( | ||
pathToFileURL(path.join(distDir, "server/entry.server.js")).href | ||
); | ||
const getRoutes = module.getRoutesByConfig as typeof getRoutesByConfig; | ||
|
||
const routes = getRoutes(config); | ||
const paths = routesToPaths(routes); | ||
|
||
const pool = new Piscina({ | ||
filename: new URL("./worker.js", import.meta.url).href, | ||
}); | ||
|
||
const start = performance.now(); | ||
let completedCount = 0; | ||
const writtenFiles = await Promise.all( | ||
paths.map(async (urlPath) => { | ||
const filename = urlPath === "/" ? "/index.html" : `${urlPath}.html`; | ||
const outputPath = path.join(distDir, filename); | ||
const url = `http://localhost${config.basePath ?? ""}${urlPath}`; | ||
const serverPath = path.join(distDir, "server"); | ||
|
||
await pool.run({ | ||
template: html, | ||
outputPath, | ||
url, | ||
serverConfigPath: path.join(serverPath, serverConfigFilename), | ||
entryServerPath: path.join(serverPath, "entry.server.js"), | ||
} satisfies WorkerData); | ||
|
||
completedCount++; | ||
|
||
if (process.stdout.isTTY) { | ||
process.stdout.write( | ||
`\rWritten ${completedCount}/${paths.length} pages`, | ||
); | ||
} | ||
return outputPath; | ||
}), | ||
); | ||
|
||
const seconds = ((performance.now() - start) / 1000).toFixed(1); | ||
process.stdout.write(`\rWritten ${paths.length} pages in ${seconds} seconds`); | ||
|
||
await pool.destroy(); | ||
|
||
await generateSitemap({ | ||
basePath: config.basePath, | ||
outputUrls: paths, | ||
config: config.sitemap, | ||
baseOutputDir: distDir, | ||
}); | ||
|
||
return writtenFiles; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { pathToFileURL } from "node:url"; | ||
import type { render } from "../../app/entry.server.js"; | ||
import type { ZudokuConfig } from "../../config/validators/validate.js"; | ||
import { FileWritingResponse } from "./FileWritingResponse.js"; | ||
|
||
export type WorkerData = { | ||
template: string; | ||
outputPath: string; | ||
url: string; | ||
serverConfigPath: string; | ||
entryServerPath: string; | ||
}; | ||
|
||
let initialized = false; | ||
let renderFn: typeof render; | ||
let config: ZudokuConfig; | ||
|
||
const initialize = async ({ | ||
serverConfigPath, | ||
entryServerPath, | ||
}: WorkerData) => { | ||
if (initialized) return; | ||
|
||
const [module, configModule] = await Promise.all([ | ||
import(pathToFileURL(entryServerPath).href), | ||
import(pathToFileURL(serverConfigPath).href), | ||
]); | ||
|
||
renderFn = module.render; | ||
config = configModule.default; | ||
initialized = true; | ||
}; | ||
|
||
const renderPage = async (data: WorkerData): Promise<string> => { | ||
await initialize(data); | ||
|
||
const { url, template, outputPath } = data; | ||
const request = new Request(url); | ||
const response = new FileWritingResponse(outputPath); | ||
|
||
await renderFn({ template, request, response, config }); | ||
await response.isSent(); | ||
|
||
return data.outputPath; | ||
}; | ||
|
||
export default renderPage; |
Oops, something went wrong.