Isomorphic Next

Usage Examples

Practical examples of using isomorphic-next in your Next.js applications

Usage Examples

Basic Usage

Managing Cookies

import { cookies } from "isomorphic-next/headers";
 
// Setting a cookie
function setUserPreference(theme: string) {
  cookies().set("theme", theme, {
    maxAge: 30 * 24 * 60 * 60, // 30 days
    path: "/",
  });
}
 
// Reading a cookie
function getUserPreference() {
  const theme = cookies().get("theme");
  return theme?.value ?? "light";
}

Reading Headers

import { headers } from "isomorphic-next/headers";
 
// In any component or page
function MyComponent() {
  const userAgent = headers().get("user-agent");
  return <div>Your browser: {userAgent}</div>;
}

Advanced Examples

Authentication Token Management

import { cookies } from "isomorphic-next/headers";
 
export function handleLogin(token: string) {
  cookies().set("auth-token", token, {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    sameSite: "strict",
    path: "/",
  });
}
 
export function handleLogout() {
  cookies().delete("auth-token");
}
 
export function getAuthToken() {
  return cookies().get("auth-token")?.value;
}

Language Preference

import { cookies, headers } from "isomorphic-next/headers";
 
function getPreferredLanguage() {
  // First check cookie preference
  const cookieLang = cookies().get("preferred-language")?.value;
  if (cookieLang) return cookieLang;
 
  // Fall back to Accept-Language header
  const acceptLanguage = headers().get("accept-language");
  return acceptLanguage?.split(",")[0] ?? "en";
}

On this page