INNER CODE UNIT · JavaScript
getRedirect
cfug/flutter.cn · tool/host-redirect/src/index.js:42
function getRedirect(currentPath, redirectsOverride) {
const redirects = redirectsOverride || data.hosting.redirects
for (const redirect of redirects) {
try {
// redirects[].regex
if (redirect.regex) {
const regexp = RE2JS.compile(redirect.regex)
const regexpMatcher = regexp.matcher(currentPath)
if (regexpMatcher.matches()) {
let destination = redirect.destination
// 处理有命名的变量(类似 P<name>)
const namedGroups = regexp.namedGroups()
if (Object.keys(namedGroups).length > 0) {
for (const name in namedGroups) {
destination = destination.replaceAll(`:${name}`, regexpMatcher.group(name))
}
}