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

context.js 3.3 KB

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  1. const languages = require('../lib/languages')
  2. const enterpriseServerReleases = require('../lib/enterprise-server-releases')
  3. const allVersions = require('../lib/all-versions')
  4. const { productMap } = require('../lib/all-products')
  5. const activeProducts = Object.values(productMap).filter(product => !product.wip && !product.hidden)
  6. const {
  7. getVersionStringFromPath,
  8. getProductStringFromPath,
  9. getCategoryStringFromPath,
  10. getPathWithoutLanguage
  11. } = require('../lib/path-utils')
  12. const productNames = require('../lib/product-names')
  13. const warmServer = require('../lib/warm-server')
  14. const featureFlags = Object.keys(require('../feature-flags'))
  15. const builtAssets = require('../lib/built-asset-urls')
  16. const searchVersions = require('../lib/search/versions')
  17. const nonEnterpriseDefaultVersion = require('../lib/non-enterprise-default-version')
  18. // Supply all route handlers with a baseline `req.context` object
  19. // Note that additional middleware in middleware/index.js adds to this context object
  20. module.exports = async function contextualize (req, res, next) {
  21. // Ensure that we load some data only once on first request
  22. const { site, redirects, siteTree, pages: pageMap } = await warmServer()
  23. req.context = {}
  24. // make feature flag environment variables accessible in layouts
  25. req.context.process = { env: {} }
  26. featureFlags.forEach(featureFlagName => {
  27. req.context.process.env[featureFlagName] = process.env[featureFlagName]
  28. })
  29. // define each context property explicitly for code-search friendliness
  30. // e.g. searches for "req.context.page" will include results from this file
  31. req.context.currentLanguage = req.language
  32. req.context.currentVersion = getVersionStringFromPath(req.path)
  33. req.context.currentProduct = getProductStringFromPath(req.path)
  34. req.context.currentCategory = getCategoryStringFromPath(req.path)
  35. req.context.productMap = productMap
  36. req.context.activeProducts = activeProducts
  37. req.context.allVersions = allVersions
  38. req.context.currentPathWithoutLanguage = getPathWithoutLanguage(req.path)
  39. req.context.currentPath = req.path
  40. req.context.query = req.query
  41. req.context.languages = languages
  42. req.context.productNames = productNames
  43. req.context.enterpriseServerReleases = enterpriseServerReleases
  44. req.context.enterpriseServerVersions = Object.keys(allVersions).filter(version => version.startsWith('enterprise-server@'))
  45. req.context.redirects = redirects
  46. req.context.site = site[req.language].site
  47. req.context.siteTree = siteTree
  48. req.context.pages = pageMap
  49. if (productMap[req.context.currentProduct]) {
  50. req.context.productCodeExamples = req.context.site.data.variables[`${productMap[req.context.currentProduct].id}_code_examples`]
  51. req.context.productCommunityExamples = req.context.site.data.variables[`${productMap[req.context.currentProduct].id}_community_examples`]
  52. }
  53. // JS + CSS asset paths
  54. req.context.builtAssets = builtAssets
  55. // Object exposing selected variables to client
  56. req.context.expose = JSON.stringify({
  57. // Languages and versions for search
  58. searchOptions: {
  59. languages: Object.keys(languages),
  60. versions: searchVersions,
  61. nonEnterpriseDefaultVersion
  62. },
  63. // `|| undefined` won't show at all for production
  64. airgap: Boolean(process.env.AIRGAP || req.cookies.AIRGAP) || undefined
  65. })
  66. if (process.env.AIRGAP || req.cookies.AIRGAP) req.context.AIRGAP = true
  67. return next()
  68. }
Tip!

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

Comments

Loading...