npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-useragent

v4.0.2

Published

Parses browser user-agent strings for Astro

Downloads

333

Readme

🚀 Astro UserAgent

version downloads github actions typescript makepr

Astro UserAgent is a simple helper for parsing user-agent header strings for browser matching inside your Astro Pages / API routes, when using SSR Mode

Note Due to the nature of Astro being an SSG by trade, This package only works when used with astro in SSR Mode.

📦 Installation

First, install the astro-useragent package using your package manager. (If you aren’t sure which package manager you’re using, run the first command.)

Using PNPM

pnpm install astro-useragent

Using NPM

npm install astro-useragent

Using Yarn

yarn add astro-useragent

🥑 Usage

Enable SSR mode

To get started, enable SSR features in development mode with the output: server configuration option:

import { defineConfig } from 'astro/config';

export default defineConfig({
  output: 'server'
});

Note For more info about SSR mode, please refer to the official docs.

Usage with Astro pages

To parse a user-agent string inside any of your top level Astro pages, import useUserAgent and then use it inside the frontmatter section:

---
import { useUserAgent } from "astro-useragent";

const uaString = Astro.request.headers.get("user-agent");
const { source, isMobile } = useUserAgent(uaString);
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>My Astro website</title>
  </head>
  <body>
    <p>Source: {source}</p>
    {isMobile ? <p>I'm on mobile</p> : <p>I'm on desktop</p>}
  </body>
</html>

Note Read more about Astro request headers here: Astro Docs

Usage with Astro API routes

useUserAgent can also be used inside your API routes, to perform some logic based on the client user-agent.

In the example below, an API route is used to redirect a user to a different mobile page when he is using a mobile client, otherwise it serves the normal content.

import type { APIContext } from 'astro';
import { useUserAgent } from 'astro-useragent';

export async function get({ request }: APIContext) {
  const uaString = request.headers.get('user-agent');
  const { isMobile } = useUserAgent(uaString);

  if (isMobile) {
    return Response.redirect('mobile.mysite.com', 307);
  }

  const greetings = {
    message: 'hello from astro API'
  };

  return new Response(JSON.stringify(greetings), {
    status: 200
  });
}

Note Read more about Astro API routes here: Astro Docs

We have also setup an example repository available here: example-useragent

Parsed object interface

The parsed UserAgent object will have the following interface:

export interface UserAgent {
  readonly source: string; // original user agent string.
  readonly deviceType: string | null;
  readonly deviceVendor: string | null;
  readonly os: string;
  readonly osVersion: number;
  readonly browser: string;
  readonly browserVersion: number;
  readonly engine: string;
  readonly engineVersion: number;
  readonly isIphone: boolean;
  readonly isIpad: boolean;
  readonly isMobile: boolean;
  readonly isTablet: boolean;
  readonly isDesktop: boolean;
  readonly isChrome: boolean;
  readonly isFirefox: boolean;
  readonly isSafari: boolean;
  readonly isIE: boolean;
  readonly isEdge: boolean;
  readonly isOpera: boolean;
  readonly isMac: boolean;
  readonly isChromeOS: boolean;
  readonly isWindows: boolean;
  readonly isIos: boolean;
  readonly isAndroid: boolean;
}

Caveats

UserAgent-based mobile detection isn’t always accurate. Instead, use the following client-side function:

function isMobile() {
    const match = window.matchMedia('(pointer:coarse)')
    return match && match.matches
}

Changelog

Please see the Changelog for more information on what has changed recently.

Acknowledgements

astro-useragent is a port from next-useragent to Astro. so big thanks to Tsuyoshi Tokuda and the contributors behind next-useragent package.