@yolean/evidence 40.2.0 → 40.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yolean/evidence",
3
- "version": "40.2.0",
3
+ "version": "40.3.0",
4
4
  "description": "dependencies for evidence projects",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -35,9 +35,9 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@evidence-dev/preprocess": "npm:@yolean/evidence-preprocess@6.1.0",
38
- "@evidence-dev/sdk": "npm:@yolean/evidence-sdk@4.1.0",
38
+ "@evidence-dev/sdk": "npm:@yolean/evidence-sdk@4.2.0",
39
39
  "@evidence-dev/telemetry": "npm:@yolean/evidence-telemetry@2.1.4",
40
- "@evidence-dev/universal-sql": "npm:@yolean/evidence-universal-sql@3.0.2",
40
+ "@evidence-dev/universal-sql": "npm:@yolean/evidence-universal-sql@3.1.0",
41
41
  "@sveltejs/adapter-static": "3.0.11",
42
42
  "@sveltejs/kit": "2.70.4",
43
43
  "chalk": "^5.3.0",
@@ -10,15 +10,20 @@ import {
10
10
  import { profile } from '@evidence-dev/component-utilities/profile';
11
11
  import { toasts } from '@evidence-dev/component-utilities/stores';
12
12
  import { setTrackProxy } from '@evidence-dev/sdk/usql';
13
- import { addBasePath } from '@evidence-dev/sdk/utils/svelte';
13
+ import { addBasePath, addScopedBasePath, getDataScope } from '@evidence-dev/sdk/utils/svelte';
14
14
  import md5 from 'blueimp-md5';
15
15
 
16
16
  export const ssr = !dev;
17
17
  export const prerender = import.meta.env.VITE_EVIDENCE_SPA !== 'true';
18
18
  export const trailingSlash = 'always';
19
19
 
20
- const loadDB = async () => {
20
+ /**
21
+ * @param {string | null} scope deployment.dataScope value; the browser loads only that scope's data
22
+ */
23
+ const loadDB = async (scope) => {
21
24
  let renderedFiles = {};
25
+ /** @param {string} path */
26
+ const addDataPath = (path) => (browser ? addScopedBasePath(path, scope) : addBasePath(path));
22
27
 
23
28
  if (!browser) {
24
29
  const { readFile } = await import('fs/promises');
@@ -26,7 +31,7 @@ const loadDB = async () => {
26
31
  await readFile('./static/data/manifest.json', 'utf-8').catch(() => '{}')
27
32
  ));
28
33
  } else {
29
- const res = await fetch(addBasePath('/data/manifest.json'));
34
+ const res = await fetch(addDataPath('/data/manifest.json'));
30
35
  if (res.ok) ({ renderedFiles } = await res.json());
31
36
  }
32
37
  await profile(initDB);
@@ -45,30 +50,50 @@ const loadDB = async () => {
45
50
  );
46
51
  }
47
52
  } else {
48
- await profile(setParquetURLs, renderedFiles, { addBasePath });
53
+ await profile(setParquetURLs, renderedFiles, { addBasePath: addDataPath });
49
54
  await profile(updateSearchPath, Object.keys(renderedFiles));
50
55
  }
51
56
  };
52
57
 
53
- const database_initialization = profile(loadDB);
58
+ /** @type {Promise<void> | undefined} */
59
+ let database_initialization;
60
+ /** @type {string | null | undefined} */
61
+ let database_scope;
62
+ /**
63
+ * The server prerenders every scope from the full data set; the browser
64
+ * reloads when navigation crosses into another scope.
65
+ * @param {string | null} scope
66
+ */
67
+ const initializeDatabase = (scope) => {
68
+ if (!database_initialization || (browser && scope !== database_scope)) {
69
+ database_scope = scope;
70
+ database_initialization = profile(loadDB, scope);
71
+ }
72
+ return database_initialization;
73
+ };
54
74
 
55
75
  /**
56
76
  *
57
77
  * @param {string} routeHash
58
78
  * @param {string} paramsHash
59
79
  * @param {typeof fetch} fetch
80
+ * @param {string | null} scope
60
81
  * @returns {Promise<Record<string, unknown[]>>}
61
82
  */
62
- async function getPrerenderedQueries(routeHash, paramsHash, fetch) {
83
+ async function getPrerenderedQueries(routeHash, paramsHash, fetch, scope) {
63
84
  // get every query that's run in the component
64
- const res = await fetch(addBasePath(`/api/${routeHash}/${paramsHash}/all-queries.json`));
85
+ const res = await fetch(
86
+ addScopedBasePath(`/api/${routeHash}/${paramsHash}/all-queries.json`, scope)
87
+ );
65
88
  if (!res.ok) return {};
66
89
 
67
90
  const sql_cache_with_hashed_query_strings = await res.json();
68
91
 
69
92
  const resolved_entries = await Promise.all(
70
93
  Object.entries(sql_cache_with_hashed_query_strings).map(async ([query_name, query_hash]) => {
71
- const res = await fetch(addBasePath(`/api/prerendered_queries/${query_hash}.arrow`));
94
+ const res = await fetch(
95
+ addScopedBasePath(`/api/prerendered_queries/${query_hash}.arrow`, scope)
96
+ );
72
97
  if (!res.ok) return null;
73
98
 
74
99
  const table = await tableFromIPC(res);
@@ -86,10 +111,14 @@ const dummy_pages = new Map();
86
111
 
87
112
  /** @satisfies {import("./$types").LayoutLoad} */
88
113
  export const load = async ({ fetch, route, params, url }) => {
114
+ const scope = getDataScope(params);
115
+ // prerendering fetches the shared files; the build copies them into each scope
116
+ /** @param {string} path */
117
+ const addApiPath = (path) => (browser ? addScopedBasePath(path, scope) : addBasePath(path));
89
118
  const [{ customFormattingSettings }, pagesManifest, evidencemeta] = await Promise.all([
90
- fetch(addBasePath('/api/customFormattingSettings.json/GET.json')).then((x) => x.json()),
91
- fetch(addBasePath('/api/pagesManifest.json')).then((x) => x.json()),
92
- fetch(addBasePath(`/api/${route.id}/evidencemeta.json`))
119
+ fetch(addApiPath('/api/customFormattingSettings.json/GET.json')).then((x) => x.json()),
120
+ fetch(addApiPath('/api/pagesManifest.json')).then((x) => x.json()),
121
+ fetch(addApiPath(`/api/${route.id}/evidencemeta.json`))
93
122
  .then((x) => x.json())
94
123
  .catch(() => ({ queries: [] }))
95
124
  ]);
@@ -121,20 +150,21 @@ export const load = async ({ fetch, route, params, url }) => {
121
150
  dummy_pages.delete(url.pathname);
122
151
  }
123
152
 
124
- if (!browser) await database_initialization;
153
+ const database_ready = initializeDatabase(scope);
154
+ if (!browser) await database_ready;
125
155
  // account for potential changes in manifest (source query hmr)
126
156
  if (!browser && dev) await initDB();
127
157
 
128
158
  // let SSR saturate the cache first
129
159
  if (browser && isUserPage && prerender) {
130
- data = await getPrerenderedQueries(routeHash, paramsHash, fetch);
160
+ data = await getPrerenderedQueries(routeHash, paramsHash, fetch, scope);
131
161
  }
132
162
 
133
163
  /** @type {App.PageData["__db"]["query"]} */
134
164
  function query(sql, { query_name, callback = (x) => x } = {}) {
135
165
  if (browser) {
136
166
  return (async () => {
137
- await database_initialization;
167
+ await database_ready;
138
168
  const result = await usqlQuery(sql);
139
169
  return callback(result);
140
170
  })();
@@ -145,7 +175,8 @@ export const load = async ({ fetch, route, params, url }) => {
145
175
  route_hash: routeHash,
146
176
  additional_hash: paramsHash,
147
177
  query_name,
148
- prerendering: building
178
+ prerendering: building,
179
+ scope
149
180
  })
150
181
  );
151
182
  }
@@ -169,12 +200,14 @@ export const load = async ({ fetch, route, params, url }) => {
169
200
  __db: {
170
201
  query,
171
202
  async load() {
172
- return database_initialization;
203
+ return database_ready;
173
204
  },
174
205
  async updateParquetURLs(manifest) {
175
206
  // todo: maybe diff with old?
176
207
  const { renderedFiles } = JSON.parse(manifest);
177
- await profile(setParquetURLs, renderedFiles, { addBasePath });
208
+ await profile(setParquetURLs, renderedFiles, {
209
+ addBasePath: (path) => addScopedBasePath(path, scope)
210
+ });
178
211
  }
179
212
  },
180
213
  inputs,