Coder Social home page Coder Social logo

next-server-context's Introduction

Next Server Context

The missing server context for Next.js App Directory.

Install

pnpm add @sodefa/next-server-context

Page Context

/app/[myParam]/page.tsx

import { pageContext } from '@sodefa/next-server-context'
import { NestedServerComponent } from './NestedServerComponent'

export default pageContext.Wrapper(({ params, searchParams }) => {
  return (
    <>
      <h1>MyPage</h1>
      <NestedServerComponent />
    </>
  )
})

/app/[myParam]/NestedServerComponent.tsx

import { pageContext } from '@sodefa/next-server-context'

export const NestedServerComponent = () => {
  const { params, searchParams } = pageContext.getOrThrow()
  return <div>NestedServerComponent: {params.myParam}</div>
}

Custom Context

/myContext.ts

import { createServerContext } from '@sodefa/next-server-context'

const myContext = createServerContext<{
  myValue: string
}>()

/ParentComp.tsx

import { myContext } from './myContext'

export const ParentComp = () => {
  myContext.set({ myValue: 'hi there' })
  return <ChildComp />
}

/ChildComp.tsx

import { myContext } from './myContext'

export const ChildComp = () => {
  const { myValue } = myContext.getOrThrow()
  return <div>ChildComp: {myValue}</div>
}

Zod Context

/app/zod/[singleParam]/[...deepParams]/myContext.ts

import { createServerContextWithZod } from '@sodefa/next-server-context'
import { z } from 'zod'

export const myContext = createServerContextWithZod(
  z.object({
    params: z.object({
      singleParam: z.string(),
      deepParams: z.array(z.string()),
    }),
    searchParams: z
      .object({
        p1: z.string().optional(),
        p2: z.array(z.string()).optional(),
      })
      .catchall(z.union([z.string(), z.array(z.string())])), // Allow additional search params (optional)
  })
)

/app/zod/[singleParam]/[...deepParams]/page.tsx

import { NestedServerComponent } from './NestedServerComponent'
import { myContext } from './myContext'

export default myContext.Wrapper(() => {
  return (
    <>
      <h1>Page with a lot of Params</h1>
      <NestedServerComponent />
    </>
  )
})

/app/zod/[singleParam]/[...deepParams]/NestedServerComponent.tsx

import { myContext } from './myContext'

export const NestedServerComponent = () => {
  const { params, searchParams } = myContext.getOrThrow()
  return (
    <pre className="p-2 border rounded flex flex-col gap-4 font-mono text-xs ">
      {JSON.stringify({ params, searchParams }, null, 2)}
    </pre>
  )
}

next-server-context's People

Contributors

rechenberger avatar felix-gehring avatar

Stargazers

Katherine Martinez avatar A. Göktuğ Yalçın avatar Onur avatar Ian Jamieson avatar  avatar Pavel Shapov avatar Julian avatar

Watchers

 avatar  avatar Veys Aliyev avatar

Forkers

felix-gehring

next-server-context's Issues

Flaws

  • myContext.get() does not work inside server actions
  • looks like revalidate(/my-page) breaks, when /my-page is using myContext.get()
  • From my understanding Next's searchParams is a proxy that checks if searchParams are read and decides on how dynamic that site is. not sure if its triggered by zodSchema.parse()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.