@@ -2,6 +2,7 @@ import { join } from 'path'
22import { createElement } from 'react'
33import { renderToString , renderToStaticMarkup } from 'react-dom/server'
44import send from 'send'
5+ import fs from 'mz/fs'
56import accepts from 'accepts'
67import mime from 'mime-types'
78import requireModule from './require'
@@ -148,19 +149,29 @@ export async function serveStaticWithGzip (req, res, path) {
148149 return serveStatic ( req , res , path )
149150 }
150151
152+ const gzipPath = `${ path } .gz`
153+
151154 try {
152- const gzipPath = `${ path } .gz`
153- const contentType = mime . lookup ( path ) || 'application/octet-stream'
154- res . setHeader ( 'Content-Type' , contentType )
155- res . setHeader ( 'Content-Encoding' , 'gzip' )
156- await serveStatic ( req , res , gzipPath )
155+ // We need to check the existance of the gzipPath.
156+ // Getting `ENOENT` error from the `serveStatic` is inconsistent and
157+ // didn't work on all the cases.
158+ //
159+ // And this won't give us a race condition because we know that
160+ // we don't add gzipped files at runtime.
161+ await fs . stat ( gzipPath )
157162 } catch ( ex ) {
158163 if ( ex . code === 'ENOENT' ) {
159- res . removeHeader ( 'Content-Encoding' )
164+ // Seems like there's no gzipped file. Let's serve the uncompressed file.
160165 return serveStatic ( req , res , path )
161166 }
167+
162168 throw ex
163169 }
170+
171+ const contentType = mime . lookup ( path ) || 'application/octet-stream'
172+ res . setHeader ( 'Content-Type' , contentType )
173+ res . setHeader ( 'Content-Encoding' , 'gzip' )
174+ return serveStatic ( req , res , gzipPath )
164175}
165176
166177export function serveStatic ( req , res , path ) {
0 commit comments