91
loading...
This website collects cookies to deliver better user experience
browserslist-useragent-regexp
package. I would also suggest to install detect-browser package as it helps us to very good information on what current version and type of browser that our application is running on, but again that's optional.npm i browserslist-useragent-regexp detect-browser
.browserlistrc
file placed at root folder of our project. Example:last 2 versions
not dead
not IE > 0
not IE_Mob > 0
package.json
file to generate the regex with help of browserslist-useragent-regexp
package which will then be used to conditionally hide / display content."scripts": {
"supportedBrowsers": "(echo module.exports = && browserslist-useragent-regexp --allowHigherVersions) > src/supportedBrowsers.js"
}
supportedBrowsers.js
within src
folder. then we can npm run supportedBrowsers
. This command will generate a RegEx which we can use with browser's userAgent
to display / hide content.module.exports =
/((CPU[ +]OS|iPhone[ +]OS|CPU[ +]iPhone|CPU IPhone OS)[ +]+(14[_.]5|14[_.](......)/
supportedBrowsers.js
file into our component and write the logic to hide / display dataimport * as supportedBrowsers from '../supportedBrowsers';
import { detect } from 'detect-browser';
...
export class AppComponent implements OnInit {
browserSupported = '';
title = 'Browser Support';
message = '';
ngOnInit(): void {
this.browserSupported = supportedBrowsers.test(navigator.userAgent) ? '' : 'not';
this.message = `Your current browser ${detect()?.name} version ${detect()?.version} is ${this.browserSupported} supported`;
}
}
<main>
<div class="app-main">
<h2>{{ title }}</h2>
<p>{{ message }}</p>
</div>
</main>
Note: You may get compiler warning while importing .js
files in .ts
files - to resolve this simply add "allowJs": true
within compilerOptions
of tsconfig.json file.