Created
January 2, 2023 19:57
Dynamic URLs with Vercel redirects. Very hacky and should not be used in production.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Greet</title> | |
</head> | |
<body> | |
Hello! | |
<script src="./paramsToURL.js"></script> | |
<script> | |
const nameParam = paramsToURL().get('name'); | |
if (nameParam) { | |
document.body.innerHTML = `Hello, ${nameParam}!`; | |
} | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Dynamic URLs</title> | |
</head> | |
<body> | |
<h1>Dynamic URLs with Vercel redirects.</h1> | |
<p> | |
Greet the <a href="/greet/world">world</a>, or <a href="/greet">no one</a> | |
</p> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function paramsToURL() { | |
const urlParams = new URLSearchParams(window.location.search); | |
const arrayParams = Array.from(urlParams.values()); | |
window.history.replaceState( | |
null, | |
'', | |
`${window.location.pathname}${ | |
arrayParams.length > 0 ? '/' + arrayParams.join('/') : '' | |
}` | |
); | |
return urlParams; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cleanUrls": true, | |
"trailingSlash": false, | |
"redirects": [ | |
{ "source": "/greet/:name", "destination": "/greet?name=:name" } | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment