65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
export interface FeatureDetectionResult {
|
|
critical: boolean;
|
|
nice: boolean;
|
|
missing: string[];
|
|
}
|
|
|
|
export function featureDetect(): FeatureDetectionResult {
|
|
console.info("Running feature detection");
|
|
// Define critical features
|
|
const criticalFeatures = {
|
|
fetch: "fetch" in window,
|
|
localStorage: "localStorage" in window && window["localStorage"] !== null,
|
|
sessionStorage: "sessionStorage" in window && window["sessionStorage"] !== null,
|
|
promise: "Promise" in window,
|
|
};
|
|
|
|
// Define nice-to-have features
|
|
const niceToHaveFeatures = {
|
|
geolocation: "geolocation" in navigator,
|
|
serviceWorker: "serviceWorker" in navigator,
|
|
dialog: "HTMLDialogElement" in window,
|
|
popover: "showPopover" in HTMLElement.prototype, // Note: 'Popover' might need more specific checks based on implementation
|
|
};
|
|
|
|
// Initialize result object
|
|
const result = {
|
|
critical: true,
|
|
nice: true,
|
|
missing: [],
|
|
};
|
|
|
|
// Check critical features
|
|
for (const [feature, available] of Object.entries(criticalFeatures)) {
|
|
if (!available) {
|
|
result.critical = false;
|
|
result.missing.push(feature);
|
|
}
|
|
}
|
|
|
|
// Check nice-to-have features
|
|
for (const [feature, available] of Object.entries(niceToHaveFeatures)) {
|
|
if (!available) {
|
|
result.nice = false;
|
|
result.missing.push(feature);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/*// Example usage
|
|
const featureSupport = featureDetect();
|
|
console.log(featureSupport);
|
|
|
|
if (!featureSupport.critical) {
|
|
alert('Some critical features required for this application are not supported by your browser.');
|
|
// Handle lack of critical features appropriately
|
|
} else if (!featureSupport.nice) {
|
|
console.warn('Some nice-to-have features are not supported by your browser. The app will still function, but some features may be unavailable.');
|
|
// Optionally handle lack of nice-to-have features
|
|
} else {
|
|
// Proceed with the normal functionality of your app
|
|
}
|
|
*/
|