Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

record-redirect.js 870 B

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  1. const { v4: uuidv4 } = require('uuid')
  2. module.exports = function recordRedirects (req, res, next) {
  3. if (!req.hydro.maySend()) return next()
  4. res.on('finish', async function recordRedirect () {
  5. // We definitely don't want 304
  6. if (![301, 302, 303, 307, 308].includes(res.statusCode)) return
  7. const schemaName = req.hydro.schemas.redirect
  8. const redirectEvent = {
  9. context: {
  10. user: req.cookies['_docs-events'] || uuidv4(),
  11. event_id: uuidv4(),
  12. version: '1.0.0',
  13. created: new Date().toISOString(),
  14. path: req.path,
  15. referrer: req.get('referer')
  16. },
  17. redirect_from: req.originalUrl,
  18. redirect_to: res.get('location')
  19. }
  20. const hydroRes = await req.hydro.publish(schemaName, redirectEvent)
  21. if (!hydroRes.ok) console.log('Failed to record redirect to Hydro')
  22. })
  23. return next()
  24. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...