Skip to content

Commit 3e7bb77

Browse files
committed
Added the ability to exclude frontend reload script injection via glob patterns at the server configuration level.
* Added minimatch as a dependency * Added new tests to test reload excludeRoutes * Added default excludeRoutes as empty array
1 parent 6a189f2 commit 3e7bb77

File tree

8 files changed

+317
-68
lines changed

8 files changed

+317
-68
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,13 @@ Resolves to:
372372
{
373373
"enable": true,
374374
"port": 9856,
375-
"httpsPort": 9857
375+
"httpsPort": 9857,
376+
"excludeRoutes": []
376377
}
377378
```
378379

380+
- `excludeRoutes`: Reload auto script injection happens on all HTML routes by default unless specified in `excludeRoutes`. An array of valid glob patterns is accepted
381+
379382
- `htmlValidator`: Parameters to send to [express-html-validator](https://github.com/rooseveltframework/express-html-validator#configuration). This feature is only available in development mode.
380383

381384
- `enable`: *[Boolean]* Enables or disables the built-in HTML validator.

lib/defaults/config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"enable": true,
4343
"port": 9856,
4444
"httpsPort": 9857,
45-
"verbose": false
45+
"verbose": false,
46+
"excludeRoutes": []
4647
},
4748
"cores": 1,
4849
"shutdownTimeout": 30000,

lib/injectReload.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// Injects <script> tag containing "reload.js"
2+
3+
const minimatch = require('minimatch')
4+
25
module.exports = function (app) {
36
const reloadParams = app.get('params').frontendReload
47

58
// check that Reload is enabled and app is running in development mode
69
if (app.get('env') === 'development' && reloadParams.enable) {
710
app.use(require('tamper')((req, res) => {
8-
if (res.getHeader('Content-Type') && res.getHeader('Content-Type').includes('text/html')) {
11+
// Check to make sure requested URL isn't the exclude routes configured and if type HTML append the reload frontend script
12+
if (!matchUrlPatterns(req.url, reloadParams.excludeRoutes) && res.getHeader('Content-Type') && res.getHeader('Content-Type').includes('text/html')) {
913
return (body) => {
1014
const pos = body.lastIndexOf('</body>')
1115
body = body.substring(0, pos) + `<!-- Injected by Roosevelt for frontend reload functionality -->\n<script src='/reload${getProtocol(req.protocol)}/reload.js'></script>\n</body>` + body.substring(pos + 7)
@@ -19,4 +23,8 @@ module.exports = function (app) {
1923
function getProtocol (protocol) {
2024
return protocol.charAt(0).toUpperCase() + protocol.slice(1)
2125
}
26+
27+
function matchUrlPatterns (url, patterns) {
28+
return patterns.some(pattern => minimatch(url, pattern))
29+
}
2230
}

lib/sourceParams.js

+3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ module.exports = (params, app, appSchema) => {
195195
},
196196
verbose: {
197197
default: defaults.frontendReload.verbose
198+
},
199+
excludeRoutes: {
200+
default: defaults.frontendReload.excludeRoutes
198201
}
199202
},
200203
cores: {

0 commit comments

Comments
 (0)