(src: string, options?: ?PreinitScriptOptions)
| 6671 | } |
| 6672 | |
| 6673 | function preinitScript(src: string, options?: ?PreinitScriptOptions): void { |
| 6674 | const request = resolveRequest(); |
| 6675 | if (!request) { |
| 6676 | // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also |
| 6677 | // possibly get them from the stack if we are not in an async context. Since we were not able to resolve |
| 6678 | // the resources for this call in either case we opt to do nothing. We can consider making this a warning |
| 6679 | // but there may be times where calling a function outside of render is intentional (i.e. to warm up data |
| 6680 | // fetching) and we don't want to warn in those cases. |
| 6681 | previousDispatcher.X(/* preinitScript */ src, options); |
| 6682 | return; |
| 6683 | } |
| 6684 | const resumableState = getResumableState(request); |
| 6685 | const renderState = getRenderState(request); |
| 6686 | if (src) { |
| 6687 | const key = getResourceKey(src); |
| 6688 | |
| 6689 | const hasKey = resumableState.scriptResources.hasOwnProperty(key); |
| 6690 | const resourceState = hasKey |
| 6691 | ? resumableState.scriptResources[key] |
| 6692 | : undefined; |
| 6693 | if (resourceState !== EXISTS) { |
| 6694 | // We are going to create this resource now so it is marked as Exists |
| 6695 | resumableState.scriptResources[key] = EXISTS; |
| 6696 | |
| 6697 | const props: ScriptProps = Object.assign( |
| 6698 | ({ |
| 6699 | src, |
| 6700 | async: true, |
| 6701 | }: ScriptProps), |
| 6702 | options, |
| 6703 | ); |
| 6704 | if (resourceState) { |
| 6705 | // When resourceState is truty it is a Preload state. We cast it for clarity |
| 6706 | const preloadState: Preloaded | PreloadedWithCredentials = |
| 6707 | resourceState; |
| 6708 | if (preloadState.length === 2) { |
| 6709 | adoptPreloadCredentials(props, preloadState); |
| 6710 | } |
| 6711 | |
| 6712 | const preloadResource = renderState.preloads.scripts.get(key); |
| 6713 | if (preloadResource) { |
| 6714 | // the preload resource exists was created in this render. Now that we have |
| 6715 | // a script resource which will emit earlier than a preload would if it |
| 6716 | // hasn't already flushed we prevent it from flushing by zeroing the length |
| 6717 | preloadResource.length = 0; |
| 6718 | } |
| 6719 | } |
| 6720 | |
| 6721 | const resource: Resource = []; |
| 6722 | // Add to the script flushing queue |
| 6723 | renderState.scripts.add(resource); |
| 6724 | // encode the tag as Chunks |
| 6725 | pushScriptImpl(resource, props); |
| 6726 | // Notify the request that there are resources to flush even if no work is currently happening |
| 6727 | flushResources(request); |
| 6728 | } |
| 6729 | return; |
| 6730 | } |
nothing calls this directly
no test coverage detected