如何使用 URLSearchParams 建立可分享的旅遊篩選連結:處理重音符號、重複值與私隱
Shareable travel filters with URLSearchParams: accents, repeated values and privacy
Mapitt 團隊以虛構的教學例子示範如何使用 URL 和 URLSearchParams 建立可分享的旅遊搜尋連結,並處理含重音符號的目的地及重複的興趣參數。讀取重複參數應使用 getAll,並避免預先編碼或把原始值串接成查詢字串,以免加號被解讀為空格。URL 可能出現在瀏覽記錄、分析數據、伺服器日誌及分享截圖中,因此不應放入個人資料或其他敏感旅遊資訊。
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 names, contact details, booking references, tokens, exact current locations or other sensitive travel information in them. 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.
來源:DEV Community · dev.to