forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record redirect events (github#16181)
* Record redirect events * Update index.js * Update record-redirect.js * Only run when it can send to Hydro * Update record-redirect.js
- Loading branch information
Showing
3 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { v4: uuidv4 } = require('uuid') | ||
const { inRange } = require('lodash') | ||
|
||
module.exports = function (req, res, next) { | ||
if (!req.hydro.maySend()) return next() | ||
|
||
res.on('finish', async function recordRedirect () { | ||
if (!inRange(res.statusCode, 300, 400)) return | ||
const schemaName = req.hydro.schemas.redirect | ||
const redirectEvent = { | ||
context: { | ||
user: req.cookies['_docs-events'] || uuidv4(), | ||
event_id: uuidv4(), | ||
version: '1.0.0', | ||
created: new Date().toISOString(), | ||
path: req.path, | ||
referrer: req.get('referer') | ||
}, | ||
redirect_from: req.originalUrl, | ||
redirect_to: res.get('location') | ||
} | ||
const hydroRes = await req.hydro.publish(schemaName, redirectEvent) | ||
if (!hydroRes.ok) console.log('Failed to record redirect to Hydro') | ||
}) | ||
|
||
return next() | ||
} |