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

plugin cannot be used in any system with SSR builds (eg Gatsby static site builder for React) #23

Open
bayareacoder opened this issue Jun 23, 2022 · 0 comments

Comments

@bayareacoder
Copy link

bayareacoder commented Jun 23, 2022

For a site builder as Gatsby (gatsbyjs.com) any React component needs to be build-able on both server-side and client.
Since for SSR document will be undefined, this plugin cannot be used with react-chartist.
I modified the code so it's not self-calling, relying on document, when importing the module but just exports a function:

function chartistPluginFillDonut(document, Chartist) {
<the original plugin code>
}

Then in the calling module:

import chartistPluginFillDonut from "../../plugins/chartist-plugin-fill-donut";
//...

and within the functional React component that uses Chartist:

  useEffect(() => {
    if (document) {
      chartistPluginFillDonut(document, Chartist);
    }
  }, []);

to only execute the function upon component mount on the client

Then options will need to be specified with ? since at SSR creation time the fillDonut function will not yet be defined. See also #22 for the responsiveFillDonutOptions option I added:

  const options = {
    donut: true,
    donutWidth: 10,
    startAngle: 190,
    total: max - min,
    showLabel: false,
    plugins: [Chartist.plugins?.fillDonut?.(fillDonutOptions)],
    fillDonutOptions,
    responsiveFillDonutOptions,
  };

It would be nice to make the changes so this plugin can be used in a React site builder without the above manual edits.

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

1 participant