shadimage
nuqs
itemadapter-inertiasource: ok · refreshed

Inertia.js Adapter

Using nuqs in Inertia.js apps (eg: with a Laravel backend)

npx shadcn add "https://nuqs.dev/r/adapter-inertia.json"
free

No selection tags yet — empty means no evidence, not lack of support.

Dependencies

Bundle weight · light
nuqs

Files1 · source cached 10/8/2026, 6:16:37 AM

src/registry/remote/adapter-inertia.ZX5GXxl__2vN.txt
import { router, usePage } from '@inertiajs/react';
import {
  unstable_createAdapterProvider as createAdapterProvider,
  renderQueryString,
  type unstable_AdapterInterface as AdapterInterface,
  type unstable_AdapterOptions as AdapterOptions,
  type unstable_UpdateUrlFunction as UpdateUrlFunction
} from 'nuqs/adapters/custom';
import * as React from 'react';

const getOrigin = () =>
  typeof window !== 'undefined' ? window.location.origin : 'http://localhost';

function useNuqsInertiaAdapter(): AdapterInterface {
  const currentUrl = usePage().url;
  // We need the searchParams to be optimistic to avoid
  // flickering when the internal state is updated
  // but the URL is not yet updated.
  const [searchParams, setSearchParams] = React.useState(
    new URL(currentUrl, getOrigin()).searchParams
  );

  React.useEffect(() => {
    setSearchParams(new URL(currentUrl, getOrigin()).searchParams);
  }, [currentUrl]);

  const updateUrl: UpdateUrlFunction = React.useCallback(
    (search: URLSearchParams, options: AdapterOptions) => {
      const url = new URL(window.location.href);
      url.search = renderQueryString(search);
      setSearchParams(url.searchParams);

      // Server-side request
      if (options?.shallow === false) {
        router.visit(url, {
          replace: options.history === 'replace',
          preserveScroll: !options.scroll,
          preserveState: true,
          async: true
        });
        return;
      }

      const method = options.history === 'replace' ? 'replace' : 'push';

      router[method]({
        url: url.pathname + url.search + url.hash,
        clearHistory: false,
        encryptHistory: false,
        preserveScroll: !options.scroll,
        preserveState: true
      });
    },
    []
  );

  return {
    searchParams,
    updateUrl
  };
}

export const NuqsAdapter = createAdapterProvider(useNuqsInertiaAdapter);

More from nuqs

Browse all components in this library →