Skip to content

Commit 26f4a56

Browse files
author
Peter Bengtsson
authored
spot-content-flaws middleware (#23451)
* spot-content-flaws * now with 100% more color * use kleur instead
1 parent 1c2219c commit 26f4a56

4 files changed

Lines changed: 59 additions & 23 deletions

File tree

middleware/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import helpToDocs from './redirects/help-to-docs.js'
3434
import languageCodeRedirects from './redirects/language-code-redirects.js'
3535
import handleRedirects from './redirects/handle-redirects.js'
3636
import findPage from './find-page.js'
37+
import spotContentFlaws from './spot-content-flaws.js'
3738
import blockRobots from './block-robots.js'
3839
import archivedEnterpriseVersionsAssets from './archived-enterprise-versions-assets.js'
3940
import events from './events.js'
@@ -173,6 +174,7 @@ export default function (app) {
173174

174175
// *** Config and context for rendering ***
175176
app.use(asyncMiddleware(instrument(findPage, './find-page'))) // Must come before archived-enterprise-versions, breadcrumbs, featured-links, products, render-page
177+
app.use(asyncMiddleware(instrument(spotContentFlaws, './spot-content-flaws'))) // Must come after findPage
176178
app.use(instrument(blockRobots, './block-robots'))
177179

178180
// Check for a dropped connection before proceeding

middleware/spot-content-flaws.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This middleware, exclusively in 'development' tries to spot flaws in
2+
// the content you're actively viewing.
3+
// The hopeful assumption is that if you're actively viewing this
4+
// page on localhost, you're actively working on its content.
5+
6+
import path from 'path'
7+
8+
import kleur from 'kleur'
9+
10+
export default async function spotContentFlaws(req, res, next) {
11+
const { page } = req.context
12+
if (process.env.NODE_ENV === 'development' && page) {
13+
const trailingSlashRedirects = (page.redirect_from || []).filter(
14+
(uri) => uri.endsWith('/') && uri.startsWith('/')
15+
)
16+
if (trailingSlashRedirects.length > 0) {
17+
console.warn(
18+
`The page ${kleur.bold(path.relative(process.cwd(), page.fullPath))} has ${
19+
trailingSlashRedirects.length
20+
} redirect_from entries that have a trailing slash\n ${kleur.yellow(
21+
trailingSlashRedirects.join('\n ')
22+
)}`
23+
)
24+
console.log(
25+
"If you're actively working on this page, consider",
26+
kleur.bold('deleting all trailing slashes in redirect_from.\n')
27+
)
28+
}
29+
}
30+
31+
return next()
32+
}

package-lock.json

Lines changed: 24 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"imurmurhash": "^0.1.4",
5050
"js-cookie": "^3.0.1",
5151
"js-yaml": "^4.1.0",
52+
"kleur": "4.1.4",
5253
"liquidjs": "^9.22.1",
5354
"lodash": "^4.17.21",
5455
"lodash-es": "^4.17.21",

0 commit comments

Comments
 (0)