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 rendering from subdomain

Oleg Isonen65ea5426b667
diff --git a/app/routes/index.tsx b/app/routes/index.tsx
index 12584093ad..f932ec0058 100644
--- a/app/routes/index.tsx
+++ b/app/routes/index.tsx
@@ -1,46 +1,53 @@
import { redirect, useLoaderData } from "remix";
import { type LoaderFunction } from "remix";
-import type { Tree } from "@webstudio-is/sdk";
+import { Document, Root, type Data } from "@webstudio-is/sdk";
import config from "~/config";
import * as preview from "./preview/$projectId";
import * as db from "~/shared/db";
// @todo all this subdomain logic is very hacky
-type LoaderData = { tree: Tree | null; errors?: string };
+type Error = { errors: string };
export const loader: LoaderFunction = async ({ request }) => {
const host =
request.headers.get("x-forwarded-host") ||
request.headers.get("host") ||
"";
- const [subdomain, domain] = host.split(".");
+ const [userDomain, designerDomain] = host.split(".");
// We render the site from a subdomain
- if (subdomain !== "www" && domain !== undefined) {
- const data = {} as LoaderData;
+ if (typeof userDomain === "string" && typeof designerDomain === "string") {
try {
- data.tree = await db.tree.loadByDomain(subdomain);
+ const tree = await db.tree.loadByDomain(userDomain);
+ const props = await db.props.loadByTreeId(tree.id);
+ return { tree, props };
} catch (error) {
if (error instanceof Error) {
- data.errors = error.message;
+ return { errors: error.message };
}
}
- return data;
}
return redirect(config.dashboardPath);
};
const Index = () => {
- const data = useLoaderData();
- if (data.errors) {
+ const data = useLoaderData<Data | Error>();
+ if ("errors" in data) {
return <p>{data.errors}</p>;
}
// We render the site from a subdomain
- if (data.tree) {
- return preview.default();
+ if (data.tree && data.props) {
+ const Outlet = () => <Root data={data} />;
+
+ // @todo This is non-standard for Remix, is there a better way?
+ // We need to render essentially the preview route but from the index,
+ // so we have to know the layout and the outlet from here.
+ // Maybe there is a way to tell remix to use the right outlet somehow and avoid passing it?
+ return <Document Outlet={Outlet} />;
}
+
return null;
};
diff --git a/app/shared/db/props.server.ts b/app/shared/db/props.server.ts
index 2cc28e857b..6923147a76 100644
--- a/app/shared/db/props.server.ts
+++ b/app/shared/db/props.server.ts
@@ -22,6 +22,10 @@ export const loadByProject = async (
throw new Error("Site needs to be published, production tree ID is null.");
}
+ return loadByTreeId(treeId);
+};
+
+export const loadByTreeId = async (treeId: Tree["id"]) => {
return await prisma.instanceProps.findMany({
where: { treeId },
});
diff --git a/package.json b/package.json
index 437de54d10..cbcacaaa2d 100644
--- a/package.json
+++ b/package.json
@@ -42,7 +42,7 @@
"@remix-run/vercel": "^1.2.3",
"@stitches/react": "^1.2.6",
"@stitches/stringify": "^1.2.6",
- "@webstudio-is/sdk": "^0.1.15",
+ "@webstudio-is/sdk": "^0.1.16",
"bson-objectid": "^2.0.2",
"camelcase": "^6.3.0",
"hyphenate-style-name": "^1.0.4",
diff --git a/yarn.lock b/yarn.lock
index 696af4ba3f..cde74f993d 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1820,10 +1820,10 @@
dependencies:
web-streams-polyfill "^3.1.1"
-"@webstudio-is/sdk@^0.1.15":
- version "0.1.15"
- resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.1.15.tgz#ececc87c8b606c872972384294a940141cbe4f19"
- integrity sha512-UgweK3q6HwkyDLSqBVslp2raSn7kgNprCjLyi9XZxZkVs43L95O2bXFIf6+ofmAP5qXyYuYYFr00xziQeas+cw==
+"@webstudio-is/sdk@^0.1.16":
+ version "0.1.16"
+ resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.1.16.tgz#5e2b326162e40f411f3b0b1c74cb2cec85c88734"
+ integrity sha512-lM7NaGwZ0gRD42SaP0rk/AS0SBH0hmgYKOS6VaQjSlTcgDJeWjlYKYwQPznPgkTE83f67Fm2d4kvw+pVsn2iyw==
dependencies:
"@prisma/client" "^3.10.0"
"@stitches/react" "^1.2.7"