mancuoj

mancuoj

Better late than never.
github
twitter

Remove empty query parameters from the URL

To avoid the occurrence of URLs with empty query parameters like ?q=, you can detect and trigger redirection in the loader function.

async function clearEmptyParams(url: URL) {
  let shouldRedirect = false
  for (const [key, value] of url.searchParams.entries()) {
    if (value === '') {
      url.searchParams.delete(key)
      shouldRedirect = true
    }
  }
  if (shouldRedirect) {
    throw redirect(url.toString())
  }
}

export async function loader({ request }: LoaderFunctionArgs) {
  const url = new URL(request.url)
  await clearEmptyParams(url)

  // ... Other code
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.