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.

provide an env config to the app before any script runs

Oleg Isonenbdeb18b608a1
diff --git a/app/env.server.ts b/app/env.server.ts
new file mode 100644
index 0000000000..170548e349
--- /dev/null
+++ b/app/env.server.ts
@@ -0,0 +1,9 @@
+// Environment variables we want to send to the UI inlined in the document.
+// Never use a private key here, because it will become public.
+const env = {
+ SENTRY_DSN: process.env.SENTRY_DSN,
+} as const;
+
+export default env;
+
+export type Env = typeof env;
diff --git a/app/routes/dashboard.tsx b/app/routes/dashboard.tsx
index d0721b7b36..936c0f8323 100644
--- a/app/routes/dashboard.tsx
+++ b/app/routes/dashboard.tsx
@@ -1,5 +1,10 @@
-import type { MetaFunction } from "@remix-run/node";
+import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import { Designer } from "~/shared/documents/designer";
+import env from "~/env.server";
+
+export const loader: LoaderFunction = () => ({
+ env,
+});
export const meta: MetaFunction = () => {
return { title: "Webstudio Dashboard" };
diff --git a/app/shared/documents/canvas.tsx b/app/shared/documents/canvas.tsx
index c5e4c999d9..084f5ac598 100644
--- a/app/shared/documents/canvas.tsx
+++ b/app/shared/documents/canvas.tsx
@@ -7,6 +7,7 @@ import {
ScrollRestoration,
} from "@remix-run/react";
import { CriticalCss } from "@webstudio-is/sdk";
+import { Env } from "~/shared/env";
/**
* We are using Outlet prop from index layout when user renders site from a subdomain.
@@ -30,6 +31,7 @@ export const Canvas = ({
<body>
<Outlet />
<ScrollRestoration />
+ <Env />
<Scripts />
{process.env.NODE_ENV === "development" && <LiveReload />}
</body>
diff --git a/app/shared/documents/designer.tsx b/app/shared/documents/designer.tsx
index 1c6f5c74ad..46ba3018f1 100644
--- a/app/shared/documents/designer.tsx
+++ b/app/shared/documents/designer.tsx
@@ -1,6 +1,7 @@
import { Links, LiveReload, Meta, Outlet, Scripts } from "@remix-run/react";
import { CriticalCss } from "@webstudio-is/sdk";
import { darkTheme } from "~/shared/design-system";
+import { Env } from "~/shared/env";
export const Designer = () => {
return (
@@ -14,6 +15,7 @@ export const Designer = () => {
</head>
<body className={darkTheme}>
<Outlet />
+ <Env />
<Scripts />
{process.env.NODE_ENV === "development" && <LiveReload />}
</body>
diff --git a/app/shared/env/env-getter.ts b/app/shared/env/env-getter.ts
new file mode 100644
index 0000000000..0bdbdf1045
--- /dev/null
+++ b/app/shared/env/env-getter.ts
@@ -0,0 +1,16 @@
+import { namespace } from "./namespace";
+import type { Env } from "~/env.server";
+
+// @todo remove once remix has a built-in way
+// https://github.com/remix-run/remix/discussions/2769
+// This provides an access to a config over window by accessing a property in an env proxy object
+export default new Proxy(
+ {},
+ {
+ get(_target, prop) {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ const env = (window[namespace as any] ?? {}) as unknown as Env;
+ return prop in env ? env[prop as keyof Env] : undefined;
+ },
+ }
+) as Env;
diff --git a/app/shared/env/env.tsx b/app/shared/env/env.tsx
new file mode 100644
index 0000000000..954efc81eb
--- /dev/null
+++ b/app/shared/env/env.tsx
@@ -0,0 +1,16 @@
+import { useLoaderData } from "@remix-run/react";
+import { namespace } from "./namespace";
+
+// @todo remove once remix has a built-in way
+// https://github.com/remix-run/remix/discussions/2769
+// This renders env config as a script tag so we can have access to it before any scripts run
+export const Env = () => {
+ const data = useLoaderData();
+ return (
+ <script
+ dangerouslySetInnerHTML={{
+ __html: `window["${namespace}"] = ${JSON.stringify(data.env)}`,
+ }}
+ />
+ );
+};
diff --git a/app/shared/env/index.ts b/app/shared/env/index.ts
new file mode 100644
index 0000000000..05a06cf9a7
--- /dev/null
+++ b/app/shared/env/index.ts
@@ -0,0 +1,3 @@
+export * from "./namespace";
+export * from "./env";
+export { default } from "./env-getter";
diff --git a/app/shared/env/namespace.ts b/app/shared/env/namespace.ts
new file mode 100644
index 0000000000..7fe5123858
--- /dev/null
+++ b/app/shared/env/namespace.ts
@@ -0,0 +1,2 @@
+// Global namespace on window.
+export const namespace = "__webstudioEnv";
diff --git a/app/shared/sentry.ts b/app/shared/sentry.ts
index 16e94ba8e9..76074712b4 100644
--- a/app/shared/sentry.ts
+++ b/app/shared/sentry.ts
@@ -1,17 +1,17 @@
import * as Sentry from "@sentry/browser";
import { Extras } from "@sentry/types";
-
import { BrowserTracing } from "@sentry/tracing";
+import env from "~/shared/env";
-export const initSentry = () =>
+export const initSentry = () => {
Sentry.init({
- dsn: "https://[email protected]/6530661",
+ dsn: env.SENTRY_DSN,
integrations: [new BrowserTracing()],
tracesSampleRate: 1.0,
environment:
process.env.NODE_ENV === "development" ? "development" : "production",
});
-
+};
type SentryHelperProps = {
message: string;
extra?: Extras;
diff --git a/package.json b/package.json
index b1d5a0478d..04d90d809b 100644
--- a/package.json
+++ b/package.json
@@ -128,7 +128,7 @@
"prettier": "^2.6.2",
"prisma": "^3.14.0",
"relative-deps": "^1.0.7",
- "typescript": "^4.1.2"
+ "typescript": "4.5.5"
},
"engines": {
"node": ">=14 <=16"
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 926f331193..c7a5edb888 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -10,6 +10,12 @@ datasource db {
url = env("DATABASE_URL")
}
+model Team {
+ id String @id @default(uuid())
+ users User[]
+
+}
+
model User {
id String @id @default(uuid())
email String? @unique
@@ -17,18 +23,20 @@ model User {
image String?
username String?
createdAt DateTime @default(now())
+ Team Team? @relation(fields: [teamId], references: [id])
+ teamId String?
projects Project[]
}
model Project {
id String @id @default(uuid())
- userId String
title String
domain String @unique
prodTreeId String? // exists when published
devTreeId String
prodTreeIdHistory String @default("[]")
User User? @relation(fields: [userId], references: [id])
+ userId String?
}
model Tree {
diff --git a/yarn.lock b/yarn.lock
index aa1be86556..c17822aa42 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -14434,7 +14434,7 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
-typescript@^4.1.2:
version "4.5.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==