Skip to content
Niko Table

Server-Side Nuqs Table

Server-side pagination, sorting, and filtering, with the whole table state kept in the URL by nuqs.

Combines TanStack Query (fetching, caching, request dedup) with nuqs URL state. The URL becomes the cache key: share a link and the other person sees the same filtered, sorted, paginated view. Use it when server-side table views must be shareable or bookmarkable. It uses the same database-agnostic wire contract as the Server-Side Table. Read that page first. This one covers only the URL layer.

The demo runs against a mock server: an in-memory array of 2,000 products with simulated delay. Every filter works without a database. Everything in its MOCK SERVER section stands in for your backend. Try it: filter or sort, watch the “URL State” card update, and imagine sending that URL to a teammate.

server-side-nuqs-state.tsx
Product Name
Category
Brand
Price
Stock
Rating
In Stock
Release Date
URL State & Server Query
Every state slice lives in the URL — copy the address bar and the exact view is shareable. The same serializable shape is what the mock fetchProducts receives; swap it for a real API and any database works.
Status:Initial load...
{
  "page": 0,
  "pageSize": 10,
  "sorting": [],
  "search": "",
  "columnFilters": []
}

Install the DataTable core and add-ons for this example:

pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-pagination @niko-table/data-table-search-filter @niko-table/data-table-view-menu @niko-table/data-table-sort-menu @niko-table/data-table-filter-menu @niko-table/data-table-column-sort @niko-table/data-table-column-faceted-filter @niko-table/data-table-column-slider-filter @niko-table/data-table-column-date-filter

Install additional dependencies:

pnpm add @tanstack/react-query nuqs

This example also uses checkbox from Shadcn UI:

pnpm dlx shadcn@latest add checkbox

First time using @niko-table? See the Installation Guide to set up the registry.

Wrap your app once, at the root, with both providers. Pick the nuqs adapter for your framework (adapter docs):

app/layout.tsx (Next.js App Router)
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { NuqsAdapter } from "nuqs/adapters/next/app"
// Pages Router: "nuqs/adapters/next/pages" — React SPA: "nuqs/adapters/react"
const queryClient = new QueryClient({
defaultOptions: {
queries: { staleTime: 60 * 1000, refetchOnWindowFocus: false },
},
})
export default function RootLayout({ children }) {
return (
<html>
<body>
<QueryClientProvider client={queryClient}>
<NuqsAdapter>{children}</NuqsAdapter>
</QueryClientProvider>
</body>
</html>
)
}

The example includes its own providers so it runs as copied. Remove that wrapper once your root provides them.

Use one parser per state slice. The keys are also the URL param names:

import { parseAsInteger, parseAsJson, parseAsString, useQueryStates } from "nuqs"
const tableStateParsers = {
page: parseAsInteger.withDefault(0),
perPage: parseAsInteger.withDefault(10),
sort: parseAsJson<SortingState>(v => v as SortingState).withDefault([]),
// mixed-shape columnFilters (widget values + advanced-menu filter objects)
filters: parseAsJson<ColumnFiltersState>(v => v as ColumnFiltersState).withDefault([]),
search: parseAsString.withDefault(""),
// OR/MIXED advanced filters — an object, never a string
global: parseAsJson<GlobalFilterObject | null>(v =>
v && typeof v === "object" && "filters" in v ? (v as GlobalFilterObject) : null,
).withDefault(null),
cols: parseAsJson<ColumnVisibilityState>(v => v as ColumnVisibilityState).withDefault({}),
}
const [urlParams, setUrlParams] = useQueryStates(tableStateParsers, {
history: "replace", // don't spam the back button
scroll: false,
shallow: true,
})

A filtered view makes a URL like this:

?page=0&perPage=10&sort=[{"id":"price","desc":true}]&filters=[{"id":"category","value":["electronics"]}]&search=pro

Build TanStack state from the URL, and write updates back in the handlers. Every filter or sort change also resets page to 0:

// URL → state
const pagination = useMemo(
() => ({ pageIndex: urlParams.page, pageSize: urlParams.perPage }),
[urlParams.page, urlParams.perPage],
)
const sorting = urlParams.sort
const columnFilters = urlParams.filters
// search string and the OR/MIXED filter object share the globalFilter slot
const globalFilter = urlParams.global ?? urlParams.search
// state → URL
const handleSortingChange = (updater: Updater<SortingState>) => {
const next = typeof updater === "function" ? updater(sorting) : updater
void setUrlParams({ sort: next.length > 0 ? next : null, page: 0 })
}

Passing null removes a param from the URL, so default states stay clean.

Two details keep URLs short and stable:

  • filterId is removed on write (serializeFiltersForUrl) and recreated on read (normalizeFiltersFromUrl). Both are exported from filters/table-filter-menu. The ids are internal to the menu UI. Recreating them by index keeps input focus stable.
  • Widget values pass through unchanged. Faceted string[], slider [min, max] tuples, and booleans are already URL-friendly JSON.

The advanced filter menu is controlled from the URL, just as the non-nuqs example controls it from useState. AND rules go in the filters param. OR and MIXED rules go in the global param. See handleMenuFiltersChange in the example.

Same as the Server-Side Table. The state from the URL feeds the query key, so a shared link hits the same cache entry.

const { data } = useQuery({
queryKey: ["products", pagination.pageIndex, pagination.pageSize, sorting, globalFilter, debouncedColumnFilters],
queryFn: () => fetchProducts({ ... }),
placeholderData: keepPreviousData,
})

The request shape is already in the URL, so a server-rendered page can run the same query during SSR from the incoming search params. The first paint needs no client round trip.

✅ Use Server-Side Nuqs Table when:

  • Everything from the Server-Side Table applies, and
  • Table views must be shareable, bookmarkable, or survive a refresh
  • You want back and forward to step through filter states (switch the snippet to history: "push")

❌ Consider other options when: