MCPcopy Index your code
hub / github.com/react/react / preloadModule

Function preloadModule

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js:6511–6582  ·  view source on GitHub ↗
(
  href: string,
  options?: ?PreloadModuleImplOptions,
)

Source from the content-addressed store, hash-verified

6509}
6510
6511function preloadModule(
6512 href: string,
6513 options?: ?PreloadModuleImplOptions,
6514): void {
6515 const request = resolveRequest();
6516 if (!request) {
6517 // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also
6518 // possibly get them from the stack if we are not in an async context. Since we were not able to resolve
6519 // the resources for this call in either case we opt to do nothing. We can consider making this a warning
6520 // but there may be times where calling a function outside of render is intentional (i.e. to warm up data
6521 // fetching) and we don't want to warn in those cases.
6522 previousDispatcher.m(/* preloadModule */ href, options);
6523 return;
6524 }
6525 const resumableState = getResumableState(request);
6526 const renderState = getRenderState(request);
6527 if (href) {
6528 const key = getResourceKey(href);
6529 const as =
6530 options && typeof options.as === 'string' ? options.as : 'script';
6531
6532 let resource;
6533 switch (as) {
6534 case 'script': {
6535 if (resumableState.moduleScriptResources.hasOwnProperty(key)) {
6536 // we can return if we already have this resource
6537 return;
6538 }
6539 resource = ([]: Resource);
6540 resumableState.moduleScriptResources[key] =
6541 options &&
6542 (typeof options.crossOrigin === 'string' ||
6543 typeof options.integrity === 'string')
6544 ? [options.crossOrigin, options.integrity]
6545 : PRELOAD_NO_CREDS;
6546 renderState.preloads.moduleScripts.set(key, resource);
6547 break;
6548 }
6549 default: {
6550 const hasAsType =
6551 resumableState.moduleUnknownResources.hasOwnProperty(as);
6552 let resources;
6553 if (hasAsType) {
6554 resources = resumableState.unknownResources[as];
6555 if (resources.hasOwnProperty(key)) {
6556 // we can return if we already have this resource
6557 return;
6558 }
6559 } else {
6560 resources = ({}: ResumableState['moduleUnknownResources']['asType']);
6561 resumableState.moduleUnknownResources[as] = resources;
6562 }
6563 resource = ([]: Resource);
6564 resources[key] = PRELOAD_NO_CREDS;
6565 }
6566 }
6567
6568 pushLinkImpl(

Callers

nothing calls this directly

Calls 10

resolveRequestFunction · 0.90
getResumableStateFunction · 0.90
getRenderStateFunction · 0.90
flushResourcesFunction · 0.90
pushLinkImplFunction · 0.85
setMethod · 0.80
addMethod · 0.80
getResourceKeyFunction · 0.70
mMethod · 0.45
assignMethod · 0.45

Tested by

no test coverage detected