Skip to content

Commit 43ab10e

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 5916d39 commit 43ab10e

2 files changed

Lines changed: 20 additions & 6 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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
export { Data };
1616

1717
const DATA_DIR = env("DATA_DIR");
18+
const CANIUSE_DIR = path.join(DATA_DIR, "caniuse");
1819

1920
interface Options {
2021
feature: string;
@@ -106,11 +107,20 @@ function sanitizeBrowsersList(browsers?: string | string[]) {
106107
}
107108

108109
export async function getData(feature: string) {
110+
// Try the feature as-is first, then fall back to stripping the "wf-" prefix.
111+
// E.g., "wf-css-grid" falls back to looking up "css-grid" in caniuse data.
112+
const data = await readFeatureFile(feature);
113+
if (data) return data;
114+
if (feature.startsWith("wf-")) return readFeatureFile(feature.slice(3));
115+
return null;
116+
}
117+
118+
async function readFeatureFile(feature: string) {
109119
if (cache.has(feature)) {
110120
return cache.get(feature) as Data;
111121
}
112122
const file = path.format({
113-
dir: path.join(DATA_DIR, "caniuse"),
123+
dir: CANIUSE_DIR,
114124
name: `${feature}.json`,
115125
});
116126

@@ -120,7 +130,10 @@ export async function getData(feature: string) {
120130
cache.set(feature, data);
121131
return data;
122132
} catch (error) {
123-
console.error(error);
133+
const code = (error as NodeJS.ErrnoException).code;
134+
if (code !== "ENOENT") {
135+
console.error(error);
136+
}
124137
return null;
125138
}
126139
}

0 commit comments

Comments
 (0)