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

Function preload

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js:4695–4771  ·  view source on GitHub ↗
(href: string, as: string, options?: ?PreloadImplOptions)

Source from the content-addressed store, hash-verified

4693}
4694
4695function preload(href: string, as: string, options?: ?PreloadImplOptions) {
4696 previousDispatcher.L(/* preload */ href, as, options);
4697 const ownerDocument = getGlobalDocument();
4698 if (ownerDocument && href && as) {
4699 let preloadSelector = `link[rel="preload"][as="${escapeSelectorAttributeValueInsideDoubleQuotes(
4700 as,
4701 )}"]`;
4702 if (as === 'image') {
4703 if (options && options.imageSrcSet) {
4704 preloadSelector += `[imagesrcset="${escapeSelectorAttributeValueInsideDoubleQuotes(
4705 options.imageSrcSet,
4706 )}"]`;
4707 if (typeof options.imageSizes === 'string') {
4708 preloadSelector += `[imagesizes="${escapeSelectorAttributeValueInsideDoubleQuotes(
4709 options.imageSizes,
4710 )}"]`;
4711 }
4712 } else {
4713 preloadSelector += `[href="${escapeSelectorAttributeValueInsideDoubleQuotes(
4714 href,
4715 )}"]`;
4716 }
4717 } else {
4718 preloadSelector += `[href="${escapeSelectorAttributeValueInsideDoubleQuotes(
4719 href,
4720 )}"]`;
4721 }
4722 // Some preloads are keyed under their selector. This happens when the preload is for
4723 // an arbitrary type. Other preloads are keyed under the resource key they represent a preload for.
4724 // Here we figure out which key to use to determine if we have a preload already.
4725 let key = preloadSelector;
4726 switch (as) {
4727 case 'style':
4728 key = getStyleKey(href);
4729 break;
4730 case 'script':
4731 key = getScriptKey(href);
4732 break;
4733 }
4734 if (!preloadPropsMap.has(key)) {
4735 const preloadProps = Object.assign(
4736 ({
4737 rel: 'preload',
4738 // There is a bug in Safari where imageSrcSet is not respected on preload links
4739 // so we omit the href here if we have imageSrcSet b/c safari will load the wrong image.
4740 // This harms older browers that do not support imageSrcSet by making their preloads not work
4741 // but this population is shrinking fast and is already small so we accept this tradeoff.
4742 href:
4743 as === 'image' && options && options.imageSrcSet ? undefined : href,
4744 as,
4745 }: PreloadProps),
4746 options,
4747 );
4748 preloadPropsMap.set(key, preloadProps);
4749
4750 if (null === ownerDocument.querySelector(preloadSelector)) {
4751 if (
4752 as === 'style' &&

Callers

nothing calls this directly

Calls 11

setInitialPropertiesFunction · 0.90
markNodeAsHoistableFunction · 0.90
getGlobalDocumentFunction · 0.85
getStyleKeyFunction · 0.85
getScriptKeyFunction · 0.85
getScriptSelectorFromKeyFunction · 0.85
setMethod · 0.80
hasMethod · 0.65
assignMethod · 0.45

Tested by

no test coverage detected