evidence-dev / evidence

Business intelligence as code: build fast, interactive data visualizations in pure SQL and markdown
https://evidence.dev
MIT License
4.44k stars 212 forks source link

InitialData interfering with HMR on inline sql changes #1762

Open smilingthax opened 8 months ago

smilingthax commented 8 months ago

Steps To Reproduce

  1. Create index.md with inline sql query returning multiple rows and display results (e.g. chart, datatable, "show sql", ...).

  2. Start dev server, open page in browser

  1. In index.md add (e.g.) LIMIT 1 to reduce the number of rows. HMR will reload the page in the browser. The page might scroll to the top (#1341 seems to be somewhat related).

Environment

Expected Behavior

The displayed results shows (e.g.) only a single row.

Actual Behaviour

The SQL Query text changes ("Show SQL"), but the number of rows stays unchanged (= multiple rows).

Reloading the whole page manually does finally display the correct result, though.

Workarounds

When starting the dev server BEFORE creating the page, and then navigating to it (needs more than 1 page), SQL changes directly lead to correct results after HMR. Also, AFAICT #1341 scroll-to-top seems to happen less frequently...

Alternatively, adding opts.initialDataDirty = true unconditionally, e.g. here: https://github.com/evidence-dev/evidence/blob/next/packages/query-store/src/QueryStore.ts#L233 "fixes" the problem.

(I believe there are additional cases where all-queries.json is not loaded, which do work correctly.)

Explanation

smilingthax commented 8 months ago

Ok, I now believe https://github.com/evidence-dev/evidence/commit/90e152cbc088e16d2f1d9686b25ba023322dc156 should have fixed this, but vite:afterUpdate just never triggers...

Version is vite/4.3.9 linux-x64 node-v20.11.1, coming from https://github.com/evidence-dev/template/blob/main/package-lock.json#L2142

Edit: npm install vite@5.1.6 does not seem to help, though...

smilingthax commented 8 months ago

Well, vite:afterUpdate does actually trigger, but _reactivity_manager's update() function https://github.com/evidence-dev/evidence/blob/next/packages/lib/preprocess/src/process-queries.cjs#L99 is not run again after the hmr.

Fix: Add __has_hmr_run as svelte dependency in https://github.com/evidence-dev/evidence/blob/next/packages/lib/preprocess/src/process-queries.cjs#L197

                // rerun if data changes during dev mode, likely source HMR, prevent initial for same reason as above
                let _${id}_hmr_once = false;
                $: if (dev) {
                    if (_${id}_hmr_once) {
                        data;
                        __has_hmr_run;      // <-- add dependency
                        _${id}_debounced_updater();
                    } else {
                        _${id}_hmr_once = true;
                    }
                }

(This does not improve the situation w/ https://github.com/evidence-dev/evidence/issues/1341 any more, though.)

ItsMeBrianD commented 4 months ago

@kwongz please give this a look to see if it is still occuring