@@ -118,12 +118,28 @@ const server = setupServer(
118
118
)
119
119
120
120
return res (
121
- ctx . set ( `Content-Type` , `image/svg+xml ` ) ,
121
+ ctx . set ( `Content-Type` , `image/jpg ` ) ,
122
122
ctx . set ( `Content-Length` , contentLength ) ,
123
123
ctx . status ( 200 ) ,
124
124
ctx . body ( content )
125
125
)
126
126
} ) ,
127
+ rest . get ( `http://external.com/dog-304.jpg` , async ( req , res , ctx ) => {
128
+ const { content, contentLength } = await getFileContent (
129
+ path . join ( __dirname , `./fixtures/dog-thumbnail.jpg` ) ,
130
+ req
131
+ )
132
+
133
+ // console.log(req.headers)
134
+
135
+ return res (
136
+ ctx . set ( `Content-Type` , `image/jpg` ) ,
137
+ ctx . set ( `Content-Length` , contentLength ) ,
138
+ ctx . set ( `etag` , `abcd` ) ,
139
+ ctx . status ( req . headers . get ( `if-none-match` ) === `abcd` ? 304 : 200 ) ,
140
+ ctx . body ( content )
141
+ )
142
+ } ) ,
127
143
rest . get ( `http://external.com/404.jpg` , async ( req , res , ctx ) => {
128
144
const content = `Page not found`
129
145
@@ -249,7 +265,7 @@ describe(`fetch-remote-file`, () => {
249
265
jest . useRealTimers ( )
250
266
} )
251
267
252
- it ( `downloads and create a file` , async ( ) => {
268
+ it ( `downloads and create a svg file` , async ( ) => {
253
269
const filePath = await fetchRemoteFile ( {
254
270
url : `http://external.com/logo.svg` ,
255
271
cache,
@@ -272,7 +288,7 @@ describe(`fetch-remote-file`, () => {
272
288
expect ( gotStream ) . toBeCalledTimes ( 1 )
273
289
} )
274
290
275
- it ( `downloads and create a file` , async ( ) => {
291
+ it ( `downloads and create a jpg file` , async ( ) => {
276
292
const filePath = await fetchRemoteFile ( {
277
293
url : `http://external.com/dog.jpg` ,
278
294
cache,
@@ -412,6 +428,35 @@ describe(`fetch-remote-file`, () => {
412
428
expect ( fsMove ) . toBeCalledTimes ( 2 )
413
429
} )
414
430
431
+ it ( `handles 304 responses correctly in different builds` , async ( ) => {
432
+ const cacheInternals = new Map ( )
433
+ const workerCache = {
434
+ get ( key ) {
435
+ return Promise . resolve ( cacheInternals . get ( key ) )
436
+ } ,
437
+ set ( key , value ) {
438
+ return Promise . resolve ( cacheInternals . set ( key , value ) )
439
+ } ,
440
+ directory : cache . directory ,
441
+ }
442
+
443
+ global . __GATSBY = { buildId : `1` }
444
+ const filePath = await fetchRemoteFile ( {
445
+ url : `http://external.com/dog-304.jpg` ,
446
+ cache : workerCache ,
447
+ } )
448
+
449
+ global . __GATSBY = { buildId : `2` }
450
+ const filePathCached = await fetchRemoteFile ( {
451
+ url : `http://external.com/dog-304.jpg` ,
452
+ cache : workerCache ,
453
+ } )
454
+
455
+ expect ( filePathCached ) . toBe ( filePath )
456
+ expect ( fsMove ) . toBeCalledTimes ( 1 )
457
+ expect ( gotStream ) . toBeCalledTimes ( 2 )
458
+ } )
459
+
415
460
it ( `doesn't keep lock when file download failed` , async ( ) => {
416
461
const cacheInternals = new Map ( )
417
462
const workerCache = {
@@ -562,6 +607,38 @@ describe(`fetch-remote-file`, () => {
562
607
expect ( fsMove ) . toBeCalledTimes ( 1 )
563
608
} )
564
609
610
+ it ( `handles 304 responses correctly in different builds and workers` , async ( ) => {
611
+ const cacheInternals = new Map ( )
612
+ const workerCache = {
613
+ get ( key ) {
614
+ return Promise . resolve ( cacheInternals . get ( key ) )
615
+ } ,
616
+ set ( key , value ) {
617
+ return Promise . resolve ( cacheInternals . set ( key , value ) )
618
+ } ,
619
+ directory : cache . directory ,
620
+ }
621
+
622
+ const fetchRemoteFileInstanceOne = getFetchInWorkerContext ( `1` )
623
+ const fetchRemoteFileInstanceTwo = getFetchInWorkerContext ( `2` )
624
+
625
+ global . __GATSBY = { buildId : `1` }
626
+ const filePath = await fetchRemoteFileInstanceOne ( {
627
+ url : `http://external.com/dog-304.jpg` ,
628
+ cache : workerCache ,
629
+ } )
630
+
631
+ global . __GATSBY = { buildId : `2` }
632
+ const filePathCached = await fetchRemoteFileInstanceTwo ( {
633
+ url : `http://external.com/dog-304.jpg` ,
634
+ cache : workerCache ,
635
+ } )
636
+
637
+ expect ( filePathCached ) . toBe ( filePath )
638
+ expect ( fsMove ) . toBeCalledTimes ( 1 )
639
+ expect ( gotStream ) . toBeCalledTimes ( 2 )
640
+ } )
641
+
565
642
it ( `fails when 404 is triggered` , async ( ) => {
566
643
await expect (
567
644
fetchRemoteFile ( {
0 commit comments