Fix for Svelte 5 timing bug in SVAR Svelte DataGrid documentation
When I run this tutorial code: https://docs.svar.dev/svelte/grid/getting_started/
Then Svelte throws a 500 error.
As far as I can tell, there's a mount timing issue in Svelte 5 that's causing the grid to fail. What I see is if I edit the page HTML then the grid loads, then if I reload in the browser, the grid fails with a 500 error.
This fix solves it for me. Is there a better way? Thank you.
<script>
// Import the SVAR Svelte DataGrid
import { Grid } from "@svar-ui/svelte-grid";
// Apply the SVAR prebuilt theme named Willow.
import { Willow } from "@svar-ui/svelte-grid";
const columns = [{
id: "id",
width: 50,
header: "Id",
footer: "Id",
},
{
id: "name",
width: 100,
header: "Name",
footer: "Name",
},
];
const data = [{
id: 1,
name: "Alice",
},
{
id: 2,
name: "Bob",
},
];
// Fix for Svelte 5 and SVAR Svelte DataGrid.
let mounted = $state(false);
import { onMount } from "svelte";
onMount(() => { mounted = true; });
</script>
<!-- Apply the SVAR prebuilt theme named Willow. -->
<Willow>
<!-- Wait for the data to be loaded because of timing. -->
{#if mounted}
<!-- Render the SVAR Svelte DataGrid. -->
<Grid {data} {columns} />
{/if}
</Willow>
Hello @joelparkerhenderson ,
SvelteKit tries to render components on the server by default, but the SVAR Grid is a client-side library that relies on the DOM and browser APIs, which aren't available during SSR. This can cause mounting errors (like the 500 error on reload) because the grid tries to initialize before the page is fully hydrated on the client.
Your fix with mounted state and onMount is a good workaround, it delays rendering until the component is mounted on the client, avoiding the SSR conflict. However, there's a more straightforward and recommended way to handle this for client-side libraries like SVAR Grid.
You can disable SSR for the specific page where you're using it. This ensures the component only renders on the client, turning it into a single-page application (SPA) behavior for that route.
export const ssr = false;
If you add export const ssr = false to your root +layout.js, your entire app will only be rendered on the client, which essentially means you turn your app into an SPA.
Please check the example:
https://stackblitz.com/edit/sveltejs-kit-template-default-mpkt1fmm?description=The%20default%20SvelteKit%20template,%20generated%20with%20create-svelte&file=src%2Froutes%2F%2Bpage.js,src%2Froutes%2F%2Bpage.svelte,src%2Froutes%2FGrid.svelte&title=SvelteKit%20Default%20Template
Thanks for the reply. What would it take to add these two solutions to the SVAR DataGrid documentation?
I believe this can help more developers try SVAR DataGrid successfully.
The SvelteKit 5 documentation emphasizes SSR by default, and I do want the speed for much of the app, so I'm keen to do SSR when possible. For my specific goal, SSR with caching is ideal because the page changes once per day for all users, so SSR caching is a large speed up.
Much appreciated.
We will investigate this further and possibly provide a fix or update to the documentation to address SSR-related timing issues more comprehensively.
Hello,
Please check Svelte Grid 2.6.0, the issue must have been fixed there. https://docs.svar.dev/svelte/grid/whats_new
Please check Svelte Grid 2.6.0, the issue must have been fixed there. https://docs.svar.dev/svelte/grid/whats_new
Currently using 2.6.2 and issue is not fixed on my end. Still have to rely on CSR
EDIT: Current behavior https://github.com/svar-widgets/grid/blob/ef5efdba4fd4c9dc7a8d7a56dca1f307ca3c8577/svelte/src/components/Layout.svelte#L412-L415
- dblclick on cell from SSR grid
=>
api.exec("open-editor", { id: rowId, column: "foo" })=>s = this.getRow(rowId) // returns undefined (row not in store yet)=>y(s, c) // tries s["foo"] and causes TypeError
Hello @guilherme-n-l ,
Thank you for the update. I've tried to reproduce the issue you described using the latest version of @svar-ui/svelte-grid (2.6.2) and a standard SvelteKit template without disabling SSR (i.e., no export const ssr = false;). So far I haven't been able to trigger the error neither on page load nor when double-clicking a cell to activate the editor.
Here is my working example for reference: https://stackblitz.com/edit/sveltejs-kit-template-default-mpkt1fmm?description=The%20default%20SvelteKit%20template,%20generated%20with%20create-svelte&file=package.json,src%2Froutes%2F%2Bpage.js,src%2Froutes%2FGrid.svelte&title=SvelteKit%20Default%20Template
To help you find the cause, could you please provide the following details?
-
SvelteKit&Svelteversions: Which exact versions of@sveltejs/kitandsvelteare you using (from yourpackage.json)? -
If possible, a link to a StackBlitz or repository where the error is reliably reproducible would be helpful.
-
The complete console output from the browser when the error occurs.
Hi @Natasha-eng,
Update on the SVAR grid issue: couldn't reproduce on our side, but here's what we think is going on so you can keep an eye out:
Theory: hydration race in @svar-ui/svelte-grid (tested 2.6.2). When data comes from a load() and is rendered at SSR time, there's a small window during hydration where the grid's internal state isn't fully wired yet. An interaction landing in that window (dblclick → getRow()) blows up because the row map is still empty.
Worth watching:
- Grids fed from +page.ts / +page.server.ts load() (client-fetched data doesn't seem to trigger it).
- Handlers attached via init / api.on(...) that call getRow(), getCell(), or read getState().editor early.
- Flows where a user could realistically dblclick a cell in the first few hundred ms after load.
What we tried: minimal SvelteKit page, data in load(), auto-dblclick on first editable cell once it appears. Nothing fired. Could be machine too fast, wrong column/editor combo, or already fixed in 2.6.2.
Thanks!