MCPcopy
hub / github.com/facebook/react / prefetchDNS

Function prefetchDNS

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js:6195–6246  ·  view source on GitHub ↗
(href: string)

Source from the content-addressed store, hash-verified

6193}
6194
6195function prefetchDNS(href: string) {
6196 const request = resolveRequest();
6197 if (!request) {
6198 // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also
6199 // possibly get them from the stack if we are not in an async context. Since we were not able to resolve
6200 // the resources for this call in either case we opt to do nothing. We can consider making this a warning
6201 // but there may be times where calling a function outside of render is intentional (i.e. to warm up data
6202 // fetching) and we don't want to warn in those cases.
6203 previousDispatcher.D(/* prefetchDNS */ href);
6204 return;
6205 }
6206 const resumableState = getResumableState(request);
6207 const renderState = getRenderState(request);
6208
6209 if (typeof href === 'string' && href) {
6210 const key = getResourceKey(href);
6211 if (!resumableState.dnsResources.hasOwnProperty(key)) {
6212 resumableState.dnsResources[key] = EXISTS;
6213
6214 const headers = renderState.headers;
6215 let header;
6216 if (
6217 headers &&
6218 headers.remainingCapacity > 0 &&
6219 // Compute the header since we might be able to fit it in the max length
6220 ((header = getPrefetchDNSAsHeader(href)),
6221 // We always consume the header length since once we find one header that doesn't fit
6222 // we assume all the rest won't as well. This is to avoid getting into a situation
6223 // where we have a very small remaining capacity but no headers will ever fit and we end
6224 // up constantly trying to see if the next resource might make it. In the future we can
6225 // make this behavior different between render and prerender since in the latter case
6226 // we are less sensitive to the current requests runtime per and more sensitive to maximizing
6227 // headers.
6228 (headers.remainingCapacity -= header.length + 2) >= 0)
6229 ) {
6230 // Store this as resettable in case we are prerendering and postpone in the Shell
6231 renderState.resets.dns[key] = EXISTS;
6232 if (headers.preconnects) {
6233 headers.preconnects += ', ';
6234 }
6235 // $FlowFixMe[unsafe-addition]: we assign header during the if condition
6236 headers.preconnects += header;
6237 } else {
6238 // Encode as element
6239 const resource: Resource = [];
6240 pushLinkImpl(resource, ({href, rel: 'dns-prefetch'}: PreconnectProps));
6241 renderState.preconnects.add(resource);
6242 }
6243 }
6244 flushResources(request);
6245 }
6246}
6247
6248function preconnect(href: string, crossOrigin: ?CrossOriginEnum) {
6249 const request = resolveRequest();

Callers

nothing calls this directly

Calls 8

resolveRequestFunction · 0.90
getResumableStateFunction · 0.90
getRenderStateFunction · 0.90
flushResourcesFunction · 0.90
getPrefetchDNSAsHeaderFunction · 0.85
pushLinkImplFunction · 0.85
addMethod · 0.80
getResourceKeyFunction · 0.70

Tested by

no test coverage detected