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
}