DEV Community

Shareable travel filters with URLSearchParams: accents, repeated values and privacy

By the Mapitt team. This educational example was prepared with AI assistance; it is fictional and does not describe Mapitt's implementation.

A travel search can become much easier to revisit when the destination and a few public filters are in its URL. There are several details worth handling deliberately: destinations can contain accents, a traveler may choose more than one interest, and a query string can be copied, logged or shared.

Build, don't concatenate

The browser's URL and URLSearchParams APIs handle query encoding. Start with raw values and let the API encode them; do not pre-encode values or interpolate them into a query string.

function createSearchLink(base, destination, interests) {
  const url = new URL(base);
  url.searchParams.set('destination', destination);
  url.searchParams.delete('interest');
  for (const interest of interests) {
    url.searchParams.append('interest', interest);
  }
  return url.href;
}

const link = createSearchLink(
  'https://example.com/search?language=es',
  'Mรฉrida + montaรฑa',
  ['cultura', 'naturaleza']
);

const parsed = new URL(link);
console.log(parsed.searchParams.get('destination')); // Mรฉrida + montaรฑa
console.log(parsed.searchParams.getAll('interest')); // ["cultura", "naturaleza"]

set replaces values for the single destination; append preserves each interest as a separate parameter. Starting with an existing URL keeps the unrelated language parameter. When reading filters, use getAll for a key that can appear multiple times.

The code uses example.com deliberately; it is not a claim about a live Mapitt endpoint.

Avoid a subtle plus-sign bug

If you build new URLSearchParams('destination=' + value), a literal + in value is interpreted as a space. Passing the raw value to set or append instead preserves it during the round trip. Similarly, pre-encoding a value and then passing it to set can encode the percent signs a second time.

Validate and keep private details out

Treat URL parameters as untrusted input. Limit length and accepted values, handle missing or repeated fields, and render text as text rather than injecting it as HTML. The server must apply its own validation even if the browser checks inputs.

URLs can appear in browser history, analytics, logs and shared screenshots; do not put the following in them:

  • names
  • contact details
  • booking references
  • tokens
  • exact current locations
  • other sensitive travel information

A link can preserve a broad search without exposing a traveler's private itinerary. For a production search, also provide labeled controls, sensible defaults and a clear way to reset filters. These examples demonstrate URL handling, not a complete search system.

References: MDN: URLSearchParams and MDN: getAll().

The Mapitt team works on travel discovery in Venezuela.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.