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: improve generating resources (#2715)

Bogdan Chadkinf0b7a418243e
diff --git a/fixtures/webstudio-custom-template/app/__generated__/_index.tsx b/fixtures/webstudio-custom-template/app/__generated__/_index.tsx
index ee554a744e..78a527d95e 100644
--- a/fixtures/webstudio-custom-template/app/__generated__/_index.tsx
+++ b/fixtures/webstudio-custom-template/app/__generated__/_index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import { Body as Body } from "@webstudio-is/sdk-components-react-remix";
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.tsx
index be79ae5fdf..36222cc414 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import { Body as Body } from "@webstudio-is/sdk-components-react-remix";
diff --git a/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.tsx b/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.tsx
index be79ae5fdf..36222cc414 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import { Body as Body } from "@webstudio-is/sdk-components-react-remix";
diff --git a/fixtures/webstudio-remix-vercel/.webstudio/data.json b/fixtures/webstudio-remix-vercel/.webstudio/data.json
index 87a0d4a819..ba9f9425fa 100644
--- a/fixtures/webstudio-remix-vercel/.webstudio/data.json
+++ b/fixtures/webstudio-remix-vercel/.webstudio/data.json
@@ -1,10 +1,10 @@
{
"build": {
- "id": "583e33b6-d105-45b8-ad11-a763f14760d4",
+ "id": "c8c21b00-1c66-4fb1-903d-5fd207fe24a2",
"projectId": "cddc1d44-af37-4cb6-a430-d300cf6f932d",
- "version": 222,
- "createdAt": "2023-11-03T13:22:34.879Z",
- "updatedAt": "2023-11-10T17:10:50.248Z",
+ "version": 228,
+ "createdAt": "2023-12-26T09:13:01.469Z",
+ "updatedAt": "2023-12-26T09:13:01.469Z",
"pages": {
"meta": {
"siteName": "KittyGuardedZone",
@@ -70,6 +70,24 @@
},
"rootInstanceId": "O-ljaGZQ0iRNTlEshMkgE",
"path": "/heading-with-id"
+ },
+ {
+ "id": "TS39WeBd0Qr3QgbSlzdf2",
+ "name": "resources",
+ "title": "resources",
+ "meta": {
+ "description": "",
+ "excludePageFromSearch": false,
+ "socialImageAssetId": "",
+ "custom": [
+ {
+ "property": "",
+ "content": ""
+ }
+ ]
+ },
+ "rootInstanceId": "AWY2qZfpbykoiWELeJhse",
+ "path": "/resources"
}
]
},
@@ -2073,6 +2091,36 @@
"instanceId": "qmxnOlSxUGpuhuonVArWJ"
}
}
+ ],
+ [
+ "QlPyQWR-xy_0Lw2-akDvI",
+ {
+ "id": "QlPyQWR-xy_0Lw2-akDvI",
+ "instanceId": "WGSZGPAhyIuWhM1DJ7HbZ",
+ "name": "data",
+ "type": "expression",
+ "value": "$ws$dataSource$PgXFOzhK1YDdnv2BuvlwR.data"
+ }
+ ],
+ [
+ "HRyG-UjWjdupsAVbyRc-H",
+ {
+ "id": "HRyG-UjWjdupsAVbyRc-H",
+ "instanceId": "WGSZGPAhyIuWhM1DJ7HbZ",
+ "name": "item",
+ "type": "parameter",
+ "value": "MShox7Ngja3P8_t6XGMBw"
+ }
+ ],
+ [
+ "CHRYq1SSaV_uPOgkM6Cv1",
+ {
+ "id": "CHRYq1SSaV_uPOgkM6Cv1",
+ "instanceId": "05oK4Ks0ocFv3w8MJOcNR",
+ "name": "code",
+ "type": "expression",
+ "value": "$ws$dataSource$MShox7Ngja3P8_t6XGMBw.name"
+ }
]
],
"dataSources": [
@@ -2114,9 +2162,40 @@
"value": "initial"
}
}
+ ],
+ [
+ "PgXFOzhK1YDdnv2BuvlwR",
+ {
+ "type": "resource",
+ "id": "PgXFOzhK1YDdnv2BuvlwR",
+ "scopeInstanceId": "AWY2qZfpbykoiWELeJhse",
+ "name": "list",
+ "resourceId": "1vX6SQdaCjJN6MvJlG_cQ"
+ }
+ ],
+ [
+ "MShox7Ngja3P8_t6XGMBw",
+ {
+ "type": "parameter",
+ "id": "MShox7Ngja3P8_t6XGMBw",
+ "scopeInstanceId": "WGSZGPAhyIuWhM1DJ7HbZ",
+ "name": "collectionItem"
+ }
+ ]
+ ],
+ "resources": [
+ [
+ "1vX6SQdaCjJN6MvJlG_cQ",
+ {
+ "id": "1vX6SQdaCjJN6MvJlG_cQ",
+ "name": "list",
+ "method": "get",
+ "url": "\"https://gist.githubusercontent.com/TrySound/56507c301ec85669db5f1541406a9259/raw/a49548730ab592c86b9e7781f5b29beec4765494/collection.json\"",
+ "headers": [],
+ "body": ""
+ }
]
],
- "resources": [],
"instances": [
[
"On9cvWCxr5rdZtY9O1Bv0",
@@ -3078,6 +3157,57 @@
}
]
}
+ ],
+ [
+ "AWY2qZfpbykoiWELeJhse",
+ {
+ "type": "instance",
+ "id": "AWY2qZfpbykoiWELeJhse",
+ "component": "Body",
+ "children": [
+ {
+ "type": "id",
+ "value": "WGSZGPAhyIuWhM1DJ7HbZ"
+ }
+ ]
+ }
+ ],
+ [
+ "WGSZGPAhyIuWhM1DJ7HbZ",
+ {
+ "type": "instance",
+ "id": "WGSZGPAhyIuWhM1DJ7HbZ",
+ "component": "ws:collection",
+ "children": [
+ {
+ "type": "id",
+ "value": "-F-b3eIEZ8WKW_F-Aw8nN"
+ }
+ ]
+ }
+ ],
+ [
+ "-F-b3eIEZ8WKW_F-Aw8nN",
+ {
+ "type": "instance",
+ "id": "-F-b3eIEZ8WKW_F-Aw8nN",
+ "component": "Box",
+ "children": [
+ {
+ "type": "id",
+ "value": "05oK4Ks0ocFv3w8MJOcNR"
+ }
+ ]
+ }
+ ],
+ [
+ "05oK4Ks0ocFv3w8MJOcNR",
+ {
+ "type": "instance",
+ "id": "05oK4Ks0ocFv3w8MJOcNR",
+ "component": "HtmlEmbed",
+ "children": []
+ }
]
],
"deployment": {
@@ -3161,6 +3291,24 @@
},
"rootInstanceId": "O-ljaGZQ0iRNTlEshMkgE",
"path": "/heading-with-id"
+ },
+ {
+ "id": "TS39WeBd0Qr3QgbSlzdf2",
+ "name": "resources",
+ "title": "resources",
+ "meta": {
+ "description": "",
+ "excludePageFromSearch": false,
+ "socialImageAssetId": "",
+ "custom": [
+ {
+ "property": "",
+ "content": ""
+ }
+ ]
+ },
+ "rootInstanceId": "AWY2qZfpbykoiWELeJhse",
+ "path": "/resources"
}
],
"assets": [
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx
index 88eedde4ef..325aac06f0 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import { Body as Body } from "@webstudio-is/sdk-components-react-remix";
@@ -89,6 +89,7 @@ export const pagesPaths = new Set([
"/_route_with_symbols_",
"/form",
"/heading-with-id",
+ "/resources",
]);
export const formsProperties = new Map<
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx
index 094db8f859..b98205708a 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import {
@@ -205,6 +205,7 @@ export const pagesPaths = new Set([
"/_route_with_symbols_",
"/form",
"/heading-with-id",
+ "/resources",
]);
export const formsProperties = new Map<
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.tsx
index 4d099eb887..4955f2d4c7 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import { Body as Body } from "@webstudio-is/sdk-components-react-remix";
@@ -91,6 +91,7 @@ export const pagesPaths = new Set([
"/_route_with_symbols_",
"/form",
"/heading-with-id",
+ "/resources",
]);
export const formsProperties = new Map<
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx
index 3b37029319..6bf21da0c7 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import { Body as Body } from "@webstudio-is/sdk-components-react-remix";
@@ -219,6 +219,7 @@ export const pagesPaths = new Set([
"/_route_with_symbols_",
"/form",
"/heading-with-id",
+ "/resources",
]);
export const formsProperties = new Map<
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.server.tsx
new file mode 100644
index 0000000000..51103cf0d3
--- /dev/null
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.server.tsx
@@ -0,0 +1,16 @@
+import { loadResource } from "@webstudio-is/sdk";
+type Params = Record<string, string | undefined>;
+export const loadResources = async (_props: { params: Params }) => {
+ const [list_1] = await Promise.all([
+ loadResource({
+ id: "1vX6SQdaCjJN6MvJlG_cQ",
+ name: "list",
+ url: "https://gist.githubusercontent.com/TrySound/56507c301ec85669db5f1541406a9259/raw/a49548730ab592c86b9e7781f5b29beec4765494/collection.json",
+ method: "get",
+ headers: [],
+ }),
+ ]);
+ return {
+ list_1,
+ } as Record<string, unknown>;
+};
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.tsx
new file mode 100644
index 0000000000..f4d46d1ed2
--- /dev/null
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.tsx
@@ -0,0 +1,113 @@
+/* eslint-disable */
+/* This is a auto generated file for building the project */
+
+import { Fragment, useState } from "react";
+import type { PageData } from "~/routes/_index";
+import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
+import { Body as Body } from "@webstudio-is/sdk-components-react-remix";
+import {
+ Box as Box,
+ HtmlEmbed as HtmlEmbed,
+} from "@webstudio-is/sdk-components-react";
+
+export const fontAssets: Asset[] = [];
+export const imageAssets: ImageAsset[] = [
+ {
+ id: "88d5e2ff-b8f2-4899-aaf8-dde4ade6da10",
+ name: "DALL_E_2023-10-30_12.39.46_-_Photo_logo_with_a_bold_cat_silhouette_centered_on_a_contrasting_background_designed_for_clarity_at_small_32x32_favicon_resolution_00h6cEA8u2pJRvVJv7hRe.png",
+ description: null,
+ projectId: "cddc1d44-af37-4cb6-a430-d300cf6f932d",
+ size: 268326,
+ type: "image",
+ format: "png",
+ createdAt: "2023-10-30T13:51:08.416Z",
+ meta: { width: 790, height: 786 },
+ },
+ {
+ id: "9a8bc926-7804-4d3f-af81-69196b1d2ed8",
+ name: "small-avif-kitty_FnabJsioMWpBtXZSGf4DR.webp",
+ description: null,
+ projectId: "cddc1d44-af37-4cb6-a430-d300cf6f932d",
+ size: 2906,
+ type: "image",
+ format: "webp",
+ createdAt: "2023-09-12T09:44:22.120Z",
+ meta: { width: 100, height: 100 },
+ },
+ {
+ id: "cd939c56-bcdd-4e64-bd9c-567a9bccd3da",
+ name: "_937084ed-a798-49fe-8664-df93a2af605e_uiBk3o6UWdqolyakMvQJ9.jpeg",
+ description: null,
+ projectId: "cddc1d44-af37-4cb6-a430-d300cf6f932d",
+ size: 210614,
+ type: "image",
+ format: "jpeg",
+ createdAt: "2023-09-06T11:28:43.031Z",
+ meta: { width: 1024, height: 1024 },
+ },
+];
+export const pageData: PageData = {
+ site: {
+ siteName: "KittyGuardedZone",
+ faviconAssetId: "88d5e2ff-b8f2-4899-aaf8-dde4ade6da10",
+ code: "<script>console.log('KittyGuardedZone')</script>\n",
+ },
+ page: {
+ id: "TS39WeBd0Qr3QgbSlzdf2",
+ name: "resources",
+ title: "resources",
+ meta: {
+ description: "",
+ excludePageFromSearch: false,
+ socialImageAssetId: "",
+ custom: [{ property: "", content: "" }],
+ },
+ rootInstanceId: "AWY2qZfpbykoiWELeJhse",
+ path: "/resources",
+ },
+};
+export const user: { email: string | null } | undefined = {
+ email: "[email protected]",
+};
+export const projectId = "cddc1d44-af37-4cb6-a430-d300cf6f932d";
+
+type Params = Record<string, string | undefined>;
+type Resources = Record<string, unknown>;
+const Page = (_props: { params: Params; resources: Resources }) => {
+ let list: any = _props.resources["list_1"];
+ return (
+ <Body data-ws-id="AWY2qZfpbykoiWELeJhse" data-ws-component="Body">
+ {list?.data?.map((collectionItem: any, index: number) => (
+ <Fragment key={index}>
+ <Box data-ws-id="-F-b3eIEZ8WKW_F-Aw8nN" data-ws-component="Box">
+ <HtmlEmbed
+ data-ws-id="05oK4Ks0ocFv3w8MJOcNR"
+ data-ws-component="HtmlEmbed"
+ code={collectionItem?.name}
+ />
+ </Box>
+ </Fragment>
+ ))}
+ </Body>
+ );
+};
+
+export { Page };
+
+export const getRemixParams = ({ ...params }: Params): Params => {
+ return params;
+};
+
+export const pagesPaths = new Set([
+ "",
+ "/radix",
+ "/_route_with_symbols_",
+ "/form",
+ "/heading-with-id",
+ "/resources",
+]);
+
+export const formsProperties = new Map<
+ string,
+ { method?: string; action?: string }
+>([]);
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[sitemap.xml].ts b/fixtures/webstudio-remix-vercel/app/__generated__/[sitemap.xml].ts
index 047c22ff6c..52480146d3 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[sitemap.xml].ts
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[sitemap.xml].ts
@@ -2,19 +2,23 @@ export const sitemap = {
pages: [
{
path: "",
- lastModified: "2023-11-10T17:10:50.248Z",
+ lastModified: "2023-12-26T09:13:01.469Z",
},
{
path: "/_route_with_symbols_",
- lastModified: "2023-11-10T17:10:50.248Z",
+ lastModified: "2023-12-26T09:13:01.469Z",
},
{
path: "/form",
- lastModified: "2023-11-10T17:10:50.248Z",
+ lastModified: "2023-12-26T09:13:01.469Z",
},
{
path: "/heading-with-id",
- lastModified: "2023-11-10T17:10:50.248Z",
+ lastModified: "2023-12-26T09:13:01.469Z",
+ },
+ {
+ path: "/resources",
+ lastModified: "2023-12-26T09:13:01.469Z",
},
],
};
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
index 28867d7399..7699348189 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
@@ -1,7 +1,7 @@
/* eslint-disable */
/* This is a auto generated file for building the project */
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
import {
@@ -164,6 +164,7 @@ export const pagesPaths = new Set([
"/_route_with_symbols_",
"/form",
"/heading-with-id",
+ "/resources",
]);
export const formsProperties = new Map<
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
new file mode 100644
index 0000000000..6af28ce456
--- /dev/null
+++ b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
@@ -0,0 +1,308 @@
+/* eslint-disable camelcase */
+import {
+ type V2_ServerRuntimeMetaFunction,
+ type LinksFunction,
+ type LinkDescriptor,
+ type ActionArgs,
+ type LoaderArgs,
+ json,
+} from "@remix-run/server-runtime";
+import { useLoaderData } from "@remix-run/react";
+import type { Page as PageType, SiteMeta } from "@webstudio-is/sdk";
+import { ReactSdkContext } from "@webstudio-is/react-sdk";
+import { n8nHandler, getFormId } from "@webstudio-is/form-handlers";
+import {
+ fontAssets,
+ pageData,
+ user,
+ projectId,
+ pagesPaths,
+ formsProperties,
+ Page,
+ imageAssets,
+ getRemixParams,
+} from "../__generated__/[resources]._index";
+import { loadResources } from "../__generated__/[resources]._index.server";
+import css from "../__generated__/index.css";
+import { assetBaseUrl, imageBaseUrl, imageLoader } from "~/constants.mjs";
+
+export type PageData = {
+ site?: SiteMeta;
+ page: PageType;
+};
+
+export const loader = async (arg: LoaderArgs) => {
+ const params = getRemixParams(arg.params);
+ const resources = await loadResources({ params });
+
+ const host =
+ arg.request.headers.get("x-forwarded-host") ||
+ arg.request.headers.get("host") ||
+ "";
+
+ const url = new URL(arg.request.url);
+ url.host = host;
+ url.protocol = "https";
+
+ // typecheck
+ arg.context.EXCLUDE_FROM_SEARCH satisfies boolean;
+
+ return json(
+ {
+ host,
+ url: url.href,
+ excludeFromSearch: arg.context.EXCLUDE_FROM_SEARCH,
+ params,
+ resources,
+ },
+ // No way for current information to change, so add cache for 10 minutes
+ // In case of CRM Data, this should be set to 0
+ { headers: { "Cache-Control": "public, max-age=600" } }
+ );
+};
+
+export const headers = () => {
+ return {
+ "Cache-Control": "public, max-age=0, must-revalidate",
+ };
+};
+
+export const meta: V2_ServerRuntimeMetaFunction<typeof loader> = ({ data }) => {
+ const { page, site } = pageData;
+
+ const metas: ReturnType<V2_ServerRuntimeMetaFunction> = [];
+
+ if (data?.url) {
+ metas.push({
+ property: "og:url",
+ content: data.url,
+ });
+ }
+
+ if (page.title) {
+ metas.push({ title: page.title });
+
+ metas.push({
+ property: "og:title",
+ content: page.title,
+ });
+ }
+
+ metas.push({ property: "og:type", content: "website" });
+
+ const origin = `https://${data?.host}`;
+
+ if (site?.siteName) {
+ metas.push({
+ property: "og:site_name",
+ content: site.siteName,
+ });
+ metas.push({
+ "script:ld+json": {
+ "@context": "https://schema.org",
+ "@type": "WebSite",
+ name: site.siteName,
+ url: origin,
+ },
+ });
+ }
+
+ if (page.meta.excludePageFromSearch || data?.excludeFromSearch) {
+ metas.push({
+ name: "robots",
+ content: "noindex, nofollow",
+ });
+ }
+
+ if (page.meta.description) {
+ metas.push({
+ name: "description",
+ content: page.meta.description,
+ });
+ metas.push({
+ property: "og:description",
+ content: page.meta.description,
+ });
+ }
+
+ if (page.meta.socialImageAssetId) {
+ const imageAsset = imageAssets.find(
+ (asset) => asset.id === page.meta.socialImageAssetId
+ );
+
+ if (imageAsset) {
+ metas.push({
+ property: "og:image",
+ content: `https://${data?.host}${imageLoader({
+ src: imageAsset.name,
+ // Do not transform social image (not enough information do we need to do this)
+ format: "raw",
+ })}`,
+ });
+ }
+ }
+
+ for (const customMeta of page.meta.custom ?? []) {
+ if (customMeta.property.trim().length === 0) {
+ continue;
+ }
+ metas.push(customMeta);
+ }
+
+ return metas;
+};
+
+export const links: LinksFunction = () => {
+ const result: LinkDescriptor[] = [];
+
+ result.push({
+ rel: "stylesheet",
+ href: css,
+ });
+
+ const { site } = pageData;
+
+ if (site?.faviconAssetId) {
+ const imageAsset = imageAssets.find(
+ (asset) => asset.id === site.faviconAssetId
+ );
+
+ if (imageAsset) {
+ result.push({
+ rel: "icon",
+ href: imageLoader({
+ src: imageAsset.name,
+ width: 128,
+ quality: 100,
+ format: "auto",
+ }),
+ type: undefined,
+ });
+ }
+ } else {
+ result.push({
+ rel: "icon",
+ href: "/favicon.ico",
+ type: "image/x-icon",
+ });
+
+ result.push({
+ rel: "shortcut icon",
+ href: "/favicon.ico",
+ type: "image/x-icon",
+ });
+ }
+
+ for (const asset of fontAssets) {
+ if (asset.type === "font") {
+ result.push({
+ rel: "preload",
+ href: assetBaseUrl + asset.name,
+ as: "font",
+ crossOrigin: "anonymous",
+ // @todo add mimeType
+ // type: asset.mimeType,
+ });
+ }
+ }
+
+ return result;
+};
+
+const getRequestHost = (request: Request): string =>
+ request.headers.get("x-forwarded-host") || request.headers.get("host") || "";
+
+const getMethod = (value: string | undefined) => {
+ if (value === undefined) {
+ return "post";
+ }
+
+ switch (value.toLowerCase()) {
+ case "get":
+ return "get";
+ default:
+ return "post";
+ }
+};
+
+export const action = async ({ request, context }: ActionArgs) => {
+ const formData = await request.formData();
+
+ const formId = getFormId(formData);
+ if (formId === undefined) {
+ // We're throwing rather than returning { success: false }
+ // because this isn't supposed to happen normally: bug or malicious user
+ throw json("Form not found", { status: 404 });
+ }
+
+ const formProperties = formsProperties.get(formId);
+
+ // form properties are not defined when defaults are used
+ const { action, method } = formProperties ?? {};
+
+ const email = user?.email;
+
+ if (email == null) {
+ return { success: false };
+ }
+
+ // wrapped in try/catch just in cases new URL() throws
+ // (should not happen)
+ let pageUrl: URL;
+ try {
+ pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+ } catch {
+ return { success: false };
+ }
+
+ if (action !== undefined) {
+ try {
+ // Test that action is full URL
+ new URL(action);
+ } catch {
+ return json(
+ {
+ success: false,
+ error: "Invalid action URL, must be valid http/https protocol",
+ },
+ { status: 200 }
+ );
+ }
+ }
+
+ const formInfo = {
+ formData,
+ projectId,
+ action: action ?? null,
+ method: getMethod(method),
+ pageUrl: pageUrl.toString(),
+ toEmail: email,
+ fromEmail: pageUrl.hostname + "@webstudio.email",
+ } as const;
+
+ const result = await n8nHandler({
+ formInfo,
+ hookUrl: context.N8N_FORM_EMAIL_HOOK,
+ });
+
+ return result;
+};
+
+const Outlet = () => {
+ const { params, resources } = useLoaderData<typeof loader>();
+ return (
+ <ReactSdkContext.Provider
+ value={{
+ imageLoader,
+ assetBaseUrl,
+ imageBaseUrl,
+ pagesPaths,
+ }}
+ >
+ <Page params={params} resources={resources} />
+ </ReactSdkContext.Provider>
+ );
+};
+
+export default Outlet;
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index b16be6630b..c9e1d4ee21 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -27,6 +27,7 @@ import {
generateRemixRoute,
generateRemixParams,
generateResourcesLoader,
+ collectionComponent,
} from "@webstudio-is/react-sdk";
import type {
Instance,
@@ -339,12 +340,13 @@ export const prebuild = async (options: {
componentsByPage[page.path] = new Set();
for (const [_instanceId, instance] of instances) {
- if (instance.component) {
- componentsByPage[page.path].add(instance.component);
- const meta = metas.get(instance.component);
- if (meta) {
- projectMetas.set(instance.component, meta);
- }
+ if (instance.component === collectionComponent) {
+ continue;
+ }
+ componentsByPage[page.path].add(instance.component);
+ const meta = metas.get(instance.component);
+ if (meta) {
+ projectMetas.set(instance.component, meta);
}
}
}
@@ -412,7 +414,7 @@ export const prebuild = async (options: {
const scope = createScope([
// manually maintained list of occupied identifiers
"useState",
- "ReactNode",
+ "Fragment",
"PageData",
"Asset",
"fontAssets",
@@ -495,7 +497,7 @@ export const prebuild = async (options: {
const pageExports = `/* eslint-disable */
/* This is a auto generated file for building the project */ \n
-import { type ReactNode, useState } from "react";
+import { Fragment, useState } from "react";
import type { PageData } from "~/routes/_index";
import type { Asset, ImageAsset, SiteMeta } from "@webstudio-is/sdk";
${componentImports}
diff --git a/packages/react-sdk/src/component-generator.test.ts b/packages/react-sdk/src/component-generator.test.ts
index 6e0f9e2a26..36eeb55ba7 100644
--- a/packages/react-sdk/src/component-generator.test.ts
+++ b/packages/react-sdk/src/component-generator.test.ts
@@ -542,7 +542,7 @@ test("generate collection component as map", () => {
})
).toEqual(
clear(`
- {data.map((element, index) =>
+ {data?.map((element: any, index: number) =>
<Fragment key={index}>
<Label
data-ws-id="label"
@@ -674,7 +674,7 @@ test("avoid generating collection parameter variable as state", () => {
return <Body
data-ws-id="body"
data-ws-component="Body">
- {data.map((element, index) =>
+ {data?.map((element: any, index: number) =>
<Fragment key={index}>
</Fragment>
)}
@@ -770,7 +770,7 @@ test("generate resources loading", () => {
type Resources = Record<string, unknown>
const Page = (_props: { params: Params, resources: Resources }) => {
let [data, set$data] = useState<any>("data")
- let data_1 = _props.resources["data_2"]
+ let data_1: any = _props.resources["data_2"]
return <Body
data-ws-id="body"
data-ws-component="Body"
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index d6e0dbea88..dc1a5c9c5f 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -141,7 +141,8 @@ export const generateJsxElement = ({
return "";
}
const indexVariable = scope.getName(`${instance.id}-index`, "index");
- generatedElement += `{${collectionDataValue}.map((${collectionItemValue}, ${indexVariable}) =>\n`;
+ // fix implicit any error
+ generatedElement += `{${collectionDataValue}?.map((${collectionItemValue}: any, ${indexVariable}: number) =>\n`;
generatedElement += `<Fragment key={${indexVariable}}>\n`;
generatedElement += children;
generatedElement += `</Fragment>\n`;
@@ -269,7 +270,8 @@ export const generatePageComponent = ({
dataSource.resourceId,
dataSource.name
);
- generatedDataSources += `let ${valueName} = _props.resources["${resourceName}"]\n`;
+ // cast to any to fix accessing fields from unknown error
+ generatedDataSources += `let ${valueName}: any = _props.resources["${resourceName}"]\n`;
}
}
diff --git a/packages/react-sdk/src/resources-generator.test.ts b/packages/react-sdk/src/resources-generator.test.ts
index 5936d6442b..bddbe4dba3 100644
--- a/packages/react-sdk/src/resources-generator.test.ts
+++ b/packages/react-sdk/src/resources-generator.test.ts
@@ -43,6 +43,8 @@ test("generate resources loader", () => {
variableName,
] = await Promise.all([
loadResource({
+ id: "resourceId",
+ name: "resourceName",
url: "https://my-json.com",
method: "post",
headers: [
@@ -109,6 +111,8 @@ test("generate variable and use in resources loader", () => {
variableName,
] = await Promise.all([
loadResource({
+ id: "resourceId",
+ name: "resourceName",
url: "https://my-json.com/",
method: "post",
headers: [
@@ -169,6 +173,8 @@ test("generate page params variable and use in resources loader", () => {
variableName,
] = await Promise.all([
loadResource({
+ id: "resourceId",
+ name: "resourceName",
url: "https://my-json.com/" + Pageparams?.slug,
method: "post",
headers: [
diff --git a/packages/react-sdk/src/resources-generator.ts b/packages/react-sdk/src/resources-generator.ts
index f1e2be0118..4eac6746a4 100644
--- a/packages/react-sdk/src/resources-generator.ts
+++ b/packages/react-sdk/src/resources-generator.ts
@@ -43,6 +43,8 @@ export const generateResourcesLoader = ({
const resourceName = scope.getName(resource.id, dataSource.name);
generatedOutput += `${resourceName},\n`;
generatedLoaders += `loadResource({\n`;
+ generatedLoaders += `id: "${resource.id}",\n`;
+ generatedLoaders += `name: ${JSON.stringify(resource.name)},\n`;
const url = generateExpression({
expression: resource.url,
dataSources,
@@ -50,19 +52,18 @@ export const generateResourcesLoader = ({
});
generatedLoaders += `url: ${url},\n`;
generatedLoaders += `method: "${resource.method}",\n`;
- if (resource.headers.length > 0) {
- generatedLoaders += `headers: [\n`;
- for (const header of resource.headers) {
- const value = generateExpression({
- expression: header.value,
- dataSources,
- scope,
- });
- generatedLoaders += `{ name: "${header.name}", value: ${value} },\n`;
- }
- generatedLoaders += `],\n`;
+ generatedLoaders += `headers: [\n`;
+ for (const header of resource.headers) {
+ const value = generateExpression({
+ expression: header.value,
+ dataSources,
+ scope,
+ });
+ generatedLoaders += `{ name: "${header.name}", value: ${value} },\n`;
}
- if (resource.body !== undefined) {
+ generatedLoaders += `],\n`;
+ // prevent computing empty expression
+ if (resource.body !== undefined && resource.body.length > 0) {
const body = generateExpression({
expression: resource.body,
dataSources,