@@ -5,30 +5,26 @@ const { getType } = require('mime');
55module . exports = function ( app ) {
66 const { config, route } = this ;
77 const { root } = config ;
8- const options = config . server ;
9- const extRegex = new RegExp ( '\\.(' + options . preCompressed . extensions . join ( '|' ) + ')$' ) ;
108
11- if ( ! options . preCompressed . enable ) return ;
9+ if ( ! config . server . preCompressed ) return ;
1210
1311 app . use ( root , ( req , res , next ) => {
1412 const routeList = route . list ( ) ;
1513 const { headers, method, url } = req ;
1614 const acceptEncoding = headers [ 'accept-encoding' ] || '' ;
17- const contentType = getType ( url . endsWith ( '/' ) ? url . concat ( 'index.html' ) : url ) ;
15+ const reqUrl = url . endsWith ( '/' ) ? url . concat ( 'index.html' ) : url ;
16+ const contentType = getType ( reqUrl ) ;
1817 const vary = res . getHeader ( 'Vary' ) ;
1918
20- if ( ( ! url . endsWith ( '/' ) && ! extRegex . test ( url ) )
21- || ( method !== 'GET' && method !== 'HEAD' ) ) {
22- return next ( ) ;
23- }
19+ if ( method !== 'GET' && method !== 'HEAD' ) return next ( ) ;
2420
2521 res . setHeader ( 'Content-Type' , contentType + '; charset=utf-8' ) ;
2622
27- if ( acceptEncoding . includes ( 'br' ) && ( routeList [ url . slice ( 1 ) + '.br' ] || url . endsWith ( '/' ) ) ) {
28- req . url = ( url . endsWith ( '/' ) ? url . concat ( 'index.html' ) : url ) + '.br' ;
23+ if ( acceptEncoding . includes ( 'br' ) && ( routeList . includes ( url . slice ( 1 ) + '.br' ) || url . endsWith ( '/' ) ) ) {
24+ req . url = reqUrl + '.br' ;
2925 res . setHeader ( 'Content-Encoding' , 'br' ) ;
30- } else if ( acceptEncoding . includes ( 'gzip' ) && ( routeList [ url . slice ( 1 ) + '.gz' ] || url . endsWith ( '/' ) ) ) {
31- req . url = ( url . endsWith ( '/' ) ? url . concat ( 'index.html' ) : url ) + '.gz' ;
26+ } else if ( acceptEncoding . includes ( 'gzip' ) && ( routeList . includes ( url . slice ( 1 ) + '.gz' ) || url . endsWith ( '/' ) ) ) {
27+ req . url = reqUrl + '.gz' ;
3228 res . setHeader ( 'Content-Encoding' , 'gzip' ) ;
3329 }
3430
0 commit comments