ft-interactive / starter-kit

A template for IG pages
64 stars 15 forks source link

Fix getData/fetchData functions to work in scripts and on CircleCI builds #323

Closed pandringa closed 8 months ago

pandringa commented 8 months ago

The existing implementation of the getData and fetchData methods in config/data.js are somewhat broken, because they don't work inside the vite build: they presume the local files are available to read (as they are when you run npm run data) — but this breaks inside CircleCI or npm run build after the Vite build finishes.

This update tweaks the loading script to ensure those functions always run within the vite context, allowing them to use dynamic import() statement and reference Vite helpers like import.meta.glob like any other script. This makes the script much more isomorphic, so that it works either via the CLI command (npm run data) or during the build phase (npm run build).

As before, template users should continue to add their own custom data-fetching code inside config/data.js -> fetchData(). By default, this function is run every time the page builds — but if a user runs npm run data, the output of that method will be cached within config/data.json and future builds will skip the live fetch.