Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to exclude frontend reload script injection via configuration #1457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,13 @@ Resolves to:
{
"enable": true,
"port": 9856,
"httpsPort": 9857
"httpsPort": 9857,
"excludeRoutes": []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HTML minifier has a similar param called exceptionRoutes, either this new one, or the old one should be renamed for consistency.

}
```

- `excludeRoutes`: Reload auto script injection happens on all HTML routes by default unless specified in `excludeRoutes`. An array of valid glob patterns is accepted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `excludeRoutes`: Reload auto script injection happens on all HTML routes by default unless specified in `excludeRoutes`. An array of valid glob patterns is accepted
- `excludeRoutes`: List of routes to exclude from Reload automatically injecting its script onto.
- Default: *[Array]* with no items in it. This means Reload will inject its script on all routes by default.


- `htmlValidator`: Parameters to send to [express-html-validator](https://github.com/rooseveltframework/express-html-validator#configuration). This feature is only available in development mode.

- `enable`: *[Boolean]* Enables or disables the built-in HTML validator.
Expand Down
3 changes: 2 additions & 1 deletion lib/defaults/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"enable": true,
"port": 9856,
"httpsPort": 9857,
"verbose": false
"verbose": false,
"excludeRoutes": []
},
"cores": 1,
"shutdownTimeout": 30000,
Expand Down
10 changes: 9 additions & 1 deletion lib/injectReload.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Injects <script> tag containing "reload.js"

const minimatch = require('minimatch')

module.exports = function (app) {
const reloadParams = app.get('params').frontendReload

// check that Reload is enabled and app is running in development mode
if (app.get('env') === 'development' && reloadParams.enable) {
app.use(require('tamper')((req, res) => {
if (res.getHeader('Content-Type') && res.getHeader('Content-Type').includes('text/html')) {
// Check to make sure requested URL isn't the exclude routes configured and if type HTML append the reload frontend script
if (!matchUrlPatterns(req.url, reloadParams.excludeRoutes) && res.getHeader('Content-Type') && res.getHeader('Content-Type').includes('text/html')) {
return (body) => {
const pos = body.lastIndexOf('</body>')
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)
Expand All @@ -19,4 +23,8 @@ module.exports = function (app) {
function getProtocol (protocol) {
return protocol.charAt(0).toUpperCase() + protocol.slice(1)
}

function matchUrlPatterns (url, patterns) {
return patterns.some(pattern => minimatch(url, pattern))
}
}
3 changes: 3 additions & 0 deletions lib/sourceParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ module.exports = (params, app, appSchema) => {
},
verbose: {
default: defaults.frontendReload.verbose
},
excludeRoutes: {
default: defaults.frontendReload.excludeRoutes
}
},
cores: {
Expand Down
Loading
Loading