From 1acda34ab92b7987ae2aa12fed7aaa72ac49fe40 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Mon, 9 Jan 2017 06:42:23 +0530 Subject: [PATCH 1/3] Check the existance of the gzipped path explicitely. --- server/render.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/server/render.js b/server/render.js index c4c3b35760a7b..8e7ffc783c53a 100644 --- a/server/render.js +++ b/server/render.js @@ -2,6 +2,7 @@ import { join } from 'path' import { createElement } from 'react' import { renderToString, renderToStaticMarkup } from 'react-dom/server' import send from 'send' +import fs from 'mz/fs' import accepts from 'accepts' import requireModule from './require' import resolvePath from './resolve' @@ -147,17 +148,27 @@ export async function serveStaticWithGzip (req, res, path) { return serveStatic(req, res, path) } + const gzipPath = `${path}.gz` + try { - const gzipPath = `${path}.gz` - res.setHeader('Content-Encoding', 'gzip') - await serveStatic(req, res, gzipPath) + // We need to check the existance of the gzipPath. + // Getting `ENOENT` error from the `serveStatic` is inconsistance and + // didn't work on all the cases. + // + // And this won't give us a race condition because we know that + // we don't add gzipped files at runtime. + await fs.stat(gzipPath) } catch (ex) { if (ex.code === 'ENOENT') { - res.removeHeader('Content-Encoding') + // Seems like there's no gzipped file the system return serveStatic(req, res, path) } + throw ex } + + res.setHeader('Content-Encoding', 'gzip') + return serveStatic(req, res, gzipPath) } export function serveStatic (req, res, path) { From ec12c417457cc5672c1001ea42cb7700756bdeff Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Mon, 9 Jan 2017 06:45:43 +0530 Subject: [PATCH 2/3] Fix a typo in the comments. --- server/render.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/render.js b/server/render.js index 8e7ffc783c53a..b84d1a6eeaffb 100644 --- a/server/render.js +++ b/server/render.js @@ -160,7 +160,7 @@ export async function serveStaticWithGzip (req, res, path) { await fs.stat(gzipPath) } catch (ex) { if (ex.code === 'ENOENT') { - // Seems like there's no gzipped file the system + // Seems like there's no gzipped file. Let's serve the uncompressed file. return serveStatic(req, res, path) } From 25675ff9ea5a6ceb44b99e78e8e10978da498dd0 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Mon, 9 Jan 2017 08:21:31 +0530 Subject: [PATCH 3/3] Fix a typo. --- server/render.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/render.js b/server/render.js index a299330c9d675..6d26eaeec89f8 100644 --- a/server/render.js +++ b/server/render.js @@ -153,7 +153,7 @@ export async function serveStaticWithGzip (req, res, path) { try { // We need to check the existance of the gzipPath. - // Getting `ENOENT` error from the `serveStatic` is inconsistance and + // Getting `ENOENT` error from the `serveStatic` is inconsistent and // didn't work on all the cases. // // And this won't give us a race condition because we know that