MCPcopy Create free account
hub / github.com/react-static/react-static / prefetchData

Function prefetchData

packages/react-static/src/browser/index.js:278–344  ·  view source on GitHub ↗
(path, { priority } = {})

Source from the content-addressed store, hash-verified

276}
277
278export async function prefetchData(path, { priority } = {}) {
279 // Get route info so we can check if path has any data
280 const routeInfo = await getRouteInfo(path, { priority })
281
282 // Not a static route? Bail out.
283 if (!routeInfo) {
284 return
285 }
286
287 // Defer to the cache first. In dev mode, this should already be available from
288 // the call to getRouteInfo
289 if (routeInfo.sharedData) {
290 return getFullRouteData(routeInfo)
291 }
292
293 // Request and build the props one by one
294 routeInfo.sharedData = {}
295
296 // Request the template and loop over the routeInfo.sharedHashesByProp, requesting each prop
297 await Promise.all(
298 Object.keys(routeInfo.sharedHashesByProp).map(async key => {
299 const hash = routeInfo.sharedHashesByProp[key]
300
301 // Check the sharedDataByHash first
302 if (!sharedDataByHash[hash]) {
303 // Reuse request for duplicate inflight requests
304 try {
305 const staticDataPath = pathJoin(
306 process.env.REACT_STATIC_ASSETS_PATH,
307 `staticData/${hash}.json`
308 )
309 const absoluteStaticDataPath = makePathAbsolute(staticDataPath)
310
311 // If priority, get it immediately
312 if (priority) {
313 const { data: prop } = await axios.get(absoluteStaticDataPath)
314 sharedDataByHash[hash] = prop
315 } else {
316 // Non priority, share inflight requests and use pool
317 if (!inflightPropHashes[hash]) {
318 inflightPropHashes[hash] = requestPool.add(() =>
319 axios.get(absoluteStaticDataPath)
320 )
321 }
322 const { data: prop } = await inflightPropHashes[hash]
323 // Place it in the cache
324 sharedDataByHash[hash] = prop
325 }
326 } catch (err) {
327 console.log(
328 'Error: There was an error retrieving a prop for this route! hashID:',
329 hash
330 )
331 console.error(err)
332 }
333 if (!priority) {
334 delete inflightPropHashes[hash]
335 }

Callers 1

prefetchFunction · 0.85

Calls 4

getFullRouteDataFunction · 0.90
pathJoinFunction · 0.90
makePathAbsoluteFunction · 0.90
getRouteInfoFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…