Explore

Explore snippets

Discover reusable code snippets, examples, fixes, and ideas shared by developers, students, and teams.

Public snippets by CodShot user
Clear
10 public snippets
Open
TSX
Public

Parallel Data Fetching in a Server Component

Start independent server requests together to reduce total rendering time.

TSX Preview
// app/dashboard/page.tsx

async function getUser() {
  return db.user.findFirst();
}

async function getRecentOrders() {
  return db.order.findMany({
    orderBy: {
      createdAt: 'desc',
    },
    take: 5,
  });
}
#nextjs #server-components #async #performance
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #230
Open
TSX
Public

Next.js Route Error Boundary

Handle uncaught route segment errors and let the user retry without a full reload.

TSX Preview
// app/dashboard/error.tsx
'use client';

import { useEffect } from 'react';

type ErrorPageProps = {
  error: Error & {
    digest?: string;
  };
  reset: () => void;
};

export default function DashboardError({
  error,
  reset,
}: ErrorPageProps) {
#nextjs #error-handling #app-router #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #229
Open
TSX
Public

Next.js Loading Skeleton

Provide instant loading UI for an App Router segment with loading.tsx.

TSX Preview
// app/dashboard/loading.tsx

export default function DashboardLoading() {
  return (
    <main aria-busy="true" aria-label="Loading dashboard">
      <div className="skeleton skeleton-title" />

      <div className="skeleton-grid">
#nextjs #suspense #loader #ui
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #228
Open
TypeScript
Public

Dynamic Metadata with generateMetadata

Generate page-specific SEO metadata from a dynamic App Router route.

TypeScript Preview
// app/blog/[slug]/page.tsx

import type { Metadata } from 'next';

type PageProps = {
  params: Promise<{
    slug: string;
  }>;
};

async function getPost(slug: string) {
  return db.post.findUnique({
    where: { slug },
    select: {
      title: true,
#nextjs #seo #meta #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #227
Open
TypeScript
Public

Next.js Route Handler with JSON Validation

Validate a JSON request body and return typed responses from an App Router Route Handler.

TypeScript Preview
// app/api/tasks/route.ts

import { NextResponse } from 'next/server';

type CreateTaskBody = {
  title?: unknown;
};

export async function POST(request: Request) {
  let body: CreateTaskBody;

  try {
    body = await request.json();
  } catch {
#nextjs #route-handler #api #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #226
Open
TypeScript
Public

Revalidate a Next.js Cache Tag after Mutation

Mark tagged data stale after a Server Action changes the underlying records.

TypeScript Preview
// app/actions/posts.ts
'use server';

import { revalidateTag } from 'next/cache';

export async function publishPost(postId: string) {
  const post = await db.post.update({
    where: {
      id: postId,
    },
    data: {
      published: true,
    },
  });
#nextjs #revalidation #server-actions #caching
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #225
Open
TypeScript
Public

Next.js Cached Data with use cache

Cache a server data function and attach a reusable invalidation tag.

TypeScript Preview
// app/lib/posts.ts

import {
  cacheLife,
  cacheTag,
} from 'next/cache';

export async function getPublishedPosts() {
  'use cache';

  cacheLife('hours');
  cacheTag('posts');

  return db.post.findMany({
    where: {
      published: true,
    },
#nextjs #caching #server-components #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #224
Open
TypeScript
Public

Next.js Proxy Authentication Guard

Redirect unauthenticated requests from protected routes with the Next.js proxy convention.

TypeScript Preview
// proxy.ts

import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

export function proxy(request: NextRequest) {
  const sessionId = request.cookies.get('session')?.value;

  if (!sessionId) {
#nextjs #proxy #auth #security
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #223
Open
TSX
Public

Next.js Server Action Form with useActionState

Connect a Next.js Server Action to a client form with validation and pending state.

TSX Preview
// app/actions.ts
'use server';

export type CreateUserState = {
  message: string;
  errors: {
    email?: string;
  };
};

export async function createUser(
  previousState: CreateUserState,
  formData: FormData,
): Promise<CreateUserState> {
#nextjs #server-actions #form #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #222
Open
TSX
Public

Dynamic Page with notFound

Render the nearest not-found UI when a requested App Router record does not exist.

TSX Preview
// app/products/[id]/page.tsx

import { notFound } from 'next/navigation';

type PageProps = {
  params: Promise<{
    id: string;
  }>;
};

export default async function ProductPage({
  params,
}: PageProps) {
  const { id } = await params;
#nextjs #app-router #routing #error-handling
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #217
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support