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 🙏

© 2026 – Pkg Stats / Ryan Hefner

next-request-metadata

v2.0.0

Published

Next.js SSR request metadata context

Readme

next-request-metadata

Lightweight utility to create and share per-request metadata for Next.js standalone mode apps.

Share x-request-id and more across SSR getServerSideProps and API handler call stack without a need to pass down arguments.

This library works in Node.js server environment only.

Installation

npm install next-request-metadata
# or
yarn add next-request-metadata

Quickstart

  • Setup shared request metadata consumer:
// logger.ts
import setup from "next-request-metadata";

const { getMetadata, metadataRequestWrapper } = setup();

const logger = (message: string) => {
  const requestMetadata = getMetadata();

  console.log(`[LOG]: ${JSON.stringify({ ...requestMetadata, message })}`);
};

export { logger, metadataRequestWrapper };
  • Wrap API handler or getServerSideProps with metadataRequestWrapper().
  • Call getMetadata() from any nested function inside the wrapped call to access the current request metadata.

getServerSideProps (SSR) example:

// SSR page
import { metadataRequestWrapper } from "@/logger";
import { calculateSomething } from "@/helperFunction";

export const getServerSideProps = metadataRequestWrapper(async (context) => {
  const someProp = calculateSomething(); // functions will share request metadata

  return {
    props: { someProp },
  };
});

API route example:

// API route
import { metadataRequestWrapper } from "next-request-metadata";
import { calculateSomething } from "@/helperFunction";

const handler = (req, res) => {
  const something = calculateSomething(); // functions will share request metadata
  res.status(200).json({ something });
};

// Use prepareMetadataDefault to ensure x-request-id is set on the response.
export default metadataRequestWrapper(handler);

Functions in the middle - no need to pass down metadata in arguments:

// helperFunction.ts
import { logger } from "@/logger";

export const calculateSomething = () => {
  logger('Calculating something');

  return 'Something important';
};

Result for both API handler and getServerSideProps:

  • log messages: [LOG]: {"x-request-id":"94496d60-0420-43c8-a446-89735cb4f036","message":"Calculating something"}
  • HTTP response: HTTP/1.1 200 OK x-request-id: 94496d60-0420-43c8-a446-89735cb4f036

Examples

See ideas described above in more advanced examples.

API reference

setup([prepareMetadata]) => requestMetadata

The exported setup function takes one optional argument prepareMetadata and returns a requestMetadata instance.

import setup from "next-request-metadata";

prepareMetadata (Function)

Default: prepareRequestIdMetadata

Creates Metadata shared within requestMetadata.metadataRequestWrapper wrapped context.

Function executed each time metadataRequestWrapper wrapped function is called. It's result can be accessed with requestMetadata.getMetadata.

import setup from "next-request-metadata";
import { GetServerSidePropsContext } from "next";

type Metadata = {
  origin: string | undefined;
}

const prepareMetadata = (context: GetServerSidePropsContext) => ({
  origin: context.req.headers['origin'],
});

const { getMetadata, metadataRequestWrapper } = setup<Metadata>(prepareMetadata);
prepareRequestIdMetadata (Function)

Prepares metadata object with x-request-id only. Supports both Next.js getServerSideProps and API handlers arguments.

Behavior:

  • Reads x-request-id from req.headers when present; otherwise generates a UUID.
  • Sets x-request-id on the response via res.setHeader.
  • Returns an object of shape { "x-request-id": string }.

If arguments do not match SSR nor API handler convention, the function returns an empty object.

requestMetadata instance

The requestMetadata instance is the module returned by the default exported next-request-metadata function.

const { getMetadata, metadataRequestWrapper } = setup();

metadataRequestWrapper (Function)

Wraps a function with a per-call metadata, prepared with prepareMetadata.

Can be used to wrap Next.js SSR getServerSideProps or API handlers functions, to share request metadata context across nested functions.

// Next.js SSR Page
export const getServerSideProps = metadataRequestWrapper(originalGetServerSideProps);

getMetadata (Function)

Gets metadata for the current async request context, created by prepareMetadata per each metadataRequestWrapper wrapped function call.

Returns Metadata object, or undefined if called outside of a wrapped invocation.

Performance

TBC