Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add loader feature #818

Merged
merged 13 commits into from
Dec 27, 2024
Merged

feat: Add loader feature #818

merged 13 commits into from
Dec 27, 2024

Conversation

franky47
Copy link
Member

@franky47 franky47 commented Dec 19, 2024

While we have the cache feature in Next.js app router to parse the incoming searchParams page prop and provide it to the rest of the RSC tree (via React's cache function, which is a hack but not the subject of this PR), other frameworks lacked the ability to parse an incoming request's search params and get the same kind of type-safe object back (without the caching part).

This PR introduces the createLoader function, a factory that takes in a search params descriptor object (a "parsers map" as we call it internally), and returns a load function. That function can then process a lot of different kinds of inputs:

  • URL object
  • Request object
  • URLSearchParams object
  • A string, whether of a fully qualified URL, or just the search part
  • A record of key/values as what Next.js usually gives us (Record<string, string | string[] | undefined>)

Example usage in Remix / React Router's loaders:

import { createLoader, parseAsInteger } from 'nuqs/server' // can also be imported from 'nuqs' in non-RSC frameworks

const searchParams = {
  pageIndex: parseAsInteger.withDefault(1),
  pageSize: parseAsInteger.withDefault(10)
}

const loadSearchParams = createLoader(searchParams, { 
  urlKeys: {
    pageIndex: 'page',
    pageSize: 'limit'
  }
})

// When called from /path?page=2&limit=50
export function loader({ request }: LoaderFunctionArgs) {
  const { pageIndex, pageSize } = loadSearchParams(request)
  // ^? { pageIndex: 2, pageSize: 50 }
  return fetchData(pageIndex, pageSize) 
}

Compatibility

  • React Routers & Remix loaders (using a Request object)
  • Next.js pages router getServerSideProps (needs a test)
  • Next.js app router searchParams prop (async in Next 15 -> good for PPR)
  • Next.js API routes

Tasks

  • Add docs

Copy link

vercel bot commented Dec 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuqs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 27, 2024 8:49pm

Copy link

pkg-pr-new bot commented Dec 19, 2024

Open in Stackblitz

npm i https://pkg.pr.new/nuqs@818

commit: 828445e

packages/docs/content/docs/server-side.mdx Outdated Show resolved Hide resolved
packages/docs/content/docs/server-side.mdx Outdated Show resolved Hide resolved
packages/docs/content/docs/server-side.mdx Outdated Show resolved Hide resolved
packages/docs/content/docs/server-side.mdx Outdated Show resolved Hide resolved
@franky47 franky47 merged commit 4762b04 into next Dec 27, 2024
26 checks passed
@franky47 franky47 deleted the feat/loaders branch December 27, 2024 21:03
Copy link

🎉 This PR is included in version 2.3.0-beta.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

github-actions bot commented Jan 1, 2025

🎉 This PR is included in version 2.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@franky47 franky47 mentioned this pull request Jan 3, 2025
@franky47 franky47 removed this from the 🚀 Shipping next milestone Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant