@@ -18,6 +18,7 @@ export default function Stories({
18
18
onStoriesStart = ( ) => { } ,
19
19
classNames = { } ,
20
20
pauseStoryWhenInActiveWindow = true ,
21
+ preloadNextStory = false ,
21
22
} : IStoryProps ) : JSX . Element | null {
22
23
const storiesWithIndex : IStoryIndexedObject [ ] = useMemo ( ( ) => {
23
24
return utilities . transformStories ( stories , defaultDuration ) ;
@@ -33,6 +34,25 @@ export default function Stories({
33
34
const hasCalledEndedCb = useRef < any > ( false ) ;
34
35
const hasCalledStartedCb = useRef < any > ( false ) ;
35
36
37
+ // Preload stories
38
+ const preloadedStoriesUrlsRef = useRef < string [ ] > ( [ ] ) ;
39
+ const preloadStory = ( index : number ) => {
40
+ if ( ! preloadNextStory ) return ;
41
+
42
+ if (
43
+ index >= storiesWithIndex . length ||
44
+ preloadedStoriesUrlsRef . current . indexOf ( storiesWithIndex [ index ] . url ) >= 0
45
+ )
46
+ return ;
47
+
48
+ if ( utilities . preloadStory (
49
+ storiesWithIndex [ index ] . type ,
50
+ storiesWithIndex [ index ] . url ,
51
+ ) ) {
52
+ preloadedStoriesUrlsRef . current . push ( storiesWithIndex [ index ] . url ) ;
53
+ }
54
+ } ;
55
+
36
56
useEffect ( ( ) => {
37
57
if ( ! hasCalledStartedCb . current ) {
38
58
hasCalledStartedCb . current = true ;
@@ -41,10 +61,14 @@ export default function Stories({
41
61
} , [ onStoriesStart ] ) ;
42
62
43
63
useEffect ( ( ) => {
64
+ console . log ( 'currentIndex change' , currentIndex ) ;
44
65
const story = storiesWithIndex [ currentIndex ] ;
45
- if ( story ) {
46
- setSelectedStory ( story ) ;
47
- }
66
+
67
+ if ( ! story ) return ;
68
+
69
+ setSelectedStory ( story ) ;
70
+
71
+ preloadStory ( currentIndex + 1 ) ;
48
72
} , [ currentIndex , stories ] ) ;
49
73
50
74
function handleNextClick ( ) {
@@ -68,6 +92,7 @@ export default function Stories({
68
92
return ;
69
93
}
70
94
setSelectedStory ( ( prev ) => {
95
+
71
96
if ( ! prev ) {
72
97
return storiesWithIndex [ 0 ] ;
73
98
}
@@ -84,9 +109,12 @@ export default function Stories({
84
109
}
85
110
86
111
useEffect ( ( ) => {
87
- if ( selectedStory ) {
88
- onStoryChange ( selectedStory . index ) ;
89
- }
112
+ console . log ( 'selectedStory change' , { sel : selectedStory ?? "" , index : currentIndex } ) ;
113
+ if ( ! selectedStory ) return ;
114
+
115
+ onStoryChange ( selectedStory . index ) ;
116
+
117
+ preloadStory ( selectedStory . index + 1 ) ;
90
118
} , [ selectedStory ] ) ;
91
119
92
120
hooks . usePausableTimeout (
@@ -110,6 +138,7 @@ export default function Stories({
110
138
defaultDuration,
111
139
isPaused,
112
140
classNames,
141
+ preloadNextStory,
113
142
} ;
114
143
115
144
if ( ! selectedStory ) {
0 commit comments