Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

custom-viz: dev server survives atomic writes to assets (#83597)

Egor Grushin62b409c3e850
diff --git a/enterprise/frontend/src/custom-viz/CHANGELOG.md b/enterprise/frontend/src/custom-viz/CHANGELOG.md
index 3d9146a32f6..ce7c7f0f7c4 100644
--- a/enterprise/frontend/src/custom-viz/CHANGELOG.md
+++ b/enterprise/frontend/src/custom-viz/CHANGELOG.md
@@ -2,6 +2,22 @@
This changelog covers the `@metabase/custom-viz` npm package — the API and CLI for building custom visualizations for Metabase. Changes to how Metabase itself hosts custom visualization plugins are covered by the [Metabase changelog](https://www.metabase.com/changelog).
+## Unreleased
+
+### Bug Fixes
+
+- The scaffolded dev server no longer crashes when an asset file disappears before it's copied, which happened with editors that save through atomic writes, or when a directory is added to `public/assets`. One save now triggers a single reload. Assets deleted from `public/assets` are now removed from `dist/assets` too ([#83597](https://github.com/metabase/metabase/pull/83597)).
+
+### Fix the asset watcher in existing projects
+
+After upgrading, copy the new config from the package:
+
+```
+cp node_modules/@metabase/custom-viz/dist/templates/vite.config.ts vite.config.ts
+```
+
+If you've customized `vite.config.ts`, port the `syncAssets` function and the `watch(assetsDirSrc, ...)` callback from that file instead.
+
## 2.0.0
### ⚠ BREAKING CHANGES
diff --git a/enterprise/frontend/src/custom-viz/src/templates/vite.config.ts b/enterprise/frontend/src/custom-viz/src/templates/vite.config.ts
index 919fd3b90d2..78534ebe23c 100644
--- a/enterprise/frontend/src/custom-viz/src/templates/vite.config.ts
+++ b/enterprise/frontend/src/custom-viz/src/templates/vite.config.ts
@@ -1,5 +1,5 @@
/* eslint-disable no-console, import/no-default-export, @typescript-eslint/no-require-imports */
-import { cpSync, existsSync, readFileSync, watch } from "fs";
+import { cpSync, existsSync, readFileSync, rmSync, watch } from "fs";
import { type ServerResponse, createServer } from "http";
import { createRequire } from "module";
import { resolve } from "path";
@@ -28,6 +28,8 @@ function metabaseCopyManifest() {
const DEV_PORT = 5174;
+const ASSETS_SYNC_DEBOUNCE_MS = 100;
+
/**
* Vite plugin that starts a dev server serving both static files from dist/
* and an SSE endpoint at /__sse for hot-reload notifications.
@@ -144,22 +146,30 @@ function metabaseDevServer() {
});
// Watch public/assets/ for changes — copy to dist/assets/ and notify
- const assetsDir = resolve(__dirname, "public/assets");
- if (existsSync(assetsDir)) {
- watch(assetsDir, { recursive: true }, (_event, filename) => {
- if (!filename) {
- return;
- }
- cpSync(
- resolve(assetsDir, filename),
- resolve(__dirname, "dist/assets", filename),
- );
- for (const client of clients) {
- client.write("data: reload\n\n");
- }
- console.log(
- `[custom-viz] Asset changed: ${filename}, notified ${clients.size} client(s)`,
- );
+ const assetsDirSrc = resolve(__dirname, "public/assets");
+ const assetsDirDest = resolve(__dirname, "dist/assets");
+ const syncAssets = () => {
+ try {
+ rmSync(assetsDirDest, { recursive: true, force: true });
+ cpSync(assetsDirSrc, assetsDirDest, { recursive: true });
+ } catch (error) {
+ console.error("[custom-viz] Failed to sync assets", error);
+ return;
+ }
+ for (const client of clients) {
+ client.write("data: reload\n\n");
+ }
+ console.log(
+ `[custom-viz] Assets synced, notified ${clients.size} client(s)`,
+ );
+ };
+ let syncTimer: ReturnType<typeof setTimeout> | undefined;
+ if (existsSync(assetsDirSrc)) {
+ watch(assetsDirSrc, { recursive: true }, () => {
+ clearTimeout(syncTimer);
+ syncTimer = setTimeout(syncAssets, ASSETS_SYNC_DEBOUNCE_MS);
+ }).on("error", (error) => {
+ console.error("[custom-viz] Assets watcher error", error);
});
}
},