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 🙏

© 2025 – Pkg Stats / Ryan Hefner

nextjs-url

v1.0.4

Published

Get absolute url, api url and all for your Next.JS project.

Readme

this package is inspired by next-absolute-url

nextjs-url

Get the protocol, host and all for your Next.js app

This module enables you to easily get the protocol, host and all URL params of your Next.js app, both on the server and the client.

Install

With npm installed, run

npm install nextjs-url --save

or with yarn installed, run

yarn add nextjs-url

This package will let you get the current url of your running Next.js website. You can get the url server side or clientside. For server side calling, you have to provide a request object as a prop. You can get this object from getServerSideFunction. So it is important to note that you can not run this module in a static page as your domain is assigned after you page is built. You may consider calling it client side in this senario or use environment variable for preknown domains.

If you are deploying with Vercel, you may consider using System Environment Variables.

Structure

By default the function return an URL Object which is structured something like:

URL {
    hash,
    host,
    hostname,
    href,
    origin,
    password,
    pathname,
    port,
    protocol,
    search,
    searchParams: URLSearchParams {},
    username,
}

Usage

For server side calling:

You must provide a request for server side calling which can be acured by calling getServerSideProps.

import { getBaseUrl, getApiRoot } from "nextjs-url";

export async function getServerSideProps({ req }) {
  const baseUrl = getBaseUrl(req).href; // http://localhost:<PORT>/
  const apiRoot = getApiRoot(req).href; // http://localhost:<PORT>/api/

  //Do something with the data

  return {
    props: {
      apiRoot,
      baseUrl,
    },
  };
}

For calling in API:

You may sometimes need to call another API from a API. In that case you can call the module with the API request prop.

import { getBaseUrl, getApiRoot } from "nextjs-url";

export default function Test(req, res) {
  const baseUrl = getBaseUrl(req).href; // http://localhost:<PORT>/
  const apiRoot = getApiRoot(req).href; // http://localhost:<PORT>/api/

  //Do something with the data

  res.send({ apiRoot, baseUrl });
}

For client side calling:

For client side calling you don't need the request object just call the function without any prop. Remeber that you can't call it without request prop in server side.

import { getBaseUrl, getApiRoot } from "nextjs-url";
import { useEffect } from "react";

export default function MyApp() {
  useEffect(() => {
    const baseUrl = getBaseUrl().href; // http://localhost:<PORT>/
    const apiRoot = getApiRoot().href; // http://localhost:<PORT>/api/

    //Do something with the data
  }, []);
  return <h>Hello World 🙋‍♂️</h>;
}

If you deployed your Next.js app with now the baseUrl will be something like https://<your-app>.now.sh/api/.

However, if you are running the app locally the baseUrl will be http://localhost:<PORT>/ instead.

Same thing with the apiRoot.

MIT