Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.6 KB
Raw Blame
export const revalidate = false; // cache indefinitely

export function generateStaticParams() {
  return [];
}

export default async function CustomURISchemePage(props: {
  params: Promise<{ url: string }>;
}) {
  const params = await props.params;
  // First decode the full URL parameter from the route
  const url = decodeURIComponent(params.url);
  // Split into base URL and query string
  const [baseUrl, queryString] = url.split("?");

  let redirectUrl = url;

  // if there are query parameters, we need to process them
  if (queryString) {
    // Parse the query string (but don't use toString() later as it adds extra encoding)
    const queryParams = new URLSearchParams(queryString);

    // Process each parameter with proper encoding
    const processedParams = Array.from(queryParams.entries()).map(
      ([key, value]) => {
        // Handle form-encoded spaces ('+' → ' ')
        const decodedFromForm = value.replace(/\+/g, " ");
        // Decode any existing percent-encoding (e.g., '%26' → '&')
        const fullyDecoded = decodeURIComponent(decodedFromForm);
        // Apply one clean round of encoding
        const encoded = encodeURIComponent(fullyDecoded);

        return `${key}=${encoded}`;
      },
    );

    // Reconstruct the URL with properly encoded parameters
    redirectUrl = `${baseUrl}?${processedParams.join("&")}`;
  }

  // Redirect to the redirect URL (which may be the same as the original URL,
  // or a cleaned-up version with properly encoded parameters)
  return <meta httpEquiv="refresh" content={`0; url=${redirectUrl}`} />;
}