@@ -38,6 +38,7 @@ import {
3838 stringToPrecomputedChunk ,
3939 clonePrecomputedChunk ,
4040} from 'react-server/src/ReactServerStreamConfig' ;
41+ import { resolveResources , pingRequest } from 'react-server/src/ReactFizzServer' ;
4142
4243import isAttributeNameSafe from '../shared/isAttributeNameSafe' ;
4344import isUnitlessNumber from '../shared/isUnitlessNumber' ;
@@ -79,30 +80,15 @@ import {
7980import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals' ;
8081const ReactDOMCurrentDispatcher = ReactDOMSharedInternals . Dispatcher ;
8182
82- const ReactDOMServerDispatcher = enableFloat
83- ? {
84- prefetchDNS,
85- preconnect,
86- preload,
87- preinit,
88- }
89- : { } ;
90-
91- let currentResources : null | Resources = null ;
92- const currentResourcesStack = [ ] ;
93-
94- export function prepareToRender ( resources : Resources ) : mixed {
95- currentResourcesStack . push ( currentResources ) ;
96- currentResources = resources ;
83+ const ReactDOMServerDispatcher = {
84+ prefetchDNS,
85+ preconnect,
86+ preload,
87+ preinit,
88+ } ;
9789
98- const previousHostDispatcher = ReactDOMCurrentDispatcher . current ;
90+ export function prepareHostDispatcher ( ) {
9991 ReactDOMCurrentDispatcher . current = ReactDOMServerDispatcher ;
100- return previousHostDispatcher ;
101- }
102-
103- export function cleanupAfterRender ( previousDispatcher : mixed ) {
104- currentResources = currentResourcesStack . pop ( ) ;
105- ReactDOMCurrentDispatcher . current = previousDispatcher ;
10692}
10793
10894// Used to distinguish these contexts from ones used in other renderers.
@@ -4804,16 +4790,18 @@ function getResourceKey(as: string, href: string): string {
48044790}
48054791
48064792export function prefetchDNS ( href : string , options ?: mixed ) {
4807- if ( ! currentResources ) {
4808- // While we expect that preconnect calls are primarily going to be observed
4809- // during render because effects and events don't run on the server it is
4810- // still possible that these get called in module scope. This is valid on
4811- // the client since there is still a document to interact with but on the
4812- // server we need a request to associate the call to. Because of this we
4813- // simply return and do not warn.
4793+ if ( ! enableFloat ) {
4794+ return ;
4795+ }
4796+ const resources = resolveResources ( ) ;
4797+ if ( ! resources ) {
4798+ // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also
4799+ // possibly get them from the stack if we are not in an async context. Since we were not able to resolve
4800+ // the resources for this call in either case we opt to do nothing. We can consider making this a warning
4801+ // but there may be times where calling a function outside of render is intentional (i.e. to warm up data
4802+ // fetching) and we don't want to warn in those cases.
48144803 return ;
48154804 }
4816- const resources = currentResources ;
48174805 if ( __DEV__ ) {
48184806 if ( typeof href !== 'string' || ! href ) {
48194807 console . error (
@@ -4858,17 +4846,19 @@ export function prefetchDNS(href: string, options?: mixed) {
48584846 }
48594847}
48604848
4861- export function preconnect ( href : string , options ?: { crossOrigin ?: string } ) {
4862- if ( ! currentResources ) {
4863- // While we expect that preconnect calls are primarily going to be observed
4864- // during render because effects and events don't run on the server it is
4865- // still possible that these get called in module scope. This is valid on
4866- // the client since there is still a document to interact with but on the
4867- // server we need a request to associate the call to. Because of this we
4868- // simply return and do not warn.
4849+ export function preconnect ( href : string , options ?: ?{ crossOrigin ?: string } ) {
4850+ if ( ! enableFloat ) {
4851+ return ;
4852+ }
4853+ const resources = resolveResources ( ) ;
4854+ if ( ! resources ) {
4855+ // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also
4856+ // possibly get them from the stack if we are not in an async context. Since we were not able to resolve
4857+ // the resources for this call in either case we opt to do nothing. We can consider making this a warning
4858+ // but there may be times where calling a function outside of render is intentional (i.e. to warm up data
4859+ // fetching) and we don't want to warn in those cases.
48694860 return;
48704861 }
4871- const resources = currentResources ;
48724862 if ( __DEV__ ) {
48734863 if ( typeof href !== 'string' || ! href ) {
48744864 console . error (
@@ -4917,24 +4907,25 @@ export function preconnect(href: string, options?: {crossOrigin?: string}) {
49174907 }
49184908}
49194909
4920- type PreloadAs = 'style' | 'font' | 'script' ;
49214910type PreloadOptions = {
4922- as : PreloadAs ,
4911+ as : string ,
49234912 crossOrigin ?: string ,
49244913 integrity ?: string ,
49254914 type ?: string ,
49264915} ;
49274916export function preload ( href : string , options : PreloadOptions ) {
4928- if ( ! currentResources ) {
4929- // While we expect that preload calls are primarily going to be observed
4930- // during render because effects and events don't run on the server it is
4931- // still possible that these get called in module scope. This is valid on
4932- // the client since there is still a document to interact with but on the
4933- // server we need a request to associate the call to. Because of this we
4934- // simply return and do not warn.
4917+ if ( ! enableFloat ) {
4918+ return ;
4919+ }
4920+ const resources = resolveResources ( ) ;
4921+ if ( ! resources ) {
4922+ // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also
4923+ // possibly get them from the stack if we are not in an async context. Since we were not able to resolve
4924+ // the resources for this call in either case we opt to do nothing. We can consider making this a warning
4925+ // but there may be times where calling a function outside of render is intentional (i.e. to warm up data
4926+ // fetching) and we don't want to warn in those cases.
49354927 return ;
49364928 }
4937- const resources = currentResources ;
49384929 if ( __DEV__ ) {
49394930 if ( typeof href !== 'string' || ! href ) {
49404931 console . error (
@@ -5055,27 +5046,30 @@ export function preload(href: string, options: PreloadOptions) {
50555046 resources. explicitOtherPreloads . add ( resource ) ;
50565047 }
50575048 }
5049+ pingRequest ( ) ;
50585050 }
50595051}
50605052
5061- type PreinitAs = 'style' | 'script' ;
50625053type PreinitOptions = {
5063- as : PreinitAs ,
5054+ as : string ,
50645055 precedence ?: string ,
50655056 crossOrigin ?: string ,
50665057 integrity ?: string ,
50675058} ;
50685059export function preinit ( href : string , options : PreinitOptions ) : void {
5069- if ( ! currentResources ) {
5070- // While we expect that preinit calls are primarily going to be observed
5071- // during render because effects and events don't run on the server it is
5072- // still possible that these get called in module scope. This is valid on
5073- // the client since there is still a document to interact with but on the
5074- // server we need a request to associate the call to. Because of this we
5075- // simply return and do not warn.
5060+ if ( ! enableFloat ) {
5061+ return ;
5062+ }
5063+ const resources = resolveResources ( ) ;
5064+ if ( ! resources ) {
5065+ // In async contexts we can sometimes resolve resources from AsyncLocalStorage. If we can't we can also
5066+ // possibly get them from the stack if we are not in an async context. Since we were not able to resolve
5067+ // the resources for this call in either case we opt to do nothing. We can consider making this a warning
5068+ // but there may be times where calling a function outside of render is intentional (i.e. to warm up data
5069+ // fetching) and we don't want to warn in those cases.
50765070 return;
50775071 }
5078- preinitImpl ( currentResources , href, options) ;
5072+ preinitImpl ( resources , href , options ) ;
50795073}
50805074
50815075// On the server, preinit may be called outside of render when sending an
@@ -5297,7 +5291,7 @@ function preinitImpl(
52975291
52985292function preloadPropsFromPreloadOptions (
52995293 href : string ,
5300- as : PreloadAs ,
5294+ as : string ,
53015295 options : PreloadOptions ,
53025296) : PreloadProps {
53035297 return {
0 commit comments