Commit d7eac7fa authored by Arunoda Susiripala's avatar Arunoda Susiripala Committed by Guillermo Rauch

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.
parent 88e4adfc
...@@ -2,6 +2,7 @@ import { join } from 'path' ...@@ -2,6 +2,7 @@ import { join } from 'path'
import { createElement } from 'react' import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server' import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import send from 'send' import send from 'send'
import fs from 'mz/fs'
import accepts from 'accepts' import accepts from 'accepts'
import mime from 'mime-types' import mime from 'mime-types'
import requireModule from './require' import requireModule from './require'
...@@ -148,19 +149,29 @@ export async function serveStaticWithGzip (req, res, path) { ...@@ -148,19 +149,29 @@ export async function serveStaticWithGzip (req, res, path) {
return serveStatic(req, res, path) return serveStatic(req, res, path)
} }
const gzipPath = `${path}.gz`
try { try {
const gzipPath = `${path}.gz` // We need to check the existance of the gzipPath.
const contentType = mime.lookup(path) || 'application/octet-stream' // Getting `ENOENT` error from the `serveStatic` is inconsistent and
res.setHeader('Content-Type', contentType) // didn't work on all the cases.
res.setHeader('Content-Encoding', 'gzip') //
await serveStatic(req, res, gzipPath) // 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) { } catch (ex) {
if (ex.code === 'ENOENT') { if (ex.code === 'ENOENT') {
res.removeHeader('Content-Encoding') // Seems like there's no gzipped file. Let's serve the uncompressed file.
return serveStatic(req, res, path) return serveStatic(req, res, path)
} }
throw ex throw ex
} }
const contentType = mime.lookup(path) || 'application/octet-stream'
res.setHeader('Content-Type', contentType)
res.setHeader('Content-Encoding', 'gzip')
return serveStatic(req, res, gzipPath)
} }
export function serveStatic (req, res, path) { export function serveStatic (req, res, path) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment