Skip to content

Commit

Permalink
feat: serve all pre-compressed file types
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Oct 5, 2019
1 parent c932a08 commit 79b95ce
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ server:
compress: false
header: true
serveStatic:
preCompressed:
enable: false
extensions: ['html', 'js', 'css']
preCompressed: false
```
- **port**: Server port
Expand All @@ -48,9 +46,7 @@ server:
- **compress**: Enable GZIP compression
- **header**: Add `X-Powered-By: Hexo` header
- **serveStatic**: Extra options passed to [serve-static](https://github.com/expressjs/serve-static#options)
- **preCompressed**: Serve pre-compressed assets. Run `hexo generate` before enabling it.
- **enable**: Enable/disable preCompressed. Defaults to `false`.
- **extensions**: Array of pre-compressed file types to serve. Defaults to `['html', 'js', 'css']`.
- **preCompressed**: Serve pre-compressed assets

## License

Expand Down
5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ hexo.config.server = Object.assign({
ip: undefined,
compress: false,
header: true,
preCompressed: {
enable: false,
extensions: ['html', 'js', 'css']
}
preCompressed: true
}, hexo.config.server);

hexo.extend.console.register('server', 'Start the server.', {
Expand Down
20 changes: 8 additions & 12 deletions lib/middlewares/pre_compressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@ const { getType } = require('mime');
module.exports = function(app) {
const { config, route } = this;
const { root } = config;
const options = config.server;
const extRegex = new RegExp('\\.(' + options.preCompressed.extensions.join('|') + ')$');

if (!options.preCompressed.enable) return;
if (!config.server.preCompressed) return;

app.use(root, (req, res, next) => {
const routeList = route.list();
const { headers, method, url } = req;
const acceptEncoding = headers['accept-encoding'] || '';
const contentType = getType(url.endsWith('/') ? url.concat('index.html') : url);
const reqUrl = url.endsWith('/') ? url.concat('index.html') : url;
const contentType = getType(reqUrl);
const vary = res.getHeader('Vary');

if ((!url.endsWith('/') && !extRegex.test(url))
|| (method !== 'GET' && method !== 'HEAD')) {
return next();
}
if (method !== 'GET' && method !== 'HEAD') return next();

res.setHeader('Content-Type', contentType + '; charset=utf-8');

if (acceptEncoding.includes('br') && (routeList[url.slice(1) + '.br'] || url.endsWith('/'))) {
req.url = (url.endsWith('/') ? url.concat('index.html') : url) + '.br';
if (acceptEncoding.includes('br') && (routeList.includes(url.slice(1) + '.br') || url.endsWith('/'))) {
req.url = reqUrl + '.br';
res.setHeader('Content-Encoding', 'br');
} else if (acceptEncoding.includes('gzip') && (routeList[url.slice(1) + '.gz'] || url.endsWith('/'))) {
req.url = (url.endsWith('/') ? url.concat('index.html') : url) + '.gz';
} else if (acceptEncoding.includes('gzip') && (routeList.includes(url.slice(1) + '.gz') || url.endsWith('/'))) {
req.url = reqUrl + '.gz';
res.setHeader('Content-Encoding', 'gzip');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/middlewares/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function(app) {
const { config, route } = this;
const { args = {} } = this.env;
const { root, server } = config;
const preCompressed = server.preCompressed ? server.preCompressed.enable : false;
const preCompressed = server.preCompressed ? server.preCompressed : false;

if (args.s || args.static) return;

Expand Down

0 comments on commit 79b95ce

Please sign in to comment.