-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdata.js
66 lines (57 loc) · 2.49 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @file
* Data downloading and configuration.
*
* Use this file to do any custom data-fetching you might need.
* Maybe you want to download it from an API or Google Sheet,
* or read/process some files from the data/ directory.
*
* Note that this function will be called only at BUILD time
* on the server: that is, when you push changes to Github
* and CircleCI rebuilds the HTML. If you know the data will change,
* you should load it here then add a useEffect() in app.jsx to fetch
* the latest version on the client.
*/
// import getArchieDoc from '../util/getArchieDoc.js';
// eslint-disable-next-line no-unused-vars
export async function fetchData(mode) {
/**
* @TODO Add any data downloading or parsing logic here.
* The return value of this method will be available
* inside the context provider as "context.data."
*
* If 'mode' is either 'development' or 'production', the function is running at build time.
* If 'mode' is 'cli', the function is running manually via "npm run download."
*
* Note that if you are running this function at build time, any files you access must be included
* in the vite build. This means you should either read data from public/, or (preferrably) only
* use dynamic import() statements to load.
*
* See below for examples of how to load raw text/csv or JSON files:
*/
// We'll want to set story to null if not using an ArchieML doc
const story = null;
// const story = await getArchieDoc();
// This { query: '?raw' } option imports files as plaintext
const dataFiles = import.meta.glob('../data/*.csv', { query: '?raw', import: 'default' });
const csv = await dataFiles['../data/example.csv']();
// JSON files can be import()-ed directly
const json = (await import('../data/example.json')).default;
// Remote fetch() calls can also work
return { story, csv, json };
}
export default async function getData(mode = 'development') {
/**
* DON'T add your data-loading code in this function - instead, use fetchData() above.
* This helper checks for a local data file (generated by scripts/data.js), and loads it if available.
* Otherwise, it calls fetchData() above to live-reload data at build time.
*/
try {
const files = import.meta.glob('./data.json');
if (files['./data.json']) return files['./data.json']();
throw new Error('No data file');
} catch (e) {
console.log('Falling back to dynamic fetchData() function...'); // eslint-disable-line no-console
return fetchData(mode);
}
}