From a15ac5a006c510e03303ef243005c415bfa9caf5 Mon Sep 17 00:00:00 2001 From: Deep Singhvi Date: Sun, 19 May 2024 10:05:08 -0400 Subject: [PATCH] (fix): improve local preview responsiveness (#3646) --- .pnp.cjs | 2 - packages/cli/docs-preview/package.json | 2 - .../cli/docs-preview/src/runPreviewServer.ts | 63 ++++++++----------- yarn.lock | 2 - 4 files changed, 27 insertions(+), 42 deletions(-) diff --git a/.pnp.cjs b/.pnp.cjs index 0d63a1cbf59..249ade36bd6 100644 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -6613,7 +6613,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["@types/decompress", "npm:4.2.7"],\ ["@types/express", "npm:4.17.20"],\ ["@types/jest", "npm:29.0.3"],\ - ["@types/lodash-es", "npm:4.17.12"],\ ["@types/node", "npm:18.7.18"],\ ["@types/uuid", "npm:9.0.8"],\ ["@types/ws", "npm:8.5.10"],\ @@ -6625,7 +6624,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["eslint", "npm:8.56.0"],\ ["express", "npm:4.19.2"],\ ["jest", "virtual:816fb67d993b0978271f762d4ccbec7209ef2546c234ca6e241662d44336c8e32c1c3c07189cfe14b67974a4840e1ed140408a7403bf9deb68c1953445072efe#npm:29.7.0"],\ - ["lodash-es", "npm:4.17.21"],\ ["organize-imports-cli", "npm:0.10.0"],\ ["prettier", "npm:2.7.1"],\ ["tmp-promise", "npm:3.0.3"],\ diff --git a/packages/cli/docs-preview/package.json b/packages/cli/docs-preview/package.json index d5f3082bdf8..0aef1395da0 100644 --- a/packages/cli/docs-preview/package.json +++ b/packages/cli/docs-preview/package.json @@ -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", @@ -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", diff --git a/packages/cli/docs-preview/src/runPreviewServer.ts b/packages/cli/docs-preview/src/runPreviewServer.ts index 5796b3b3e81..e9056ebe280 100644 --- a/packages/cli/docs-preview/src/runPreviewServer.ts +++ b/packages/cli/docs-preview/src/runPreviewServer.ts @@ -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"; @@ -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, diff --git a/yarn.lock b/yarn.lock index e749c90b001..d976c60f205 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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