DEV Community

Cover image for User agent detection and the ua-parser-js license change
Megan Lee for LogRocket

Posted on • Originally published at blog.logrocket.com

User agent detection and the ua-parser-js license change

Written by Ikeh Akinyemi✏️

User agent detection plays an important role in helping developers optimize their websites and applications for various devices, browsers, and operating systems. By accurately identifying their users’ environments, developers can tailor their solutions to deliver the best user experience.

In this article, we’ll learn about user agent detection and explore the JavaScript library that has gained significant adoption among developers: ua-parser-js. ua-parser-js recently made headlines due to a change in its licensing model, and we’ll cover its switch from a permissive MIT license to a dual AGPLv3 + commercial license model, and how this affects individual and SaaS projects.

What is user agent detection?

User agent detection is the process of identifying the specific software and hardware components your users are using to access your website or application. The detection involves information about the user’s browser name and version, operating system, device type, and more.

By leveraging the user agent detection, the developer can make informed decisions about how to present and optimize their users’ content, ensuring accessibility, tailored experiences, cross-browser and hardware compatibility, and possibly enhanced performance across the wide range of different platforms used.

The ua-parser-js library and its recent changes

ua-parser-js is a lightweight JavaScript library that simplifies user agent detection. This library was developed and maintained by Faisal Salman, and it has gained strong adoption in the developer community due to its ease of use, extensive browser support, and reliable results.

With ua-parser-js, you can easily parse user agent strings and get precise information about the user’s browser, operating system, device, and more. The library provides a simple and intuitive API that can be easily integrated into your web projects.

In the following sections, we’ll learn about the ua-parser-js library, including its important features, installation methods, and usage examples. We’ll also discuss its recent licensing changes, which have sparked debates within the developer community.

ua-parser-js installation and setup

The ua-parser-js library can be installed using various methods, depending on your development environment and preferences. With a lightweight footprint of approximately 18KB minified and 7.9KB gzipped, ua-parser-js can be easily integrated into both client-side (browser) and server-side (Node.js) environments.

To use ua-parser-js in an HTML file, you can simply include the library script in your HTML file:

<!DOCTYPE html>
<html>
  <head>
    <script src="ua-parser.min.js"></script>
  </head>
  <body>
    var parser = new UAParser();
    <!-- Your content goes here -->
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Download the minified JavaScript file, and include it in the same directory level as the HTML file. If you're using ua-parser-js in a Node.js environment, you can install it using npm:

npm install ua-parser-js
Enter fullscreen mode Exit fullscreen mode

Then, in your Node.js script, you can require the library:

const UAParser = require('ua-parser-js');
Enter fullscreen mode Exit fullscreen mode

For TypeScript projects, you can install the library along with its type definitions using npm:

npm install --save ua-parser-js @types/ua-parser-js
Enter fullscreen mode Exit fullscreen mode

Then, in your .ts file, you can import the library:

import { UAParser } from "ua-parser-js";
const parser = new UAParser()
Enter fullscreen mode Exit fullscreen mode

Usage and examples

The ua-parser-js library provides a simple API for parsing user agent strings and accessing the parsed data.

To parse a user agent string, you can create an instance of the UAParser object and call the setUA method with the user agent string:

const parser = new UAParser();
parser.setUA('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36');
Enter fullscreen mode Exit fullscreen mode

Once the user agent string is parsed, you can access the parsed data using the available methods provided by the UAParser object:

const result = parser.getResult();
console.log(result.browser); // {name: "Chrome", version: "93.0.4577.82", major: "93"}
console.log(result.os);      // {name: "Windows", version: "10"}
console.log(result.device);  // {vendor: undefined, model: undefined, type: undefined}
Enter fullscreen mode Exit fullscreen mode

The getResult method returns an object containing the parsed data, including information about the browser, operating system, device, CPU, and engine.

Using extensions

ua-parser-js also allows you to extend its parsing capabilities by providing custom regular expressions and parsing rules. You can pass an array of extensions when creating a new instance of the UAParser object:

const myExtensions = [
  [/(myapp)\/([\w\.]+)/i, [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]],
];
const parser = new UAParser(navigator.userAgent, myExtensions);
Enter fullscreen mode Exit fullscreen mode

With these features and examples, you should have a good understanding of how to install, set up, and use ua-parser-js in your web development projects. In the next section, we'll explore the recent licensing changes surrounding ua-parser-js and their implications for developers and the open source community.

The ua-parser-js license change

Recently, ua-parser-js underwent a significant license change that sparked discussions in the developer community. Before the change, ua-parser-js was initially distributed under the MIT license, which is known for its permissive nature. This license allowed developers to use, modify, and distribute the library with minimal restrictions, making it a popular choice for both open source and commercial projects.

ua-parser-js has grown in popularity, with over 2,240 dependent projects, and has been downloaded more than 12.3 million times. This growth has led to increased maintenance demands and a need for a more sustainable development model. The new licensing model aims to generate revenue to support ongoing maintenance and development efforts.

With the recent release of version 2.0, ua-parser-js adopted a dual licensing model: AGPLv3 (GNU Affero General Public License version 3) for the free and open source version and a proprietary, PRO license for commercial use. This change caused a significant shift in how developers can use and distribute ua-parser-js in their projects.

The dual licensing model tries to achieve a middle ground between maintaining an open source library and profiting from commercial users who might need extra functions or support. Currently, commercial projects are faced with a decision — they either abide by AGPLv3 license terms (which may require them to release their own source code) or buy a PRO license. The PRO license pricing starts from $12 for personal use and goes up to $500 for enterprise use. This model, often referred to as "open core," has been adopted by other projects in the open source ecosystem, such as Sidekiq, Mastodon, Nextcloud, and others.

There's been talk of potential forks of the MIT-licensed version or the development of alternative libraries. For example, Node.js TSC member Matteo Collina has already created a fork called my-ua-parser to maintain an MIT-licensed version.

As you navigate this transition, it's important for you to understand the changes and consider how they might impact your projects. In the next section, we'll explore some strategies for dealing with this license change in your own work.

Navigating the license change as a developer

When deciding which license to use, you need to consider your project's nature and requirements, reassess its dependencies, and make informed decisions to avoid the challenges that license change presents.

If your project is already using a compatible open source license, then the AGPLv3 version might be suitable. This means you’ll make your entire application's source code available if you distribute it or run it as a network service. However, keep in mind that using the AGPL version might limit the adoption of your project by others who can't comply with AGPL terms.

But if you're developing proprietary software or can't comply with AGPL terms, you should consider purchasing the PRO license; evaluate if the cost of the PRO license is justified by the benefits and features you need from ua-parser-js. Alternatively, you can continue using the v1.x branch or forks of ua-parser-js, which remains under the MIT license. But you should note that this version may receive limited updates in the future.

Conclusion

For years, ua-parser-js has been appreciated as a valuable tool for web developers. Its ability to accurately parse user agent strings and provide detailed information about browsers, operating systems, and devices has made it an essential library for many of us.

The switch from the MIT license to a double AGPLv3 + PRO model undoubtedly caused a stir in the developer community. We witnessed a variety of responses to it; some community members were understanding while others demonstrated concern and opposition. To some, it would mean adjusting their projects to comply with the AGPLv3 license, while for others it might involve purchasing a PRO license or looking for alternative solutions.

As users of open source software, we need to be prepared for such changes and have strategies in place to adapt when necessary.

Top comments (0)