Skip to content

Commit

Permalink
final version
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Nov 21, 2024
1 parent 116652c commit aca3f9f
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions apps/web/app/deeplink/[url]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
export const runtime = "edge";

export default function DeepLinkPage({ params }: { params: { url: string } }) {
const url = decodeURIComponent(params.url).replace(/\+/g, " ");
// First decode the full URL parameter from the route
const url = decodeURIComponent(params.url);
// Split into base URL and query string
const [front, query] = url.split("?");

return <meta httpEquiv="refresh" content={`0; url=${url}`} />;
let redirectUrl = url;

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

// 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 = `${front}?${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}`} />;
}

0 comments on commit aca3f9f

Please sign in to comment.