function versionlessRedirects()

in gulpfile.js [97:114]


function versionlessRedirects (text) {
  const lines = text.split('\n')
  const processed = lines.reduce((accum, line) => {
    accum.push(line)
    const m = line.match(REDIRECT_RX)
    if (m) {
      accum.push(`RedirectMatch 302 "^/${m.groups.component}(/?)$" "/${m.groups.component}/${m.groups.version}/"`)
      // The first line redirects **/next to **/next/ so the second line does not match.
      // Apparently it needs to be a match or it will transform **/next/ to **/next//
      accum.push(`RedirectMatch 301 "^/${m.groups.component}/next$" "/${m.groups.component}/next/"`)
      accum.push(`RedirectMatch 302 "^/${m.groups.component}/(?![0-9].*|next/)(.+)$" "/${m.groups.component}/${m.groups.version}/$1"`)
      // As an alternative, the following line works as long as no file names start with 'next'
      // accum.push(`RedirectMatch 302 "^/${m.groups.component}/(?![0-9].*|next)(.+)$" "/${m.groups.component}/${m.groups.version}/$1"`)
    }
    return accum
  }, [])
  return processed.join('\n')
}