Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

5.0 KB
Raw Blame
import {
  createPath,
  generatePath,
  matchPath,
  parsePath,
  redirect,
} from "react-router";
import { removeTrailingSlash } from "@webstudio-is/sdk";

type RedirectItem = {
  old: string;
  new: string;
  status?: number | string;
};

type MatchedRedirect = {
  url: string;
  status: number;
};

/**
 * Expands route params in local redirect targets.
 * External and protocol-relative URLs are returned unchanged because route params
 * only apply to app paths.
 */
export const generateRedirectUrl = (
  url: string,
  params: Record<string, string | undefined>
) => {
  if (
    /^[a-zA-Z][a-zA-Z\d+.-]*:/.test(url) ||
    url.startsWith("//") ||
    url.startsWith("?") ||
    url.startsWith("#")
  ) {
    return url;
  }

  const targetParams = Object.fromEntries(
    Object.entries(params).map(([name, value]) => [
      name,
      name === "*" ? value : value?.replaceAll("/", "%2F"),
    ])
  );
  const path = parsePath(url);
  try {
    return createPath({
      ...path,
      pathname: generatePath(path.pathname ?? "/", targetParams),
    });
  } catch {
    return url;
  }
};

const stripHash = (source: string) => {
  const hashIndex = source.indexOf("#");
  return hashIndex === -1 ? source : source.slice(0, hashIndex);
};

const getRedirectStatus = (status: RedirectItem["status"]) => {
  return Number(status) === 302 ? 302 : 301;
};

const decodePathname = (pathname: string) => {
  try {
    return decodeURI(pathname);
  } catch {
    return pathname;
  }
};

const getPathnameVariants = (pathname: string) => {
  return Array.from(new Set([pathname, decodePathname(pathname)]));
};

const preserveRequestSearch = (
  target: string,
  search: string,
  requestPathname: string
) => {
  if (search === "") {
    return target;
  }
  if (URL.canParse(target) || target.startsWith("//")) {
    const url = new URL(target, "https://webstudio.invalid");
    if (url.search !== "") {
      return target;
    }
    url.search = search;
    return target.startsWith("//")
      ? url.href.slice(url.protocol.length)
      : url.href;
  }
  const path = parsePath(target);
  if (path.search !== undefined && path.search !== "") {
    return target;
  }
  return createPath({
    ...path,
    pathname: path.pathname ?? requestPathname,
    search,
  });
};

const isOptionalSegmentMarker = (source: string, index: number) => {
  const nextChar = source[index + 1];
  if (nextChar !== undefined && nextChar !== "/") {
    return false;
  }

  const segmentStart = source.lastIndexOf("/", index - 1);
  const segment = source.slice(segmentStart + 1, index);
  return segment !== "";
};

const getSourceSearchIndex = (source: string) => {
  for (let index = 0; index < source.length; index += 1) {
    if (
      source[index] === "?" &&
      isOptionalSegmentMarker(source, index) === false
    ) {
      return index;
    }
  }
  return -1;
};

const isRedirectPattern = (source: string) => {
  return (
    /(^|\/):[^/]+/.test(source) ||
    /(^|\/)\*(?=\/|$)/.test(source) ||
    /(^|\/)[^/]+\?(?=\/|$)/.test(source)
  );
};

export const matchRedirect = (
  requestUrl: string,
  redirects: RedirectItem[]
): MatchedRedirect | undefined => {
  const url = new URL(requestUrl);
  const requestPathnames = getPathnameVariants(url.pathname);

  for (const redirect of redirects) {
    const source = stripHash(redirect.old);
    if (getSourceSearchIndex(source) !== -1) {
      const exactMatch = requestPathnames.some(
        (requestPathname) => source === `${requestPathname}${url.search}`
      );
      if (exactMatch) {
        return {
          url: generateRedirectUrl(redirect.new, {}),
          status: getRedirectStatus(redirect.status),
        };
      }
      continue;
    }

    if (isRedirectPattern(source) === false) {
      if (requestPathnames.includes(source)) {
        return {
          url: preserveRequestSearch(
            generateRedirectUrl(redirect.new, {}),
            url.search,
            url.pathname
          ),
          status: getRedirectStatus(redirect.status),
        };
      }
      continue;
    }

    const match = requestPathnames
      .map((requestPathname) =>
        matchPath(
          { path: source, caseSensitive: true, end: true },
          requestPathname
        )
      )
      .find((match) => match !== null);
    if (match === undefined) {
      continue;
    }

    return {
      url: preserveRequestSearch(
        generateRedirectUrl(redirect.new, match.params),
        url.search,
        url.pathname
      ),
      status: getRedirectStatus(redirect.status),
    };
  }
};

export const redirectRequest = (
  request: Request,
  redirects: RedirectItem[]
): Response | undefined => {
  const matchedRedirect = matchRedirect(request.url, redirects);
  if (matchedRedirect !== undefined) {
    return redirect(matchedRedirect.url, matchedRedirect.status);
  }

  const url = new URL(request.url);
  if (url.pathname.startsWith("//")) {
    return;
  }

  const pathname = removeTrailingSlash(url.pathname);
  if (pathname === url.pathname) {
    return;
  }

  return redirect(`${pathname}${url.search}`, 301);
};