44 */
55
66class NetlifyServerPushPlugin {
7+ constructor ( { redirects } ) {
8+ this . redirects = [ ] ;
9+ if ( redirects !== undefined ) {
10+ if ( Array . isArray ( redirects ) ) {
11+ this . redirects = redirects ;
12+ } else {
13+ throw new TypeError (
14+ `redirects should be an array, but was of type '${ typeof redirects } '` ,
15+ ) ;
16+ }
17+ }
18+ }
19+
720 apply ( compiler ) {
821 compiler . plugin ( 'emit' , ( compilation , callback ) => {
922 const routes = [ ] ;
@@ -26,15 +39,17 @@ class NetlifyServerPushPlugin {
2639 let headers =
2740 '/*\n\tCache-Control: public, max-age=3600, no-cache\n\tAccess-Control-Max-Age: 600\n/sw.js\n\tCache-Control: private, no-cache\n/*.chunk.*.js\n\tCache-Control: public, max-age=31536000' ;
2841
29- const redirects = `/* /index.html 200` ;
42+ const redirects = `${ this . redirects . join ( '\n' ) } \n /* /index.html 200` ;
3043
3144 routes . forEach ( filename => {
3245 const path = filename
3346 . replace ( / r o u t e - / , '/' )
3447 . replace ( / \. c h u n k ( \. \w + ) ? \. j s $ / , '' )
3548 . replace ( / \/ h o m e / , '/' ) ;
3649 const routeJs = `Link: </${ filename } >; rel=preload; as=script` ;
37- headers = `${ headers } \n${ path } \n\t${ mainCss } \n\t${ mainJs } \n\t${ routeJs } ` ;
50+ headers = `${ headers } \n${ path } \n\t${ mainCss } \n\t${ mainJs } \n\t${
51+ routeJs
52+ } `;
3853 } ) ;
3954
4055 compilation . assets . _headers = {
@@ -60,11 +75,13 @@ class NetlifyServerPushPlugin {
6075 }
6176}
6277
63- module . exports = function ( config ) {
78+ module . exports = function ( config , options = { } ) {
6479 if ( ! config || ! config . plugins ) {
65- throw new Error ( 'You need to pass the webpack config to preact-cli-plugin-netlify!' ) ;
80+ throw new Error (
81+ 'You need to pass the webpack config to preact-cli-plugin-netlify!' ,
82+ ) ;
6683 }
67- config . plugins . push ( new NetlifyServerPushPlugin ( ) ) ;
84+ config . plugins . push ( new NetlifyServerPushPlugin ( options ) ) ;
6885 const plugins = config . plugins ;
6986 for ( let pluginIndex = 0 ; pluginIndex < plugins . length ; pluginIndex ++ ) {
7087 const plugin = plugins [ pluginIndex ] ;
0 commit comments