@@ -49,7 +49,7 @@ interface FetchHandlerArg {
4949const ONE_HOUR_IN_SECONDS = 3600
5050
5151function getCacheKey ( url : URL , headers : Headers , renderPreview : boolean , config : ConfigDb ) : string {
52- return `${ url } -${ headers . get ( 'accept' ) ?? '' } -preview-${ renderPreview } -indexes-${ config . supportDirectoryIndexes } -redirects-${ config . supportWebRedirects } `
52+ return `${ url } -${ headers . get ( 'accept' ) } -preview-${ renderPreview } -indexes-${ config . supportDirectoryIndexes } -redirects-${ config . supportWebRedirects } -match- ${ headers . get ( 'if-none-match' ) } `
5353}
5454
5555async function getResponseFromCacheOrFetch ( args : FetchHandlerArg ) : Promise < Response > {
@@ -295,41 +295,41 @@ async function fetchHandler ({ url, headers, renderPreview, event, logs, subdoma
295295 renderPreview = shouldRenderDirectory ( url , config , accept )
296296 }
297297
298- if ( ! response . ok ) {
298+ if ( response . status > 399 ) {
299299 return fetchErrorPageResponse ( resource , init , response , await response . text ( ) , providers , config , firstInstallTime , logs )
300300 }
301301
302- if ( renderPreview ) {
303- try {
304- return renderEntityPageResponse ( url , headers , response , await response . arrayBuffer ( ) )
305- } catch ( err : any ) {
306- log . error ( 'error while loading body to render - %e' , err )
307-
308- // if the response content involves loading more than one block and
309- // loading a subsequent block fails, the `.arrayBuffer()` promise will
310- // reject with an opaque 'TypeError: Failed to fetch' so show a 502 Bad
311- // Gateway with debugging information
312- return fetchErrorPageResponse ( resource , init , new Response ( '' , {
313- status : 502 ,
314- statusText : 'Bad Gateway' ,
315- headers : {
316- 'Content-Type' : 'application/json'
317- }
318- } ) , JSON . stringify ( errorToObject ( err ) , null , 2 ) , providers , config , firstInstallTime , logs )
319- }
320- }
321-
322- if ( url . searchParams . get ( 'download' ) === 'true' ) {
323- // override inline attachments if present
324- let contentDisposition = response . headers . get ( 'content-disposition' )
302+ if ( response . status > 199 && response . status < 300 ) {
303+ if ( renderPreview ) {
304+ try {
305+ return renderEntityPageResponse ( url , headers , response , await response . arrayBuffer ( ) )
306+ } catch ( err : any ) {
307+ log . error ( 'error while loading body to render - %e' , err )
308+
309+ // if the response content involves loading more than one block and
310+ // loading a subsequent block fails, the `.arrayBuffer()` promise will
311+ // reject with an opaque 'TypeError: Failed to fetch' so show a 502 Bad
312+ // Gateway with debugging information
313+ return fetchErrorPageResponse ( resource , init , new Response ( '' , {
314+ status : 502 ,
315+ statusText : 'Bad Gateway' ,
316+ headers : {
317+ 'Content-Type' : 'application/json'
318+ }
319+ } ) , JSON . stringify ( errorToObject ( err ) , null , 2 ) , providers , config , firstInstallTime , logs )
320+ }
321+ } else if ( url . searchParams . get ( 'download' ) === 'true' ) {
322+ // override inline attachments if present
323+ let contentDisposition = response . headers . get ( 'content-disposition' )
324+
325+ if ( contentDisposition == null || contentDisposition === '' ) {
326+ contentDisposition = 'attachment'
327+ } else if ( contentDisposition . startsWith ( 'inline' ) ) {
328+ contentDisposition = contentDisposition . replace ( 'inline' , 'attachment' )
329+ }
325330
326- if ( contentDisposition == null || contentDisposition === '' ) {
327- contentDisposition = 'attachment'
328- } else if ( contentDisposition . startsWith ( 'inline' ) ) {
329- contentDisposition = contentDisposition . replace ( 'inline' , 'attachment' )
331+ response . headers . set ( 'content-disposition' , contentDisposition )
330332 }
331-
332- response . headers . set ( 'content-disposition' , contentDisposition )
333333 }
334334
335335 // Create a completely new response object with the same body, status,
0 commit comments