Skip to content

Commit b350086

Browse files
committed
fix(caniuse): handle wf- prefixed web-features IDs
When a caniuse feature lookup fails with a wf- prefix, strip the prefix and retry. Returns a clear 404 if no match is found. Closes #467
1 parent 39ce491 commit b350086

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

routes/caniuse/feature.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Request, Response } from "express";
2-
import { seconds } from "../../utils/misc.js";
32
import { BROWSERS, DEFAULT_BROWSERS } from "./lib/constants.js";
43
import { Data, getData } from "./lib/index.js";
54

@@ -11,16 +10,18 @@ export default async function route(req: IRequest, res: Response) {
1110
const { feature } = req.params;
1211
const browsers = normalizeBrowsers(req.query.browsers);
1312

14-
1513
try {
1614
const data = await getData(feature);
1715
if (data === null) {
18-
res.sendStatus(404);
16+
const hint = feature.startsWith("wf-")
17+
? ` The "wf-" prefix indicates a web-features ID. No matching caniuse data was found for "${feature}" or "${feature.slice(3)}".`
18+
: "";
19+
res.status(404).json({ error: `Feature "${feature}" not found.${hint}` });
1920
return;
2021
}
2122
const result = [];
2223
for (const browser of browsers) {
23-
result.push({ browser, ...getBrowserData(data?.all[browser]) });
24+
result.push({ browser, ...getBrowserData(data.all[browser]) });
2425
}
2526
res.json({ result });
2627
} catch (error) {

routes/caniuse/lib/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ function sanitizeBrowsersList(browsers?: string | string[]) {
106106
}
107107

108108
export async function getData(feature: string) {
109+
// Try the feature as-is first, then fall back to stripping the "wf-" prefix.
110+
// E.g., "wf-css-grid" falls back to looking up "css-grid" in caniuse data.
111+
const data = await readFeatureFile(feature);
112+
if (data) return data;
113+
if (feature.startsWith("wf-")) return readFeatureFile(feature.slice(3));
114+
return null;
115+
}
116+
117+
async function readFeatureFile(feature: string) {
109118
if (cache.has(feature)) {
110119
return cache.get(feature) as Data;
111120
}
@@ -120,7 +129,9 @@ export async function getData(feature: string) {
120129
cache.set(feature, data);
121130
return data;
122131
} catch (error) {
123-
console.error(error);
132+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
133+
console.error(error);
134+
}
124135
return null;
125136
}
126137
}

0 commit comments

Comments
 (0)