@@ -5,7 +5,8 @@ import React, {
55 Fragment ,
66 Suspense ,
77 memo ,
8- useState
8+ useState ,
9+ useSyncExternalStore
910} from 'preact/compat' ;
1011import { logCall , getLog , clearLog } from '../../../test/_util/logCall' ;
1112import {
@@ -98,6 +99,46 @@ describe('suspense hydration', () => {
9899 } ) ;
99100 } ) ;
100101
102+ it ( 'should use the server snapshot when suspended hydration resumes' , async ( ) => {
103+ scratch . innerHTML = '<div>server</div>' ;
104+ clearLog ( ) ;
105+
106+ const snapshots = [ ] ;
107+ const [ Lazy , resolve ] = createLazy ( ) ;
108+
109+ function StoreValue ( ) {
110+ const value = useSyncExternalStore (
111+ ( ) => ( ) => { } ,
112+ ( ) => {
113+ snapshots . push ( 'client' ) ;
114+ return 'client' ;
115+ } ,
116+ ( ) => {
117+ snapshots . push ( 'server' ) ;
118+ return 'server' ;
119+ }
120+ ) ;
121+ return < div > { value } </ div > ;
122+ }
123+
124+ hydrate (
125+ < Suspense >
126+ < Lazy />
127+ </ Suspense > ,
128+ scratch
129+ ) ;
130+ rerender ( ) ;
131+ expect ( snapshots ) . to . deep . equal ( [ ] ) ;
132+
133+ await resolve ( ( ) => < StoreValue /> ) ;
134+ rerender ( ) ;
135+
136+ const [ first , ...rest ] = snapshots ;
137+ expect ( first ) . to . equal ( 'server' ) ;
138+ expect ( rest . every ( snap => snap === 'client' ) ) . to . equal ( true ) ;
139+ expect ( scratch . innerHTML ) . to . equal ( '<div>client</div>' ) ;
140+ } ) ;
141+
101142 it ( 'Should not crash when oldVNode._children is null during shouldComponentUpdate optimization' , ( ) => {
102143 const originalHtml = '<div>Hello</div>' ;
103144 scratch . innerHTML = originalHtml ;
0 commit comments