Cookbook Router
Practical Patterns

Preserve unknown search parameters

Keep campaign, experiment, and partner query parameters without polluting typed route state.

Preserve unknown search params

Sometimes the URL carries value your route contract does not own. Preserve unknown search params when UTM tags, attribution keys, or partner parameters need to survive navigation.

const router = createRouter({
  routes,
  url: {
    unknownSearch: 'preserve',
  },
});

Read undeclared keys separately from typed declared search:

import { useSearchParams, useUnknownSearchParams } from '@cookbook/router-react';

function Attribution() {
  const search = useSearchParams('landing');
  const unknownSearch = useUnknownSearchParams();

  analytics.track('landing-view', {
    campaign: search.campaign,
    utmSource: unknownSearch.utm_source,
  });

  return null;
}

If unknown keys should not survive routing, keep the default 'strip' behavior or configure unknownSearch: 'strip' explicitly.

On this page