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

Function preinitStyle

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js:6584–6671  ·  view source on GitHub ↗
(
  href: string,
  precedence: ?string,
  options?: ?PreinitStyleOptions,
)

Source from the content-addressed store, hash-verified

6582}
6583
6584function preinitStyle(
6585 href: string,
6586 precedence: ?string,
6587 options?: ?PreinitStyleOptions,
6588): void {
6589 const request = resolveRequest();
6590 if (!request) {
6591 // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also
6592 // possibly get them from the stack if we are not in an async context. Since we were not able to resolve
6593 // the resources for this call in either case we opt to do nothing. We can consider making this a warning
6594 // but there may be times where calling a function outside of render is intentional (i.e. to warm up data
6595 // fetching) and we don't want to warn in those cases.
6596 previousDispatcher.S(/* preinitStyle */ href, precedence, options);
6597 return;
6598 }
6599 const resumableState = getResumableState(request);
6600 const renderState = getRenderState(request);
6601 if (href) {
6602 precedence = precedence || 'default';
6603 const key = getResourceKey(href);
6604
6605 let styleQueue = renderState.styles.get(precedence);
6606 const hasKey = resumableState.styleResources.hasOwnProperty(key);
6607 const resourceState = hasKey
6608 ? resumableState.styleResources[key]
6609 : undefined;
6610 if (resourceState !== EXISTS) {
6611 // We are going to create this resource now so it is marked as Exists
6612 resumableState.styleResources[key] = EXISTS;
6613
6614 // If this is the first time we've encountered this precedence we need
6615 // to create a StyleQueue
6616 if (!styleQueue) {
6617 styleQueue = {
6618 precedence: stringToChunk(escapeTextForBrowser(precedence)),
6619 rules: ([]: Array<Chunk | PrecomputedChunk>),
6620 hrefs: ([]: Array<Chunk | PrecomputedChunk>),
6621 sheets: (new Map(): Map<string, StylesheetResource>),
6622 };
6623 renderState.styles.set(precedence, styleQueue);
6624 }
6625
6626 const resource = {
6627 state: PENDING,
6628 props: Object.assign(
6629 ({
6630 rel: 'stylesheet',
6631 href,
6632 'data-precedence': precedence,
6633 }: StylesheetProps),
6634 options,
6635 ),
6636 };
6637
6638 if (resourceState) {
6639 // When resourceState is truty it is a Preload state. We cast it for clarity
6640 const preloadState: Preloaded | PreloadedWithCredentials =
6641 resourceState;

Callers

nothing calls this directly

Calls 11

resolveRequestFunction · 0.90
getResumableStateFunction · 0.90
getRenderStateFunction · 0.90
stringToChunkFunction · 0.90
flushResourcesFunction · 0.90
escapeTextForBrowserFunction · 0.85
adoptPreloadCredentialsFunction · 0.85
setMethod · 0.80
getResourceKeyFunction · 0.70
getMethod · 0.65
assignMethod · 0.45

Tested by

no test coverage detected