Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1409b4f

Browse files
vripochegregberge
authored andcommitted
feat: add smooth-plugin-headers (#32)
1 parent b48740d commit 1409b4f

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# smooth-plugin-headers
2+
3+
Automatically adds response headers.
4+
5+
## Usage
6+
7+
Edit `smooth.config.js`:
8+
9+
```js
10+
// smooth.config.js
11+
module.exports = {
12+
plugins: [
13+
{
14+
resolve: 'smooth-plugin-headers',
15+
options: {
16+
// Add any options here
17+
},
18+
}
19+
],
20+
}
21+
```
22+
23+
## Options
24+
25+
### headers
26+
27+
Set headers for all maching pages, ReactRouter format can be used (see [path-to-regexp](https://www.npmjs.com/package/path-to-regexp)).
28+
Headers entries are merged on differents rules but are overwritten on sames, below rules overwrite above ones.
29+
30+
```js
31+
// smooth.config.js
32+
module.exports = {
33+
plugins: [
34+
{
35+
resolve: 'smooth-plugin-headers',
36+
options: {
37+
headers: {
38+
'(.*)': ['Cache-Control: public, max-age=900'],
39+
'(*.ico)': ['Cache-Control: public, max-age=2592000'],
40+
'/graphql': ['Cache-Control: no-cache'],
41+
},
42+
},
43+
}
44+
],
45+
}
46+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "smooth-plugin-headers",
3+
"description": "Smooth plugin to add custom headers",
4+
"version": "0.1.1-alpha.56",
5+
"repository": "https://github.com/smooth-code/smooth.js/tree/master/packages/smooth-plugin-headers",
6+
"author": "Vivien Ripoche <[email protected]>",
7+
"publishConfig": {
8+
"access": "public"
9+
},
10+
"keywords": [
11+
"smooth",
12+
"smooth-plugin"
13+
],
14+
"engines": {
15+
"node": ">=8"
16+
},
17+
"main": "index.js",
18+
"license": "MIT",
19+
"scripts": {
20+
"prebuild": "shx rm -rf lib",
21+
"build": "babel --root-mode upward -d lib --ignore \"**/*.test.js\" --copy-files src",
22+
"prepublishOnly": "yarn run build"
23+
},
24+
"dependencies": {
25+
"path-to-regexp": "^3.0.0"
26+
},
27+
"peerDependencies": {
28+
"smooth": "^0.1.0"
29+
}
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./src/smooth-node')
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
import pathToRegexp from 'path-to-regexp'
3+
4+
export function parseHeaders(url = '', headers = {}) {
5+
return [
6+
...Object.entries(headers)
7+
.reduce((acc, [rule, values = []]) => {
8+
if (pathToRegexp(rule).test(url)) {
9+
values.forEach(header => {
10+
const [, key, value] = /^([^:]*)\s*:\s*(.*)$/.exec(header)
11+
acc.set(key, value)
12+
})
13+
}
14+
return acc
15+
}, new Map())
16+
.entries(),
17+
]
18+
}
19+
20+
export function onCreateServer({ app }, { headers = {} }) {
21+
app.use(({ url }, res, next) => {
22+
parseHeaders(url, headers).forEach(args => res.set(...args))
23+
next()
24+
})
25+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { parseHeaders } from './smooth-node'
2+
3+
describe('Smooth Header Plugin', () => {
4+
describe('parseHeaders', () => {
5+
it('should parse to empty array by default', () => {
6+
expect(parseHeaders()).toEqual([])
7+
})
8+
9+
it('should parse Cache-control on matching (.*)', () => {
10+
expect(
11+
parseHeaders('/page/home', { '(.*)': ['Cache-Control: no-cache'] }),
12+
).toEqual([['Cache-Control', 'no-cache']])
13+
})
14+
15+
it('should not parse Cache-control on non matching /page/(.*) url', () => {
16+
expect(
17+
parseHeaders('/home', { '/page/(.*)': ['Cache-Control: no-cache'] }),
18+
).toEqual([])
19+
})
20+
21+
it('should parse Cache-control only on matching /home url', () => {
22+
expect(
23+
parseHeaders('/home', {
24+
'/page/(.*)': ['Cache-Control: no-cache'],
25+
'/home': ['Cache-Control: public, max-age=31557600'],
26+
}),
27+
).toEqual([['Cache-Control', 'public, max-age=31557600']])
28+
})
29+
30+
it('should parse all headers rules that match with url', () => {
31+
expect(
32+
parseHeaders('/home', {
33+
'/(.*)': ['Cache-Control: no-cache'],
34+
'/home': ['Basic-Auth: user:passwd'],
35+
}),
36+
).toEqual([['Cache-Control', 'no-cache'], ['Basic-Auth', 'user:passwd']])
37+
})
38+
39+
it('should overwrite same rules', () => {
40+
expect(
41+
parseHeaders('/favicon.ico', {
42+
'/(.*)': ['Cache-Control: no-cache'],
43+
'/(.*).ico': ['Cache-Control: public, max-age=2592000'],
44+
}),
45+
).toEqual([['Cache-Control', 'public, max-age=2592000']])
46+
})
47+
})
48+
})

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8804,6 +8804,11 @@ path-to-regexp@^1.7.0:
88048804
dependencies:
88058805
isarray "0.0.1"
88068806

8807+
path-to-regexp@^3.0.0:
8808+
version "3.0.0"
8809+
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-3.0.0.tgz#c981a218f3df543fa28696be2f88e0c58d2e012a"
8810+
integrity sha512-ZOtfhPttCrqp2M1PBBH4X13XlvnfhIwD7yCLx+GoGoXRPQyxGOTdQMpIzPSPKXAJT/JQrdfFrgdJOyAzvgpQ9A==
8811+
88078812
path-type@^1.0.0:
88088813
version "1.1.0"
88098814
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"

0 commit comments

Comments
 (0)