Skip to content

Commit eb1d54f

Browse files
authored
Merge pull request #1 from lionralfs/patch-1
Optional custom rewrites
2 parents c2f23f2 + 18aaa7c commit eb1d54f

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ export default function (config) {
3030
}
3131
```
3232

33+
### Options
34+
35+
#### Custom redirects
36+
37+
In addition to the generated redirects, you may want to supply your
38+
own rewrite rules. In this case, you can pass array of custom redirects to the plugin, such as
39+
```js
40+
export default function(config) {
41+
netlifyPlugin(config, {
42+
redirects: [
43+
'/api/* https://api.example.com/:splat 200',
44+
'/custom/* https://custom.example.com/ 200'
45+
]
46+
});
47+
}
48+
```
49+
which generates the following `_redirects` file:
50+
```
51+
/api/* https://api.example.com/:splat 200
52+
/custom/* https://custom.example.com/ 200
53+
/* /index.html 200
54+
```
55+
3356
## Generated files
3457

3558
This plugin genererates `_headers` and `_redirects` files inside build folder

index.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
*/
55

66
class 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(/route-/, '/')
3447
.replace(/\.chunk(\.\w+)?\.js$/, '')
3548
.replace(/\/home/, '/');
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

Comments
 (0)