Skip to content

Commit a37d991

Browse files
authored
Pass feature checks to internal link checker (#60769)
1 parent db8473b commit a37d991

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/links/scripts/check-links-internal.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { uploadArtifact } from '@/links/scripts/upload-artifact'
4141
import { createReportIssue, linkReports } from '@/workflows/issue-report'
4242
import github from '@/workflows/github'
4343
import excludedLinks from '@/links/lib/excluded-links'
44+
import { getFeaturesByVersion } from '@/versions/middleware/features'
4445
import type { Page, Permalink, Context } from '@/types'
4546
import * as coreLib from '@actions/core'
4647

@@ -171,25 +172,30 @@ async function checkVersion(
171172

172173
console.log(` Checking ${relevantPages.length} pages for ${version}/${language}`)
173174

175+
// Build a base context once per version — feature flags and version info are the same for all pages
176+
const baseContext: Context = {
177+
currentVersion: version,
178+
currentLanguage: language,
179+
currentVersionObj: versionObj,
180+
[versionObj.shortName]: true,
181+
pages: pageMap,
182+
redirects,
183+
...getFeaturesByVersion(version),
184+
} as Context
185+
174186
for (const page of relevantPages) {
175187
// Find the permalink for this version
176188
const permalink = page.permalinks?.find((p) => p.pageVersion === version)
177189
if (!permalink) continue
178190

179191
totalPagesChecked++
180192

181-
// Create context for rendering
182-
const context: Context = {
183-
currentVersion: version,
184-
currentLanguage: language,
185-
currentVersionObj: versionObj,
186-
page,
187-
pages: pageMap,
188-
redirects,
189-
} as Context
193+
// Mutate the page property in place — safe because the loop is sequential (each iteration
194+
// awaits before the next begins), so there is no concurrent access to baseContext.
195+
baseContext.page = page
190196

191197
// Get links from rendered page
192-
const links = await getLinksFromRenderedPage(page, permalink, context)
198+
const links = await getLinksFromRenderedPage(page, permalink, baseContext)
193199
totalLinksChecked += links.length
194200

195201
// Check each link
@@ -233,7 +239,7 @@ async function checkVersion(
233239

234240
// Check anchors if enabled
235241
if (options.checkAnchors) {
236-
const anchorFlaws = await checkAnchorsOnPage(page, permalink, context)
242+
const anchorFlaws = await checkAnchorsOnPage(page, permalink, baseContext)
237243
brokenLinks.push(...anchorFlaws)
238244
}
239245

src/versions/middleware/features.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type FeatureVersions = {
2626

2727
let allFeatures: Record<string, FeatureVersions>
2828

29-
const cache = new Map()
30-
function getFeaturesByVersion(currentVersion: string): Record<string, boolean> {
29+
const cache = new Map<string, Record<string, boolean>>()
30+
export function getFeaturesByVersion(currentVersion: string): Record<string, boolean> {
3131
if (!cache.has(currentVersion)) {
3232
if (!allFeatures) {
3333
// As of Oct 2022, the `data/features/**` reading is *not* JIT.
@@ -55,5 +55,5 @@ function getFeaturesByVersion(currentVersion: string): Record<string, boolean> {
5555
cache.set(currentVersion, featureFlags)
5656
}
5757

58-
return cache.get(currentVersion)
58+
return cache.get(currentVersion) as Record<string, boolean>
5959
}

0 commit comments

Comments
 (0)