Files
webstudio/packages/sdk/src/url-pattern.test.ts
T
Oleg Isonen 81d5148046 fix: canonicalize trailing-slash page URLs (#6173)
## Outcome

Canonical page URLs now omit a trailing slash in Webstudio Cloud sites
and exported JavaScript applications. A request such as
`/about/?campaign=launch` receives a permanent `301` redirect to
`/about?campaign=launch`, preventing both URL variants from serving
identical page content.

The behavior is implemented once in the generated root redirect handler,
so it covers fixed and dynamic application routes without generating an
extra route per page. Exact project redirects remain authoritative.
Protocol-relative-looking paths such as `//example.com/` are left
untouched so canonicalization cannot become an external redirect.

Static exports cannot redirect requests themselves. Their URL behavior
remains host-specific and is documented for Cloudflare Pages, Netlify,
Vercel, and other static hosts.

## Implementation

- Add one shared pathname normalizer in the SDK.
- Apply canonicalization after exact authored redirect matching in both
generated application runtimes.
- Ignore repeated leading slashes before constructing a redirect
location.
- Extend redirect-loop detection to model the implicit canonical
redirect.
- Regenerate the affected fixture applications from their templates.
- Document application canonicalization and host-specific static export
behavior.

## Todo

1. [x] Add central trailing-slash canonicalization.
2. [x] Preserve query strings, encoded path segments, and the root path.
3. [x] Cover concrete dynamic URLs without per-route generation.
4. [x] Preserve explicit project redirect precedence.
5. [x] Detect redirect loops that pass through canonicalization.
6. [x] Prevent protocol-relative external redirects.
7. [x] Regenerate and review fixture output.
8. [x] Review documentation impact; document application and SSG
behavior.
9. [x] Run targeted and repository-level verification.

## Verification

- SDK tests: 548 passed
- SDK typecheck: passed
- Project Build tests: 2,379 passed
- Project Build typecheck: passed
- CLI tests: 997 passed; two tests timed out only while three package
suites ran concurrently, then both affected files passed in isolation
(6/6)
- Redirect regression suite after security follow-up: 302 passed
- CLI typecheck: passed
- `pnpm lint`: passed
- `pnpm check:package-boundaries`: passed
- `pnpm fixtures`: passed
- Documentation internal anchors and Markdown whitespace: checked
- New regression tests were confirmed failing before their
implementations and passing afterward
- Agent evaluations were not run because they require separate explicit
approval

Closes #6172
Closes https://github.com/webstudio-is/webstudio/issues/4327
2026-08-18 11:38:27 +02:00

57 lines
2.2 KiB
TypeScript

import { expect, test, describe } from "vitest";
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);
expect(isPathnamePattern("/:slug*")).toEqual(true);
expect(isPathnamePattern("/:id?")).toEqual(true);
expect(isPathnamePattern("/*")).toEqual(true);
expect(isPathnamePattern("")).toEqual(false);
expect(isPathnamePattern("/")).toEqual(false);
expect(isPathnamePattern("/blog")).toEqual(false);
expect(isPathnamePattern("/blog/post-name")).toEqual(false);
});
describe("isAbsoluteUrl", () => {
test("returns true for absolute URLs", () => {
expect(isAbsoluteUrl("https://example.com")).toBe(true);
expect(isAbsoluteUrl("http://example.com")).toBe(true);
expect(isAbsoluteUrl("https://example.com/path")).toBe(true);
expect(isAbsoluteUrl("https://example.com/path?query=1")).toBe(true);
expect(isAbsoluteUrl("https://example.com:8080/path")).toBe(true);
expect(isAbsoluteUrl("ftp://files.example.com")).toBe(true);
expect(isAbsoluteUrl("mailto:test@example.com")).toBe(true);
expect(isAbsoluteUrl("data:text/html,<h1>Hello</h1>")).toBe(true);
});
test("returns false for relative URLs", () => {
expect(isAbsoluteUrl("/path")).toBe(false);
expect(isAbsoluteUrl("/path/to/file")).toBe(false);
expect(isAbsoluteUrl("path/to/file")).toBe(false);
expect(isAbsoluteUrl("./path")).toBe(false);
expect(isAbsoluteUrl("../path")).toBe(false);
expect(isAbsoluteUrl("")).toBe(false);
expect(isAbsoluteUrl("?query=1")).toBe(false);
expect(isAbsoluteUrl("#hash")).toBe(false);
});
test("returns false for invalid URLs", () => {
expect(isAbsoluteUrl("not a url")).toBe(false);
expect(isAbsoluteUrl("https://")).toBe(false);
expect(isAbsoluteUrl("://missing-protocol")).toBe(false);
});
});