sindresorhus / normalize-url

Normalize a URL
MIT License
837 stars 123 forks source link

Add removeQueryParametersByValue option #176

Closed mlshvdv closed 6 months ago

mlshvdv commented 1 year ago

The option removeQueryParametersByValue removes query parameters that match the specified values. May be helpful to make a canonical URL.

normalizeUrl('www.sindresorhus.com?foo=bar&page=1&page_size=30', {
    removeQueryParametersByValue: [
        {
            key: 'page',
            value: 1
        },
        {
            key: 'page_size',
            value: 10
        }
    ]
});
//=> 'http://sindresorhus.com/?foo=bar&page_size=30'
sindresorhus commented 1 year ago

Do you have an actual real-world use-case for this?

mlshvdv commented 1 year ago

@sindresorhus

For example, the pages https://example.com/posts and https://example.com/posts?page=1 are the same. When you need to create a canonical URL for a search engine's crawler (to put in <link rel="canonical" href="https://..." />, the option removeQueryParametersByValue will help with it to prevent page duplication (https://developers.google.com/search/docs/advanced/crawling/consolidate-duplicate-urls)

Another example. The page has a posts list where users can choose post count per page. By default, only 10 records are displayed per page. In that case, we don't need to have page_size=10 in the URL for pagination. But, when the user chooses another number, for example 25, the full canonical URL for this page must contain page_size=25.

sindresorhus commented 1 year ago

Alright. Makes sense. I think you should document the use-case in text though. That it's useful when you want to remove query parameters that are the same as the default.

sindresorhus commented 1 year ago

Bump :)