Skip to content

Commit

Permalink
Enable compression on dev server responses (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
stramel committed Jun 3, 2020
1 parent 3ffb036 commit e121749
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -59,6 +59,7 @@
"cachedir": "^2.3.0",
"chalk": "^4.0.0",
"chokidar": "^3.4.0",
"compressible": "^2.0.18",
"cosmiconfig": "^6.0.0",
"css-modules-loader-core": "^1.1.0",
"deepmerge": "^4.2.2",
Expand Down Expand Up @@ -100,6 +101,7 @@
"@pika/plugin-ts-standard-pkg": "^0.9.1",
"@types/babel__traverse": "^7.0.7",
"@types/cacache": "^12.0.1",
"@types/compressible": "^2.0.0",
"@types/es-module-lexer": "^0.3.0",
"@types/http-proxy": "^1.17.4",
"@types/mkdirp": "^1.0.0",
Expand Down
38 changes: 34 additions & 4 deletions src/commands/dev.ts
Expand Up @@ -27,6 +27,8 @@
import cacache from 'cacache';
import chalk from 'chalk';
import chokidar from 'chokidar';
import isCompressible from 'compressible'
import detectPort from 'detect-port';
import etag from 'etag';
import {EventEmitter} from 'events';
import execa from 'execa';
Expand All @@ -37,9 +39,10 @@ import mime from 'mime-types';
import npmRunPath from 'npm-run-path';
import os from 'os';
import path from 'path';
import url from 'url';
import onProcessExit from 'signal-exit';
import detectPort from 'detect-port';
import stream from 'stream'
import url from 'url';
import zlib from 'zlib';
import {BuildScript, SnowpackPluginBuildResult} from '../config';
import {EsmHmrEngine} from '../hmr-server-engine';
import {scanCodeImportsExports, transformEsmImports} from '../rewrite-imports';
Expand Down Expand Up @@ -101,19 +104,46 @@ const sendFile = (
ext = '.html',
) => {
const ETag = etag(body);
const headers = {
const headers: Record<string, string> = {
'Content-Type': mime.contentType(ext) || 'application/octet-stream',
'Access-Control-Allow-Origin': '*',
ETag,
Vary: 'Accept-Encoding'
};

if (req.headers['if-none-match'] === ETag) {
res.writeHead(304, headers);
res.end()
return
}

let acceptEncoding = (req.headers['accept-encoding'] as string) || ''
if (req.headers["cache-control"]?.includes('no-transform') || ['HEAD', 'OPTIONS'].includes(req.method!) || !isCompressible(mime.contentType(ext))) {
acceptEncoding = ''
}

function onError(err) {
if (err) {
res.end()
console.error(
chalk.red(
` ✘ An error occurred while compressing ${chalk.bold(req.url)}`,
),
err
);
}
}

const raw = stream.Readable.from([body])
if (/\bgzip\b/.test(acceptEncoding)) {
headers['Content-Encoding'] = 'gzip'
res.writeHead(200, headers)
stream.pipeline(raw, zlib.createGzip(), res, onError)
} else {
res.writeHead(200, headers);
res.write(body, getEncodingType(ext));
res.end()
}
res.end();
};

const sendError = (res, status) => {
Expand Down

0 comments on commit e121749

Please sign in to comment.