Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
} from "@webstudio-is/sdk";
import { useRef, useState, type ChangeEvent } from "react";
import { flushSync } from "react-dom";
import { matchPathnamePattern } from "~/builder/shared/url-pattern";
import { $pages, $publishedOrigin } from "~/shared/nano-states";
import { serverSyncStore } from "~/shared/sync";
import { getExistingRoutePaths, sectionSpacing } from "./utils";
Expand Down Expand Up @@ -82,32 +81,9 @@ export const SectionRedirects = () => {

const validateNewPath = (newPath: string): string[] => {
const newPathValidationResult = ProjectNewRedirectPath.safeParse(newPath);

if (newPathValidationResult.success === true) {
/*
This is the new path, that users want to redirect to.
If the new path doesn't exist, it's not a valid redirect.
*/

if (newPath === "/") {
return [];
}

if (newPath.startsWith("/") === true) {
let matched = false;
for (const pattern of existingPaths) {
if (matchPathnamePattern(pattern, newPath)) {
matched = true;
}
}
if (matched === false) {
return ["This path doesn't exist in the project"];
}
}

if (newPathValidationResult.success) {
return [];
}

return newPathValidationResult.error.format()._errors;
};

Expand Down
25 changes: 10 additions & 15 deletions packages/sdk/src/schema/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,16 @@ const ProjectMeta = z.object({
});
export type ProjectMeta = z.infer<typeof ProjectMeta>;

export const ProjectNewRedirectPath = PagePath.or(
z.string().refine((data) => {
// Users should be able to redirect from any old-path to the home page in the new project.
if (data === "/") {
return true;
}

try {
new URL(data);
return true;
} catch {
return false;
}
}, "Must be a valid URL")
);
export const ProjectNewRedirectPath = z.string().refine((data) => {
// Users should be able to redirect from any old-path to the home page in the new project.
try {
// can be relative and absolute paths
new URL(data, "http://url.com");
return true;
} catch {
return false;
}
}, "Must be a valid URL");

export const PageRedirect = z.object({
old: OldPagePath,
Expand Down
Loading