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

purge-fastly-by-url.js 2.9 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const program = require('commander')
  5. const { execSync } = require('child_process')
  6. const languageCodes = Object.keys(require('../lib/languages'))
  7. const { getPathWithoutLanguage } = require('../lib/path-utils')
  8. // [start-readme]
  9. //
  10. // Run this script to manually purge the Fastly cache
  11. // for all language variants of a single URL or for a batch of URLs in a file. This script does
  12. // not require authentication.
  13. //
  14. // [end-readme]
  15. const requiredUrlPrefix = 'https://docs.github.com'
  16. const purgeCommand = 'curl -s -X PURGE -H "Fastly-Soft-Purge:1"'
  17. program
  18. .description('Purge the Fastly cache for a single URL or a batch of URLs in a file, plus all language variants of the given URL(s).')
  19. .option('-s, --single <URL>', `provide a single ${requiredUrlPrefix} URL`)
  20. .option('-b, --batch <FILE>', `provide a path to a file containing a list of ${requiredUrlPrefix} URLs`)
  21. .option('-d, --dry-run', 'print URLs to be purged without actually purging')
  22. .parse(process.argv)
  23. const singleUrl = program.opts().single
  24. const batchFile = program.opts().batch
  25. const dryRun = program.opts().dryRun
  26. // verify CLI options
  27. if (!singleUrl && !batchFile) {
  28. console.error('error: you must specify --single <URL> or --batch <FILE>.\n')
  29. process.exit(1)
  30. }
  31. if (singleUrl && !singleUrl.startsWith(requiredUrlPrefix)) {
  32. console.error(`error: cannot purge ${singleUrl} because URLs must start with ${requiredUrlPrefix}.\n`)
  33. process.exit(1)
  34. }
  35. if (batchFile && !fs.existsSync(batchFile)) {
  36. console.error('error: cannot find batch file.\n')
  37. process.exit(1)
  38. }
  39. // do the purge
  40. if (singleUrl) {
  41. purge(singleUrl)
  42. }
  43. if (batchFile) {
  44. fs.readFileSync(batchFile, 'utf8')
  45. .split('\n')
  46. .filter(line => line !== '')
  47. .forEach(url => {
  48. if (!url.startsWith(requiredUrlPrefix)) {
  49. console.error(`error: cannot purge ${url} because URLs must start with ${requiredUrlPrefix}.\n`)
  50. process.exit(1)
  51. }
  52. purge(url)
  53. })
  54. }
  55. function purge (url) {
  56. getLanguageVariants(url).forEach(localizedUrl => {
  57. if (dryRun) {
  58. console.log(`This is a dry run! Will purge cache for ${localizedUrl}`)
  59. return
  60. }
  61. console.log(`Purging cache for ${localizedUrl}`)
  62. const result = execSync(`${purgeCommand} ${localizedUrl}`).toString()
  63. logStatus(result)
  64. // purge twice to ensure referenced content on the page is updated too
  65. const secondResult = execSync(`${purgeCommand} ${localizedUrl}`).toString()
  66. logStatus(secondResult)
  67. })
  68. }
  69. function getLanguageVariants (url) {
  70. // for https://docs.github.com/en/foo, get https://docs.github.com/foo
  71. const languagelessUrl = getPathWithoutLanguage(url.replace(requiredUrlPrefix, ''))
  72. // then derive localized urls
  73. return languageCodes.map(lc => path.join(requiredUrlPrefix, lc, languagelessUrl))
  74. }
  75. function logStatus (result) {
  76. // only log status if it's not ok
  77. if (JSON.parse(result).status === 'ok') return
  78. console.log(result)
  79. }
Tip!

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

Comments

Loading...