Skip to content

Commit

Permalink
spot-content-flaws middleware (github#23451)
Browse files Browse the repository at this point in the history
* spot-content-flaws

* now with 100% more color

* use kleur instead
  • Loading branch information
peterbe authored Dec 15, 2021
1 parent 1c2219c commit 26f4a56
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
2 changes: 2 additions & 0 deletions middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import helpToDocs from './redirects/help-to-docs.js'
import languageCodeRedirects from './redirects/language-code-redirects.js'
import handleRedirects from './redirects/handle-redirects.js'
import findPage from './find-page.js'
import spotContentFlaws from './spot-content-flaws.js'
import blockRobots from './block-robots.js'
import archivedEnterpriseVersionsAssets from './archived-enterprise-versions-assets.js'
import events from './events.js'
Expand Down Expand Up @@ -173,6 +174,7 @@ export default function (app) {

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

// Check for a dropped connection before proceeding
Expand Down
32 changes: 32 additions & 0 deletions middleware/spot-content-flaws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This middleware, exclusively in 'development' tries to spot flaws in
// the content you're actively viewing.
// The hopeful assumption is that if you're actively viewing this
// page on localhost, you're actively working on its content.

import path from 'path'

import kleur from 'kleur'

export default async function spotContentFlaws(req, res, next) {
const { page } = req.context
if (process.env.NODE_ENV === 'development' && page) {
const trailingSlashRedirects = (page.redirect_from || []).filter(
(uri) => uri.endsWith('/') && uri.startsWith('/')
)
if (trailingSlashRedirects.length > 0) {
console.warn(
`The page ${kleur.bold(path.relative(process.cwd(), page.fullPath))} has ${
trailingSlashRedirects.length
} redirect_from entries that have a trailing slash\n ${kleur.yellow(
trailingSlashRedirects.join('\n ')
)}`
)
console.log(
"If you're actively working on this page, consider",
kleur.bold('deleting all trailing slashes in redirect_from.\n')
)
}
}

return next()
}
47 changes: 24 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"imurmurhash": "^0.1.4",
"js-cookie": "^3.0.1",
"js-yaml": "^4.1.0",
"kleur": "4.1.4",
"liquidjs": "^9.22.1",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
Expand Down

0 comments on commit 26f4a56

Please sign in to comment.