Skip to content

Instantly share code, notes, and snippets.

@HananoshikaYomaru
Created April 5, 2023 10:48
Show Gist options
  • Save HananoshikaYomaru/bd635a576e6fff1a4d64c2b469b70587 to your computer and use it in GitHub Desktop.
Save HananoshikaYomaru/bd635a576e6fff1a4d64c2b469b70587 to your computer and use it in GitHub Desktop.
getVercelBranchUrl.js
const { createHash } = require("crypto");
function sha256(string) {
return createHash("sha256").update(string).digest("hex");
}
const firstUrlMaxLength = 74;
/**
*
* https://vercel.com/docs/concepts/deployments/generated-urls#truncation
*
* @param {string} projectName
* @param {string} branchName
* @param {string} scopeName
* @returns {string} the generated branch url
*/
const getVercelBranchUrl = (projectName, branchName, scopeName) => {
const firstUrl =
`${projectName}-git-${branchName}-${scopeName}.vercel.app`.replace(
"/",
"-"
);
if (firstUrl.length <= firstUrlMaxLength) return firstUrl;
const temp = `git-${branchName}${projectName}`;
const hash = sha256(temp).slice(0, 6);
const secondPart = `-${hash}-${scopeName}`;
return `${firstUrl.substring(
0,
firstUrlMaxLength - ".vercel.app".length - secondPart.length
)}${secondPart}.vercel.app`.replace("/", "-");
};
module.exports = {
getVercelBranchUrl,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment