Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Does it support server side rendering? #604

Open
ZeRego opened this issue Apr 16, 2024 · 1 comment
Open

[Question] Does it support server side rendering? #604

ZeRego opened this issue Apr 16, 2024 · 1 comment

Comments

@ZeRego
Copy link

ZeRego commented Apr 16, 2024

I want to render in the server a react page that includes a Vega chart. Is this possible?

At the moment the example code only returns an empty div.

<div style="width:100%;height:100%;min-height:300px"/>

Server

const html = renderToStaticMarkup(<VegaViz spec={vegaSpec}/>);

Component

const VegaViz = ({spec}: { spec: vega.Spec }) => {
    return (
        <Vega
            style={{
                width: '100%',
                height: '100%',
                minHeight: 300,
            }}
            spec={spec}
            renderer={'svg'}
        />
    );
};
@jardelva96
Copy link

Rendering a Vega chart on the server-side using React can be a bit tricky because Vega (and Vega-Lite) relies on browser-specific APIs (like the DOM) to render charts. The renderToStaticMarkup function from react-dom/server renders components to static HTML and doesn't execute any client-side code or lifecycle methods, which is why your Vega chart isn't appearing.

However, you can achieve server-side rendering (SSR) by using a workaround that involves pre-rendering the chart on the server and then hydrating it on the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants