Skip to content

Commit

Permalink
(fix): improve local preview responsiveness (#3646)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored May 19, 2024
1 parent a0e5c6b commit a15ac5a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
2 changes: 0 additions & 2 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions packages/cli/docs-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"cors": "^2.8.5",
"decompress": "^4.2.1",
"express": "^4.19.2",
"lodash-es": "^4.17.21",
"tmp-promise": "^3.0.3",
"uuid": "^9.0.1",
"watcher": "^2.3.1",
Expand All @@ -52,7 +51,6 @@
"@types/decompress": "^4.2.7",
"@types/express": "^4.17.20",
"@types/jest": "^29.0.3",
"@types/lodash-es": "^4.17.12",
"@types/node": "^18.7.18",
"@types/uuid": "^9.0.8",
"@types/ws": "^8.5.10",
Expand Down
63 changes: 27 additions & 36 deletions packages/cli/docs-preview/src/runPreviewServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import chalk from "chalk";
import cors from "cors";
import express from "express";
import http from "http";
import { debounce } from "lodash-es";
import path from "path";
import Watcher from "watcher";
import { WebSocketServer, type WebSocket } from "ws";
Expand Down Expand Up @@ -52,54 +51,46 @@ export async function runPreviewServer({
wrapWithHttps(initialProject.docsWorkspaces?.config.instances[0]?.url ?? "localhost:3000")
);

let docsDefinition = getPreviewDocsDefinition({
let docsDefinition = await getPreviewDocsDefinition({
domain: instance.host,
project: initialProject,
context
});

const reloadDocsDefinition = debounce(
() => {
context.logger.info("Reloading project and docs");
const startTime = Date.now();
docsDefinition = reloadProject()
.then((project) => {
context.logger.info(`Reload project took ${Date.now() - startTime}ms`);
return getPreviewDocsDefinition({
domain: instance.host,
project,
context
});
})
.then((newDocsDefinition) => {
context.logger.info(`Reload project and docs completed in ${Date.now() - startTime}ms`);
return newDocsDefinition;
});
return docsDefinition;
},
300,
{ leading: false, trailing: true }
);
const reloadDocsDefinition = async () => {
context.logger.info("Reloading project and docs");
const startTime = Date.now();
const project = await reloadProject();
context.logger.info(`Reload project took ${Date.now() - startTime}ms`);
const newDocsDefinition = await getPreviewDocsDefinition({
domain: instance.host,
project,
context
});
context.logger.info(`Reload project and docs completed in ${Date.now() - startTime}ms`);
return newDocsDefinition;
};

const watcher = new Watcher(absoluteFilePathToFern, { recursive: true, ignoreInitial: true });
watcher.on("all", (event: string, targetPath: string, _targetPathNext: string) => {
watcher.on("all", async (event: string, targetPath: string, _targetPathNext: string) => {
context.logger.debug(chalk.dim(`[${event}] ${targetPath}`));

// after the docsDefinition is reloaded, send a message to all connected clients to reload the page
void reloadDocsDefinition()?.then(() => {
for (const connection of connections) {
connection.send(
JSON.stringify({
reload: true
})
);
}
});
const reloadedDocsDefinition = await reloadDocsDefinition();
if (reloadedDocsDefinition != null) {
docsDefinition = reloadedDocsDefinition;
}
for (const connection of connections) {
connection.send(
JSON.stringify({
reload: true
})
);
}
});

app.post("/v2/registry/docs/load-with-url", async (_, res) => {
try {
const definition = await docsDefinition;
const definition = docsDefinition;
const response: DocsV2Read.LoadDocsForUrlResponse = {
baseUrl: {
domain: instance.host,
Expand Down
2 changes: 0 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3569,7 +3569,6 @@ __metadata:
"@types/decompress": ^4.2.7
"@types/express": ^4.17.20
"@types/jest": ^29.0.3
"@types/lodash-es": ^4.17.12
"@types/node": ^18.7.18
"@types/uuid": ^9.0.8
"@types/ws": ^8.5.10
Expand All @@ -3581,7 +3580,6 @@ __metadata:
eslint: ^8.56.0
express: ^4.19.2
jest: ^29.7.0
lodash-es: ^4.17.21
organize-imports-cli: ^0.10.0
prettier: ^2.7.1
tmp-promise: ^3.0.3
Expand Down

0 comments on commit a15ac5a

Please sign in to comment.