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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: canonicalize trailing-slash page URLs (#6173)
Oleg Isonen81d5148046a9
diff --git a/docs/university/foundations/page-settings.md b/docs/university/foundations/page-settings.mdindex b73b65aa03..8e77b64aca 100644--- a/docs/university/foundations/page-settings.md+++ b/docs/university/foundations/page-settings.md@@ -61,6 +61,11 @@ Path rules:- Wildcards such as `*` and `:path*` must be the final segment- Parameter names can contain letters, numbers, and underscores+Webstudio Cloud sites and exported JavaScript applications permanently redirect+a trailing-slash URL such as `/about/` to the page path `/about` and preserve+its query string. For static exports, the hosting platform controls+[trailing-slash behavior](../self-hosting/#static-site-url-behavior).+## Status codeThe HTTP status code returned when this page is requested. Defaults to `200`.diff --git a/docs/university/self-hosting/README.md b/docs/university/self-hosting/README.mdindex 7a3727f2b8..3c455d696c 100644--- a/docs/university/self-hosting/README.md+++ b/docs/university/self-hosting/README.md@@ -111,6 +111,24 @@ While static site exporting and hosting are less technical, this comes at the co* No robots.txt* No sitemap.xml+#### Static site URL behavior++Webstudio static exports cannot redirect requests by themselves. The hosting+platform decides whether `/about`, `/about/`, or both URLs serve a generated+page such as `about/index.html`.++| Platform | Default behavior for a Webstudio static export |+| --- | --- |+| Cloudflare Pages | Redirects `/about` to `/about/` for a generated `about/index.html` file. See [Cloudflare Pages serving behavior](https://developers.cloudflare.com/pages/configuration/serving-pages/). |+| Netlify | Pretty URLs are enabled by default and normalize the generated page to `/about/`. Netlify applies this normalization before redirect rules, so a static redirect rule cannot change it to `/about`. See [Netlify redirect options](https://docs.netlify.com/manage/routing/redirects/redirect-options/). |+| Vercel | Serves both `/about` and `/about/` unless the project sets `trailingSlash`. Set `"trailingSlash": false` in `vercel.json` to redirect `/about/` to `/about`. See [Vercel project configuration](https://vercel.com/docs/project-configuration/vercel-json#trailingslash). |+| Other static hosts | Behavior depends on how the host resolves directory index files and normalizes URLs. Configure redirects or canonical URLs with the host. |++If every URL must redirect to the slashless Webstudio page path, use an+exported JavaScript application. On static hosting, enforcing that convention+on Netlify or Cloudflare Pages requires request-handling code at the edge rather+than static redirect files.+#### LocalTo run a project locally, you must run a simple local server. Use the command `npx serve .` to spin one up. This is required because the static files use absolute URLs.diff --git a/docs/university/self-hosting/cloudflare-pages.md b/docs/university/self-hosting/cloudflare-pages.mdindex d2b0fdf7d4..2c1b0f4790 100644--- a/docs/university/self-hosting/cloudflare-pages.md+++ b/docs/university/self-hosting/cloudflare-pages.md@@ -16,6 +16,10 @@ See [export types](./#export-types) for more information about JavaScript applicLearn how to upload your static site to Cloudflare Pages.+Cloudflare Pages uses a trailing slash for generated directory index pages.+Review the [static site URL behavior](./#static-site-url-behavior) before+deployment.+<figure><img src="../../.gitbook/assets/cloudflare-pages-new-project.png" alt="Cloudflare Pages new project dashboard"><figcaption></figcaption></figure>### Prerequisitesdiff --git a/docs/university/self-hosting/netlify.md b/docs/university/self-hosting/netlify.mdindex cfdff0657d..9b56607cab 100644--- a/docs/university/self-hosting/netlify.md+++ b/docs/university/self-hosting/netlify.md@@ -84,6 +84,9 @@ When you make changes in Webstudio:Learn how to upload your static site to Netlify.+Netlify uses a trailing slash for generated directory index pages. Review the+[static site URL behavior](./#static-site-url-behavior) before deployment.+<figure><img src="../../.gitbook/assets/netlify-new-project.png" alt="netlify new site dashboard"><figcaption></figcaption></figure>### Prerequisitesdiff --git a/docs/university/self-hosting/vercel.md b/docs/university/self-hosting/vercel.mdindex c0adbaf3d3..256eb1bf00 100644--- a/docs/university/self-hosting/vercel.md+++ b/docs/university/self-hosting/vercel.md@@ -41,6 +41,10 @@ Vercel injects a few [files](https://github.com/vercel/vercel/blob/a8ad176262ef8Learn how to upload your static site to Vercel.+Vercel serves both forms of a path by default, but can redirect to slashless+paths with project configuration. Review the+[static site URL behavior](./#static-site-url-behavior) before deployment.+<figure><img src="../../.gitbook/assets/vercel-new-project.png" alt="Vercel add new project dashboard"><figcaption></figcaption></figure>### Prerequisitesdiff --git a/fixtures/react-router-cloudflare/app/redirect-url.ts b/fixtures/react-router-cloudflare/app/redirect-url.tsindex 0e9e45efde..dbe78d060c 100644--- a/fixtures/react-router-cloudflare/app/redirect-url.ts+++ b/fixtures/react-router-cloudflare/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,redirect,} from "react-router";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/fixtures/react-router-docker/app/redirect-url.ts b/fixtures/react-router-docker/app/redirect-url.tsindex 0e9e45efde..dbe78d060c 100644--- a/fixtures/react-router-docker/app/redirect-url.ts+++ b/fixtures/react-router-docker/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,redirect,} from "react-router";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/fixtures/react-router-netlify/app/redirect-url.ts b/fixtures/react-router-netlify/app/redirect-url.tsindex 0e9e45efde..dbe78d060c 100644--- a/fixtures/react-router-netlify/app/redirect-url.ts+++ b/fixtures/react-router-netlify/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,redirect,} from "react-router";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/fixtures/react-router-vercel/app/redirect-url.ts b/fixtures/react-router-vercel/app/redirect-url.tsindex 0e9e45efde..dbe78d060c 100644--- a/fixtures/react-router-vercel/app/redirect-url.ts+++ b/fixtures/react-router-vercel/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,redirect,} from "react-router";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/fixtures/webstudio-cloudflare-template/app/redirect-url.ts b/fixtures/webstudio-cloudflare-template/app/redirect-url.tsindex 8e13c608e9..a428e93828 100644--- a/fixtures/webstudio-cloudflare-template/app/redirect-url.ts+++ b/fixtures/webstudio-cloudflare-template/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,} from "@remix-run/react";import { redirect } from "@remix-run/server-runtime";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/fixtures/webstudio-features/app/redirect-url.ts b/fixtures/webstudio-features/app/redirect-url.tsindex 0e9e45efde..dbe78d060c 100644--- a/fixtures/webstudio-features/app/redirect-url.ts+++ b/fixtures/webstudio-features/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,redirect,} from "react-router";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/packages/cli/src/redirect-url.test.ts b/packages/cli/src/redirect-url.test.tsindex 67757e6b15..0104a28d5b 100644--- a/packages/cli/src/redirect-url.test.ts+++ b/packages/cli/src/redirect-url.test.ts@@ -529,11 +529,6 @@ for (const {requestPath: "/old/nested",redirects: [{ old: "/old", new: "/new" }],},- {- name: "does not normalize trailing slash",- requestPath: "/old/",- redirects: [{ old: "/old", new: "/new" }],- },{name: "matches paths case-sensitively",requestPath: "/path",@@ -615,6 +610,68 @@ for (const {expect(matchRedirect(url(requestPath), redirects)).toBeUndefined();});+ test("does not use a project redirect for its trailing-slash variant", () => {+ expect(+ matchRedirect(url("/old/"), [{ old: "/old", new: "/new" }])+ ).toBeUndefined();+ });++ test.each([+ {+ name: "normalizes a static path",+ requestPath: "/about/",+ location: "/about",+ },+ {+ name: "normalizes a concrete dynamic path",+ requestPath: "/blog/post/",+ location: "/blog/post",+ },+ {+ name: "preserves the query string",+ requestPath: "/about/?campaign=launch",+ location: "/about?campaign=launch",+ },+ {+ name: "removes repeated trailing slashes",+ requestPath: "/about///",+ location: "/about",+ },+ {+ name: "preserves encoded path segments",+ requestPath: "/file%2Fname/",+ location: "/file%2Fname",+ },+ ])(+ "returns canonical redirect response: $name",+ ({ requestPath, location }) => {+ const response = redirectRequest(new Request(url(requestPath)), []);+ expect(response?.status).toBe(301);+ expect(response?.headers.get("Location")).toBe(location);+ }+ );++ test("does not redirect the root path", () => {+ expect(redirectRequest(new Request(url("/")), [])).toBeUndefined();+ });++ test.each(["//other.example/", "///other.example/"])(+ "does not create an external redirect from %s",+ (requestPath) => {+ expect(+ redirectRequest(new Request(url(requestPath)), [])+ ).toBeUndefined();+ }+ );++ test("gives an exact project redirect precedence", () => {+ const response = redirectRequest(new Request(url("/old/")), [+ { old: "/old/", new: "/new" },+ ]);+ expect(response?.status).toBe(301);+ expect(response?.headers.get("Location")).toBe("/new");+ });+test.each(nonMatchingCases)("does not return router redirect response: $name",({ requestPath, redirects }) => {diff --git a/packages/cli/templates/defaults/app/redirect-url.ts b/packages/cli/templates/defaults/app/redirect-url.tsindex 8e13c608e9..a428e93828 100644--- a/packages/cli/templates/defaults/app/redirect-url.ts+++ b/packages/cli/templates/defaults/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,} from "@remix-run/react";import { redirect } from "@remix-run/server-runtime";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/packages/cli/templates/react-router/app/redirect-url.ts b/packages/cli/templates/react-router/app/redirect-url.tsindex 0e9e45efde..dbe78d060c 100644--- a/packages/cli/templates/react-router/app/redirect-url.ts+++ b/packages/cli/templates/react-router/app/redirect-url.ts@@ -5,6 +5,7 @@ import {parsePath,redirect,} from "react-router";+import { removeTrailingSlash } from "@webstudio-is/sdk";type RedirectItem = {old: string;@@ -160,8 +161,19 @@ export const redirectRequest = (redirects: RedirectItem[]): Response | undefined => {const matchedRedirect = matchRedirect(request.url, redirects);- if (matchedRedirect === undefined) {+ 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(matchedRedirect.url, matchedRedirect.status);++ return redirect(`${pathname}${url.search}`, 301);};diff --git a/packages/project-build/src/runtime/redirect-loop-detection.test.ts b/packages/project-build/src/runtime/redirect-loop-detection.test.tsindex 5514ccab0e..c8a2dd03d2 100644--- a/packages/project-build/src/runtime/redirect-loop-detection.test.ts+++ b/packages/project-build/src/runtime/redirect-loop-detection.test.ts@@ -8,6 +8,10 @@ describe("wouldCreateLoop", () => {expect(wouldCreateLoop("/about", "/about", [])).toBe(true);});+ test("returns true for self-redirect through trailing-slash normalization", () => {+ expect(wouldCreateLoop("/about", "/about/", [])).toBe(true);+ });+test("returns true for self-redirect with existing redirects", () => {const existing: PageRedirect[] = [{ old: "/other", new: "/page", status: "301" },@@ -69,6 +73,13 @@ describe("wouldCreateLoop", () => {expect(wouldCreateLoop("/a", "/b", existing)).toBe(true);});+ test("returns true when targets loop through trailing-slash normalization", () => {+ const existing: PageRedirect[] = [+ { old: "/b", new: "/a/", status: "301" },+ ];+ expect(wouldCreateLoop("/a", "/b/", existing)).toBe(true);+ });+test("returns false when no loop exists", () => {const existing: PageRedirect[] = [{ old: "/b", new: "/c", status: "301" },diff --git a/packages/project-build/src/runtime/redirect-loop-detection.ts b/packages/project-build/src/runtime/redirect-loop-detection.tsindex 3159372fdb..aaa4f50e32 100644--- a/packages/project-build/src/runtime/redirect-loop-detection.ts+++ b/packages/project-build/src/runtime/redirect-loop-detection.ts@@ -1,6 +1,7 @@-import type { PageRedirect } from "@webstudio-is/sdk";+import { removeTrailingSlash, type PageRedirect } from "@webstudio-is/sdk";import {doesRedirectSourceMatchLocalUrl,+ getRedirectSourceSearchIndex,getRedirectSourcePathname,normalizeRedirectSource,} from "./redirect-source";@@ -31,6 +32,13 @@ const resolveLocalTarget = (source: string, target: string) => {}};+const normalizeTrailingSlash = (url: string) => {+ const searchIndex = getRedirectSourceSearchIndex(url);+ const pathname = searchIndex === -1 ? url : url.slice(0, searchIndex);+ const search = searchIndex === -1 ? "" : url.slice(searchIndex);+ return `${removeTrailingSlash(pathname)}${search}`;+};+export const wouldCreateLoop = (fromPath: string,toPath: string,@@ -77,7 +85,10 @@ export const wouldCreateLoop = (}}if (next === undefined) {- return false;+ next = normalizeTrailingSlash(current);+ if (next === current) {+ return false;+ }}current = next;diff --git a/packages/sdk/src/url-pattern.test.ts b/packages/sdk/src/url-pattern.test.tsindex baeb60cbf8..209340f37b 100644--- a/packages/sdk/src/url-pattern.test.ts+++ b/packages/sdk/src/url-pattern.test.ts@@ -1,5 +1,17 @@import { expect, test, describe } from "vitest";-import { isPathnamePattern, isAbsoluteUrl } from "./url-pattern";+import {+ isPathnamePattern,+ isAbsoluteUrl,+ removeTrailingSlash,+} from "./url-pattern";++test("removes trailing slashes without changing the root path", () => {+ expect(removeTrailingSlash("/")).toBe("/");+ expect(removeTrailingSlash("/about")).toBe("/about");+ expect(removeTrailingSlash("/about/")).toBe("/about");+ expect(removeTrailingSlash("/about///")).toBe("/about");+ expect(removeTrailingSlash("/file%2Fname/")).toBe("/file%2Fname");+});test("check pathname is pattern", () => {expect(isPathnamePattern("/:name")).toEqual(true);diff --git a/packages/sdk/src/url-pattern.ts b/packages/sdk/src/url-pattern.tsindex 0ed69f46b9..946a3ba9ee 100644--- a/packages/sdk/src/url-pattern.ts+++ b/packages/sdk/src/url-pattern.ts@@ -14,6 +14,14 @@ export const matchPathnameParams = (pathname: string) => {return pathname.matchAll(tokenRegexGlobal);};+export const removeTrailingSlash = (pathname: string) => {+ let end = pathname.length;+ while (end > 1 && pathname[end - 1] === "/") {+ end -= 1;+ }+ return pathname.slice(0, end);+};+/*** Check if a string is an absolute URL (has a valid protocol)*/