Skip to content

Commit

Permalink
fix(pre_compressed): rename preCompressed to pre_compressed
Browse files Browse the repository at this point in the history
- fix compatibility with trailing .html
  • Loading branch information
curbengh committed Jul 29, 2020
1 parent 8db24bf commit 40fe06d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ server:
cache: false
header: true
serveStatic:
preCompressed: false
pre_compressed: false
```
- **port**: Server port
Expand All @@ -50,7 +50,7 @@ server:
- Suitable for production environment only.
- **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, requires a [minifier plugin](https://hexo.io/plugins/) that supports gzip or brotli
- **pre_compressed**: Serve pre-compressed assets, requires a [minifier plugin](https://hexo.io/plugins/) that supports gzip or brotli

## License

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ hexo.config.server = Object.assign({
ip: undefined,
compress: false,
header: true,
preCompressed: false
pre_compressed: false
}, hexo.config.server);

hexo.extend.console.register('server', 'Start the server.', {
Expand Down
47 changes: 34 additions & 13 deletions lib/middlewares/pre_compressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,48 @@ const { getType } = require('mime');
module.exports = function(app) {
const { config, route } = this;
const { root } = config;
const { pretty_urls } = config;
const { trailing_index, trailing_html } = pretty_urls ? pretty_urls : {};

if (!config.server.preCompressed) return;
if (!config.server.pre_compressed) return;

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

if (method !== 'GET' && method !== 'HEAD') return next();

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

if (acceptEncoding.includes('br') && (route.get(url.slice(1) + '.br') || url.endsWith('/'))) {
req.url = encodeURI(reqUrl + '.br');
res.setHeader('Content-Encoding', 'br');
} else if (acceptEncoding.includes('gzip') && (route.get(url.slice(1) + '.gz') || url.endsWith('/'))) {
req.url = encodeURI(reqUrl + '.gz');
res.setHeader('Content-Encoding', 'gzip');
const preFn = (acceptEncoding, url, req, res) => {
res.setHeader('Content-Type', getType(url) + '; charset=utf-8');

if (acceptEncoding.includes('br') && route.get(url + '.br')) {
req.url = encodeURI('/' + url + '.br');
res.setHeader('Content-Encoding', 'br');
} else if (acceptEncoding.includes('gzip') && route.get(url + '.gz')) {
req.url = encodeURI('/' + url + '.gz');
res.setHeader('Content-Encoding', 'gzip');
}
};

if (data) {
if ((trailing_html === false && !requestUrl.endsWith('/index.html') && requestUrl.endsWith('.html')) || (trailing_index === false && requestUrl.endsWith('/index.html'))) {
// location `foo/bar.html`; request `foo/bar.html`; redirect to `foo/bar`
// location `foo/index.html`; request `foo/index.html`; redirect to `foo/`
return next();
}
// location `foo/bar/index.html`; request `foo/bar/` or `foo/bar/index.html; proxy to the location
// also applies to non-html
preFn(acceptEncoding, url, req, res);
} else {
if (route.get(url + '.html')) {
// location `foo/bar.html`; request `foo/bar`; proxy to the `foo/bar.html.br`
preFn(acceptEncoding, url + '.html', req, res);
} else {
return next();
}
}

if (!vary) {
Expand Down
4 changes: 2 additions & 2 deletions 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 : false;
const preCompressed = server.pre_compressed ? server.pre_compressed : false;

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

Expand Down Expand Up @@ -64,7 +64,7 @@ module.exports = function(app) {
return;
}

if (!preCompressed || (!req.url.endsWith('gz') && !req.url.endsWith('br'))) {
if (!preCompressed || (!req.url.endsWith('.br') && !req.url.endsWith('.gz'))) {
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
}

Expand Down
18 changes: 9 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ describe('server', () => {
.expect('Content-Type', 'image/jpeg')
.expect(200)));

describe('options.preCompressed', () => {
beforeEach(() => { hexo.config.server.preCompressed = false; });
describe('options.pre_compressed', () => {
beforeEach(() => { hexo.config.server.pre_compressed = false; });

it('Serve brotli (br) if supported', async () => {
hexo.config.server.preCompressed = true;
hexo.config.server.pre_compressed = true;

await Promise.using(
prepareServer(),
Expand All @@ -129,7 +129,7 @@ describe('server', () => {
});

it('Serve gzip if br is not supported', async () => {
hexo.config.server.preCompressed = true;
hexo.config.server.pre_compressed = true;

return Promise.using(
prepareServer(),
Expand All @@ -142,7 +142,7 @@ describe('server', () => {
});

it('Disable', async () => {
hexo.config.server.preCompressed = false;
hexo.config.server.pre_compressed = false;

await Promise.using(
prepareServer(),
Expand All @@ -155,8 +155,8 @@ describe('server', () => {
);
});

it('route / to /index.html (br)', async () => {
hexo.config.server.preCompressed = true;
it('route / to /index.html.br', async () => {
hexo.config.server.pre_compressed = true;

await Promise.using(
prepareServer(),
Expand All @@ -167,8 +167,8 @@ describe('server', () => {
);
});

it('route / to /index.html (gzip)', async () => {
hexo.config.server.preCompressed = true;
it('route / to /index.html.gz', async () => {
hexo.config.server.pre_compressed = true;

await Promise.using(
prepareServer(),
Expand Down

0 comments on commit 40fe06d

Please sign in to comment.