Skip to content

Commit d7eac7f

Browse files
arunodarauchg
authored andcommitted
Check the existence of the gzipped path explicitly (#704)
* Check the existance of the gzipped path explicitely. * Fix a typo in the comments. * Fix a typo.
1 parent 88e4adf commit d7eac7f

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

server/render.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { join } from 'path'
22
import { createElement } from 'react'
33
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
44
import send from 'send'
5+
import fs from 'mz/fs'
56
import accepts from 'accepts'
67
import mime from 'mime-types'
78
import 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

166177
export function serveStatic (req, res, path) {

0 commit comments

Comments
 (0)