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.

fix: Enable greedy redirects (#5801)

Oleg Isonen4d3cca74e2a6
diff --git a/fixtures/react-router-cloudflare/app/redirect-url.ts b/fixtures/react-router-cloudflare/app/redirect-url.ts
new file mode 100644
index 0000000000..1e717eaa73
--- /dev/null
+++ b/fixtures/react-router-cloudflare/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "react-router";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/fixtures/react-router-docker/app/redirect-url.ts b/fixtures/react-router-docker/app/redirect-url.ts
new file mode 100644
index 0000000000..1e717eaa73
--- /dev/null
+++ b/fixtures/react-router-docker/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "react-router";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/fixtures/react-router-netlify/app/redirect-url.ts b/fixtures/react-router-netlify/app/redirect-url.ts
new file mode 100644
index 0000000000..1e717eaa73
--- /dev/null
+++ b/fixtures/react-router-netlify/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "react-router";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/fixtures/react-router-vercel/app/redirect-url.ts b/fixtures/react-router-vercel/app/redirect-url.ts
new file mode 100644
index 0000000000..1e717eaa73
--- /dev/null
+++ b/fixtures/react-router-vercel/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "react-router";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/fixtures/webstudio-cloudflare-template/app/redirect-url.ts b/fixtures/webstudio-cloudflare-template/app/redirect-url.ts
new file mode 100644
index 0000000000..0e51b5c977
--- /dev/null
+++ b/fixtures/webstudio-cloudflare-template/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "@remix-run/react";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/fixtures/webstudio-features/app/redirect-url.ts b/fixtures/webstudio-features/app/redirect-url.ts
new file mode 100644
index 0000000000..1e717eaa73
--- /dev/null
+++ b/fixtures/webstudio-features/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "react-router";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/packages/cli/src/redirect-url.test.ts b/packages/cli/src/redirect-url.test.ts
new file mode 100644
index 0000000000..7335419502
--- /dev/null
+++ b/packages/cli/src/redirect-url.test.ts
@@ -0,0 +1,60 @@
+import { expect, test } from "vitest";
+import { matchPath as matchRemixPath } from "@remix-run/react";
+import { matchPath as matchReactRouterPath } from "react-router";
+import { generateRedirectUrl as generateRemixRedirectUrl } from "../templates/defaults/app/redirect-url";
+import { generateRedirectUrl as generateReactRouterRedirectUrl } from "../templates/react-router/app/redirect-url";
+
+const implementations = [
+ {
+ name: "Remix",
+ generateRedirectUrl: generateRemixRedirectUrl,
+ matchParams: (pattern: string, pathname: string) =>
+ matchRemixPath(pattern, pathname)?.params ?? {},
+ },
+ {
+ name: "React Router",
+ generateRedirectUrl: generateReactRouterRedirectUrl,
+ matchParams: (pattern: string, pathname: string) =>
+ matchReactRouterPath(pattern, pathname)?.params ?? {},
+ },
+] as const;
+
+for (const { name, generateRedirectUrl, matchParams } of implementations) {
+ test(`${name}: generates redirect url for /privat/* to /*`, () => {
+ const params = matchParams("/privat/*", "/privat/foo");
+ expect(params).toEqual({ "*": "foo" });
+
+ expect(generateRedirectUrl("/*", params)).toEqual("/foo");
+ });
+
+ test(`${name}: preserves nested splat params`, () => {
+ const params = matchParams("/privat/*", "/privat/foo/bar");
+ expect(params).toEqual({ "*": "foo/bar" });
+
+ expect(generateRedirectUrl("/*", params)).toEqual("/foo/bar");
+ });
+
+ test(`${name}: generates redirect url from named params`, () => {
+ const params = matchParams("/blog/:slug", "/blog/post");
+ expect(params).toEqual({ slug: "post" });
+
+ expect(generateRedirectUrl("/articles/:slug", params)).toEqual(
+ "/articles/post"
+ );
+ });
+
+ test(`${name}: preserves search and hash`, () => {
+ expect(generateRedirectUrl("/*?next=*#section", { "*": "foo" })).toEqual(
+ "/foo?next=*#section"
+ );
+ });
+
+ test(`${name}: preserves non-local urls`, () => {
+ expect(
+ generateRedirectUrl("https://example.com/*", { "*": "foo" })
+ ).toEqual("https://example.com/*");
+ expect(generateRedirectUrl("//example.com/*", { "*": "foo" })).toEqual(
+ "//example.com/*"
+ );
+ });
+}
diff --git a/packages/cli/templates/defaults/app/redirect-url.ts b/packages/cli/templates/defaults/app/redirect-url.ts
new file mode 100644
index 0000000000..0e51b5c977
--- /dev/null
+++ b/packages/cli/templates/defaults/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "@remix-run/react";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/packages/cli/templates/defaults/app/route-templates/redirect.tsx b/packages/cli/templates/defaults/app/route-templates/redirect.tsx
index 398cff3f91..a65d706868 100644
--- a/packages/cli/templates/defaults/app/route-templates/redirect.tsx
+++ b/packages/cli/templates/defaults/app/route-templates/redirect.tsx
@@ -1,6 +1,7 @@
-import { redirect } from "@remix-run/server-runtime";
+import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
+import { generateRedirectUrl } from "../redirect-url";
import { url, status } from "__REDIRECT__";
-export const loader = () => {
- return redirect(url, status);
+export const loader = (arg: LoaderFunctionArgs) => {
+ return redirect(generateRedirectUrl(url, arg.params), status);
};
diff --git a/packages/cli/templates/react-router/app/redirect-url.ts b/packages/cli/templates/react-router/app/redirect-url.ts
new file mode 100644
index 0000000000..1e717eaa73
--- /dev/null
+++ b/packages/cli/templates/react-router/app/redirect-url.ts
@@ -0,0 +1,21 @@
+import { createPath, generatePath, parsePath } from "react-router";
+
+/**
+ * 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 (url.startsWith("/") === false || url.startsWith("//")) {
+ return url;
+ }
+
+ const path = parsePath(url);
+ return createPath({
+ ...path,
+ pathname: generatePath(path.pathname ?? "/", params),
+ });
+};
diff --git a/packages/cli/templates/react-router/app/route-templates/redirect.tsx b/packages/cli/templates/react-router/app/route-templates/redirect.tsx
index 0e7d00e3b8..5b10d776e0 100644
--- a/packages/cli/templates/react-router/app/route-templates/redirect.tsx
+++ b/packages/cli/templates/react-router/app/route-templates/redirect.tsx
@@ -1,6 +1,7 @@
-import { redirect } from "react-router";
+import { type LoaderFunctionArgs, redirect } from "react-router";
+import { generateRedirectUrl } from "../redirect-url";
import { url, status } from "__REDIRECT__";
-export const loader = () => {
- throw redirect(url, status);
+export const loader = (arg: LoaderFunctionArgs) => {
+ throw redirect(generateRedirectUrl(url, arg.params), status);
};