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.

feat: Copy publish URLs for current page (#5773)

Oleg Isonen31983180cd9f
diff --git a/apps/builder/app/builder/features/address-bar.tsx b/apps/builder/app/builder/features/address-bar.tsx
index 0939d4c555..5b9ccf6e34 100644
--- a/apps/builder/app/builder/features/address-bar.tsx
+++ b/apps/builder/app/builder/features/address-bar.tsx
@@ -27,15 +27,21 @@ import {
MenuList,
} from "@webstudio-is/design-system";
import { CheckMarkIcon, CopyIcon, DynamicPageIcon } from "@webstudio-is/icons";
-import { $publishedOrigin } from "~/shared/nano-states";
import {
compilePathnamePattern,
isPathnamePattern,
matchPathnamePattern,
tokenizePathnamePattern,
} from "~/builder/shared/url-pattern";
-import { $selectedPage, $selectedPagePath } from "~/shared/nano-states";
+import {
+ $selectedPage,
+ $selectedPagePath,
+ $stagingPassword,
+ $stagingUsername,
+} from "~/shared/nano-states";
import { $currentSystem, updateCurrentSystem } from "~/shared/system";
+import { $project, $publisherHost } from "~/shared/sync/data-stores";
+import { getPublishUrl } from "./publish/publish-url";
const $selectedPageHistory = computed(
$selectedPage,
@@ -76,6 +82,24 @@ const useCopyUrl = (pageUrl: string) => {
};
};
+const usePublishedPageUrl = (pathname: string) => {
+ const project = useStore($project);
+ const publisherHost = useStore($publisherHost);
+ const stagingUsername = useStore($stagingUsername);
+ const stagingPassword = useStore($stagingPassword);
+
+ if (project === undefined) {
+ return "";
+ }
+
+ return getPublishUrl({
+ domain: `${project.domain}.${publisherHost}`,
+ pathname,
+ username: stagingUsername,
+ password: stagingPassword,
+ }).toString();
+};
+
const moveSelection = (menu: HTMLElement, diff: number) => {
const options = Array.from(menu.querySelectorAll("[role=option]"));
const index = options.findIndex((element) => element.ariaSelected === "true");
@@ -233,7 +257,6 @@ const AddressBar = forwardRef<
onSubmit: () => void;
}
>(({ onSubmit }, ref) => {
- const publishedOrigin = useStore($publishedOrigin);
const path = useStore($selectedPagePath);
let history = useStore($selectedPageHistory);
history = useMemo(() => {
@@ -244,9 +267,8 @@ const AddressBar = forwardRef<
);
const tokens = tokenizePathnamePattern(path);
const compiledPath = compilePathnamePattern(tokens, pathParams);
- const { tooltipProps, buttonProps } = useCopyUrl(
- `${publishedOrigin}${compiledPath}`
- );
+ const pageUrl = usePublishedPageUrl(compiledPath);
+ const { tooltipProps, buttonProps } = useCopyUrl(pageUrl);
const errors = new Map<string, string>();
for (const token of tokens) {
@@ -338,8 +360,8 @@ const AddressBar = forwardRef<
export const AddressBarPopover = () => {
const [isOpen, setIsOpen] = useState(false);
const path = useStore($selectedPagePath);
- const publishedOrigin = useStore($publishedOrigin);
- const { tooltipProps, buttonProps } = useCopyUrl(`${publishedOrigin}${path}`);
+ const pageUrl = usePublishedPageUrl(path);
+ const { tooltipProps, buttonProps } = useCopyUrl(pageUrl);
const formRef = useRef<HTMLFormElement>(null);
// show only copy button when path is static
diff --git a/apps/builder/app/builder/features/publish/domains.tsx b/apps/builder/app/builder/features/publish/domains.tsx
index 63c806ad18..cf81f4d73d 100644
--- a/apps/builder/app/builder/features/publish/domains.tsx
+++ b/apps/builder/app/builder/features/publish/domains.tsx
@@ -40,6 +40,8 @@ import { useEffectEvent } from "~/shared/hook-utils/effect-event";
import { DomainCheckbox } from "./domain-checkbox";
import { CopyToClipboard } from "~/shared/copy-to-clipboard";
import { RelativeTime } from "~/builder/shared/relative-time";
+import { $currentSystem } from "~/shared/system";
+import { getPublishUrl } from "./publish-url";
export type Domain = Project["domainsVirtual"][number];
@@ -308,6 +310,11 @@ const DomainItem = ({
});
const publisherHost = useStore($publisherHost);
+ const currentSystem = useStore($currentSystem);
+ const pageUrl = getPublishUrl({
+ domain: projectDomain.domain,
+ pathname: currentSystem.pathname,
+ });
const cname = extractCname(projectDomain.domain);
const dnsRecords = [
{
@@ -349,8 +356,8 @@ const DomainItem = ({
/>
<CopyToClipboard
- text={`https://${projectDomain.domain}`}
- copyText={`Copy link: https://${projectDomain.domain}`}
+ text={pageUrl.toString()}
+ copyText={`Copy link: ${pageUrl.toString()}`}
>
<IconButton type="button" tabIndex={-1}>
<CopyIcon />
diff --git a/apps/builder/app/builder/features/publish/publish-url.test.ts b/apps/builder/app/builder/features/publish/publish-url.test.ts
new file mode 100644
index 0000000000..ef85291001
--- /dev/null
+++ b/apps/builder/app/builder/features/publish/publish-url.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, test } from "vitest";
+import {
+ compilePathnamePattern,
+ tokenizePathnamePattern,
+} from "~/builder/shared/url-pattern";
+import { getPublishUrl } from "./publish-url";
+
+describe("getPublishUrl", () => {
+ test("uses current page pathname with dynamic params", () => {
+ expect(
+ getPublishUrl({
+ domain: "example.wstd.io",
+ pathname: "/blog/my-post",
+ }).toString()
+ ).toBe("https://example.wstd.io/blog/my-post");
+ });
+
+ test("falls back to root when pathname is empty", () => {
+ expect(
+ getPublishUrl({
+ domain: "example.wstd.io",
+ pathname: "",
+ }).toString()
+ ).toBe("https://example.wstd.io/");
+ });
+
+ test("falls back to root when dynamic pathname compiles empty", () => {
+ const pathname = compilePathnamePattern(
+ tokenizePathnamePattern("/:slug?"),
+ {}
+ );
+
+ expect(
+ getPublishUrl({
+ domain: "example.wstd.io",
+ pathname,
+ }).toString()
+ ).toBe("https://example.wstd.io/");
+ });
+
+ test("adds staging credentials to Webstudio-hosted domains", () => {
+ expect(
+ getPublishUrl({
+ domain: "example.wstd.io",
+ pathname: "/blog/my-post",
+ username: "user",
+ password: "pass",
+ }).toString()
+ ).toBe("https://user:[email protected]/blog/my-post");
+ });
+
+ test("does not add credentials when not provided", () => {
+ expect(
+ getPublishUrl({
+ domain: "example.com",
+ pathname: "/blog/my-post",
+ }).toString()
+ ).toBe("https://example.com/blog/my-post");
+ });
+
+ test("accepts domains with an explicit protocol", () => {
+ expect(
+ getPublishUrl({
+ domain: "http://localhost:3000",
+ pathname: "/preview",
+ }).toString()
+ ).toBe("http://localhost:3000/preview");
+ });
+});
diff --git a/apps/builder/app/builder/features/publish/publish-url.ts b/apps/builder/app/builder/features/publish/publish-url.ts
new file mode 100644
index 0000000000..abab53f691
--- /dev/null
+++ b/apps/builder/app/builder/features/publish/publish-url.ts
@@ -0,0 +1,31 @@
+const ensureProtocol = (domain: string) => {
+ try {
+ return new URL(domain);
+ } catch {
+ const url = new URL(`//${domain}`, "https://default.invalid");
+ url.protocol = "https:";
+ return url;
+ }
+};
+
+export const getPublishUrl = ({
+ domain,
+ pathname,
+ password,
+ username,
+}: {
+ domain: string;
+ pathname: string;
+ username?: string;
+ password?: string;
+}) => {
+ const url = ensureProtocol(domain);
+ url.pathname = pathname || "/";
+
+ if (username && password) {
+ url.username = username;
+ url.password = password;
+ }
+
+ return url;
+};
diff --git a/apps/builder/app/builder/features/publish/publish.tsx b/apps/builder/app/builder/features/publish/publish.tsx
index 7b209aa7f1..eeb722f229 100644
--- a/apps/builder/app/builder/features/publish/publish.tsx
+++ b/apps/builder/app/builder/features/publish/publish.tsx
@@ -42,16 +42,12 @@ import {
SmallIconButton,
} from "@webstudio-is/design-system";
import { validateProjectDomain, type Project } from "@webstudio-is/project";
-import {
- $selectedPagePath,
- $selectedInstanceSelector,
-} from "~/shared/nano-states";
+import { $selectedInstanceSelector } from "~/shared/nano-states";
import { findPageAndSelectorByInstanceId } from "~/shared/instance-utils";
import { $selectedPageId } from "~/shared/nano-states";
import {
$authTokenPermissions,
$editingPageId,
- $publishedOrigin,
$permissions,
$stagingUsername,
$stagingPassword,
@@ -87,6 +83,8 @@ import { $openProjectSettings } from "~/shared/nano-states/project-settings";
import { $dataSources, $instances, $pages } from "~/shared/sync/data-stores";
import { RelativeTime } from "~/builder/shared/relative-time";
import cmsUpgradeBanner from "~/shared/cms-upgrade-banner.svg?url";
+import { $currentSystem } from "~/shared/system";
+import { getPublishUrl } from "./publish-url";
type ChangeProjectDomainProps = {
project: Project;
@@ -99,8 +97,7 @@ const ChangeProjectDomain = ({
refresh,
}: ChangeProjectDomainProps) => {
const id = useId();
- const publishedOrigin = useStore($publishedOrigin);
- const selectedPagePath = useStore($selectedPagePath);
+ const currentSystem = useStore($currentSystem);
const stagingUsername = useStore($stagingUsername);
const stagingPassword = useStore($stagingPassword);
const publisherHost = useStore($publisherHost);
@@ -110,18 +107,12 @@ const ChangeProjectDomain = ({
const [isUpdateInProgress, setIsUpdateInProgress] = useOptimistic(false);
const [isUnpublishing, setIsUnpublishing] = useOptimistic(false);
- const pageUrl = new URL(publishedOrigin);
- pageUrl.pathname = selectedPagePath;
-
- // Add username:password@ for staging domains
- if (
- stagingUsername &&
- stagingPassword &&
- pageUrl.hostname.endsWith(`.${publisherHost}`)
- ) {
- pageUrl.username = stagingUsername;
- pageUrl.password = stagingPassword;
- }
+ const pageUrl = getPublishUrl({
+ domain: `${project.domain}.${publisherHost}`,
+ pathname: currentSystem.pathname,
+ username: stagingUsername,
+ password: stagingPassword,
+ });
const updateProjectDomain = async () => {
setIsUpdateInProgress(true);