Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the existence of the gzipped path explicitly #704

Merged
merged 4 commits into from
Jan 9, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsistent*

// 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. Let's serve the uncompressed file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specially in dev mode.
We can remove this check when we get #657

return serveStatic(req, res, path)
}

throw ex
}

res.setHeader('Content-Encoding', 'gzip')
return serveStatic(req, res, gzipPath)
}

export function serveStatic (req, res, path) {
Expand Down