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

Fetching data #864

Open
martavis opened this issue Jun 30, 2020 · 2 comments
Open

Fetching data #864

martavis opened this issue Jun 30, 2020 · 2 comments

Comments

@martavis
Copy link

Hi. Charge looks promising, but I'm not clear on fetching data. I noticed an emphasis on using JSON files for data, but does this project support fetching data from a CMS and creating a build with that data?

@brandonweiss
Copy link
Owner

Hey, thanks! That’s a good question—I didn’t really cover that in the documentation.

So one way to go about fetching data from a remote source is to write a script that fetches the data, writes JSON files to the data folder, and then run it before you build/serve, something like: node ./load_data.js && charge serve

I’ve done that on a few projects and it works well if the data in the remote source is infrequently updated, but it doesn’t work well if you’re changing the data in the remote source a lot, because every time you change it you’ll need to kill the server and start it up again (or run the ./load_data.js script again).

Someone in another issue mentioned the idea of allowing data files to be .js instead of just .json files. They were mostly coming at it from an ergonomics perspective, but one benefit of it is that in a .js data file you could do your data fetching there too. That means that in development the data is going to be re-requested any time you change a JSX/MDX file. That’s great if the data in the remote source changes a lot, but if it doesn’t then that could potentially create an unnecessarily slow feedback cycle.

So maybe I need to both support JS data files and add documentation explaining the two ways of fetching data and their tradeoffs, then users can decide which one to use depending on how frequently they think their data will change.

What do you think?

@clsource
Copy link

clsource commented Jul 7, 2020

I believe is a good idea to implement js files as data sources (additionally to JSON files).
You can always return an object which has both static data types and functions.

The function can be memoized (https://en.wikipedia.org/wiki/Memoization) so the performance is improved. But this would depend on each user's implementation. 👍

As per getting remote dynamic data. The end result is just an html website. So you can always use vanilla js or similar helpers inside the <head> tags to handle dynamic data.

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

3 participants