webstudio 0.0.0-017f1bd

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +139 -0
  3. package/bin.js +5 -0
  4. package/lib/cli.js +9121 -0
  5. package/package.json +90 -0
  6. package/templates/cloudflare/WS_CF_README.md +48 -0
  7. package/templates/cloudflare/functions/[[path]].ts +9 -0
  8. package/templates/cloudflare/load-context.ts +9 -0
  9. package/templates/cloudflare/package.json +22 -0
  10. package/templates/cloudflare/tsconfig.json +18 -0
  11. package/templates/cloudflare/vite.config.ts +29 -0
  12. package/templates/cloudflare/worker-configuration.d.ts +3 -0
  13. package/templates/cloudflare/wrangler.toml +55 -0
  14. package/templates/defaults/app/constants.mjs +12 -0
  15. package/templates/defaults/app/extension.ts +16 -0
  16. package/templates/defaults/app/root.tsx +39 -0
  17. package/templates/defaults/app/route-templates/default-sitemap.tsx +34 -0
  18. package/templates/defaults/app/route-templates/html.tsx +296 -0
  19. package/templates/defaults/app/route-templates/redirect.tsx +6 -0
  20. package/templates/defaults/app/route-templates/xml.tsx +83 -0
  21. package/templates/defaults/app/routes/[robots.txt].tsx +24 -0
  22. package/templates/defaults/package.json +35 -0
  23. package/templates/defaults/public/favicon.ico +0 -0
  24. package/templates/defaults/tsconfig.json +22 -0
  25. package/templates/defaults/vite.config.ts +24 -0
  26. package/templates/internal/package.json +11 -0
  27. package/templates/internal/tsconfig.json +5 -0
  28. package/templates/react-router/app/extension.ts +13 -0
  29. package/templates/react-router/app/root.tsx +39 -0
  30. package/templates/react-router/app/route-templates/default-sitemap.tsx +34 -0
  31. package/templates/react-router/app/route-templates/html.tsx +294 -0
  32. package/templates/react-router/app/route-templates/redirect.tsx +6 -0
  33. package/templates/react-router/app/route-templates/xml.tsx +87 -0
  34. package/templates/react-router/app/routes/[robots.txt].tsx +24 -0
  35. package/templates/react-router/app/routes.ts +4 -0
  36. package/templates/react-router/package.json +34 -0
  37. package/templates/react-router/public/favicon.ico +0 -0
  38. package/templates/react-router/tsconfig.json +18 -0
  39. package/templates/react-router/vite.config.ts +14 -0
  40. package/templates/react-router-cloudflare/app/constants.mjs +21 -0
  41. package/templates/react-router-cloudflare/app/entry.server.tsx +43 -0
  42. package/templates/react-router-cloudflare/package.json +12 -0
  43. package/templates/react-router-cloudflare/react-router.config.ts +7 -0
  44. package/templates/react-router-cloudflare/tsconfig.json +23 -0
  45. package/templates/react-router-cloudflare/vite.config.ts +15 -0
  46. package/templates/react-router-cloudflare/workers/app.ts +26 -0
  47. package/templates/react-router-cloudflare/wrangler.jsonc +6 -0
  48. package/templates/react-router-docker/.dockerignore +4 -0
  49. package/templates/react-router-docker/Dockerfile +19 -0
  50. package/templates/react-router-docker/app/constants.mjs +31 -0
  51. package/templates/react-router-docker/app/routes/[_image].$.ts +24 -0
  52. package/templates/react-router-docker/package.json +11 -0
  53. package/templates/react-router-netlify/app/constants.mjs +29 -0
  54. package/templates/react-router-netlify/netlify.toml +6 -0
  55. package/templates/react-router-netlify/package.json +10 -0
  56. package/templates/react-router-netlify/vite.config.ts +15 -0
  57. package/templates/react-router-vercel/app/constants.mjs +25 -0
  58. package/templates/react-router-vercel/package.json +9 -0
  59. package/templates/react-router-vercel/react-router.config.ts +6 -0
  60. package/templates/react-router-vercel/vercel.json +12 -0
  61. package/templates/saas-helpers/package.json +9 -0
  62. package/templates/saas-helpers/tsconfig.json +18 -0
  63. package/templates/saas-helpers/vite.config.ts +48 -0
  64. package/templates/ssg/app/constants.mjs +13 -0
  65. package/templates/ssg/app/route-templates/html/+Head.tsx +100 -0
  66. package/templates/ssg/app/route-templates/html/+Page.tsx +23 -0
  67. package/templates/ssg/app/route-templates/html/+data.ts +37 -0
  68. package/templates/ssg/package.json +31 -0
  69. package/templates/ssg/pages/+config.ts +12 -0
  70. package/templates/ssg/public/favicon.ico +0 -0
  71. package/templates/ssg/renderer/+onRenderClient.tsx +30 -0
  72. package/templates/ssg/renderer/+onRenderHtml.tsx +37 -0
  73. package/templates/ssg/tsconfig.json +22 -0
  74. package/templates/ssg/vike.d.ts +25 -0
  75. package/templates/ssg/vite.config.ts +15 -0
  76. package/templates/ssg-netlify/app/constants.mjs +30 -0
  77. package/templates/ssg-vercel/app/constants.mjs +26 -0
  78. package/templates/ssg-vercel/public/vercel.json +11 -0
@@ -0,0 +1,6 @@
1
+ import { redirect } from "@remix-run/server-runtime";
2
+ import { url, status } from "__REDIRECT__";
3
+
4
+ export const loader = () => {
5
+ return redirect(url, status);
6
+ };
@@ -0,0 +1,83 @@
1
+ import { renderToString } from "react-dom/server";
2
+ import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
3
+ import { isLocalResource, loadResources } from "@webstudio-is/sdk/runtime";
4
+ import {
5
+ ReactSdkContext,
6
+ xmlNodeTagSuffix,
7
+ } from "@webstudio-is/react-sdk/runtime";
8
+ import { Page, breakpoints } from "__CLIENT__";
9
+ import { getPageMeta, getRemixParams, getResources } from "__SERVER__";
10
+ import { assetBaseUrl, imageLoader } from "__CONSTANTS__";
11
+ import { sitemap } from "__SITEMAP__";
12
+
13
+ const customFetch: typeof fetch = (input, init) => {
14
+ if (typeof input !== "string") {
15
+ return fetch(input, init);
16
+ }
17
+
18
+ if (isLocalResource(input, "sitemap.xml")) {
19
+ // @todo: dynamic import sitemap ???
20
+ const response = new Response(JSON.stringify(sitemap));
21
+ response.headers.set("content-type", "application/json; charset=utf-8");
22
+ return Promise.resolve(response);
23
+ }
24
+
25
+ return fetch(input, init);
26
+ };
27
+
28
+ export const loader = async (arg: LoaderFunctionArgs) => {
29
+ const url = new URL(arg.request.url);
30
+ const host =
31
+ arg.request.headers.get("x-forwarded-host") ||
32
+ arg.request.headers.get("host") ||
33
+ "";
34
+ url.host = host;
35
+ url.protocol = "https";
36
+
37
+ const params = getRemixParams(arg.params);
38
+
39
+ const system = {
40
+ params,
41
+ search: Object.fromEntries(url.searchParams),
42
+ origin: url.origin,
43
+ };
44
+
45
+ const resources = await loadResources(
46
+ customFetch,
47
+ getResources({ system }).data
48
+ );
49
+ const pageMeta = getPageMeta({ system, resources });
50
+
51
+ if (pageMeta.redirect) {
52
+ const status =
53
+ pageMeta.status === 301 || pageMeta.status === 302
54
+ ? pageMeta.status
55
+ : 302;
56
+ return redirect(pageMeta.redirect, status);
57
+ }
58
+
59
+ // typecheck
60
+ arg.context.EXCLUDE_FROM_SEARCH satisfies boolean;
61
+
62
+ let text = renderToString(
63
+ <ReactSdkContext.Provider
64
+ value={{
65
+ imageLoader,
66
+ assetBaseUrl,
67
+ resources,
68
+ breakpoints,
69
+ onError: console.error,
70
+ }}
71
+ >
72
+ <Page system={system} />
73
+ </ReactSdkContext.Provider>
74
+ );
75
+
76
+ // React has issues rendering certain elements, such as errors when a <link> element has children.
77
+ // To render XML, we wrap it with an <svg> tag and add a suffix to avoid React's default behavior on these elements.
78
+ text = text.replaceAll(xmlNodeTagSuffix, "");
79
+
80
+ return new Response(`<?xml version="1.0" encoding="UTF-8"?>\n${text}`, {
81
+ headers: { "Content-Type": "application/xml" },
82
+ });
83
+ };
@@ -0,0 +1,24 @@
1
+ import type { LoaderFunctionArgs } from "@remix-run/server-runtime";
2
+
3
+ export const loader = (arg: LoaderFunctionArgs) => {
4
+ const host =
5
+ arg.request.headers.get("x-forwarded-host") ||
6
+ arg.request.headers.get("host") ||
7
+ "";
8
+
9
+ return new Response(
10
+ `
11
+ User-agent: *
12
+ Disallow: /api/
13
+
14
+ Sitemap: https://${host}/sitemap.xml
15
+
16
+ `,
17
+ {
18
+ headers: {
19
+ "Content-Type": "text/plain",
20
+ },
21
+ status: 200,
22
+ }
23
+ );
24
+ };
@@ -0,0 +1,35 @@
1
+ {
2
+ "type": "module",
3
+ "private": true,
4
+ "sideEffects": false,
5
+ "scripts": {
6
+ "build": "remix vite:build",
7
+ "dev": "remix vite:dev",
8
+ "typecheck": "tsc"
9
+ },
10
+ "dependencies": {
11
+ "@remix-run/node": "2.16.5",
12
+ "@remix-run/react": "2.16.5",
13
+ "@remix-run/server-runtime": "2.16.5",
14
+ "@webstudio-is/image": "0.0.0-017f1bd",
15
+ "@webstudio-is/react-sdk": "0.0.0-017f1bd",
16
+ "@webstudio-is/sdk": "0.0.0-017f1bd",
17
+ "@webstudio-is/sdk-components-react": "0.0.0-017f1bd",
18
+ "@webstudio-is/sdk-components-animation": "0.0.0-017f1bd",
19
+ "@webstudio-is/sdk-components-react-radix": "0.0.0-017f1bd",
20
+ "@webstudio-is/sdk-components-react-remix": "0.0.0-017f1bd",
21
+ "isbot": "^5.1.25",
22
+ "react": "18.3.0-canary-14898b6a9-20240318",
23
+ "react-dom": "18.3.0-canary-14898b6a9-20240318"
24
+ },
25
+ "devDependencies": {
26
+ "@remix-run/dev": "2.16.5",
27
+ "@types/react": "^18.2.70",
28
+ "@types/react-dom": "^18.2.25",
29
+ "typescript": "5.8.2",
30
+ "vite": "^6.3.4"
31
+ },
32
+ "engines": {
33
+ "node": ">=20.0.0"
34
+ }
35
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "include": ["**/*.ts", "**/*.tsx", "**/*.mjs"],
3
+ "compilerOptions": {
4
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
5
+ "types": [
6
+ "@remix-run/node",
7
+ "vite/client",
8
+ "@webstudio-is/react-sdk/placeholder"
9
+ ],
10
+ "isolatedModules": true,
11
+ "esModuleInterop": true,
12
+ "jsx": "react-jsx",
13
+ "module": "ESNext",
14
+ "moduleResolution": "bundler",
15
+ "target": "ES2022",
16
+ "strict": true,
17
+ "allowJs": true,
18
+ "forceConsistentCasingInFileNames": true,
19
+ "noEmit": true,
20
+ "skipLibCheck": true
21
+ }
22
+ }
@@ -0,0 +1,24 @@
1
+ import { defineConfig } from "vite";
2
+ import { vitePlugin as remix } from "@remix-run/dev";
3
+
4
+ export default defineConfig({
5
+ plugins: [
6
+ remix({
7
+ future: {
8
+ v3_lazyRouteDiscovery: false,
9
+ v3_relativeSplatPath: false,
10
+ v3_singleFetch: false,
11
+ v3_fetcherPersist: false,
12
+ v3_throwAbortReason: false,
13
+ },
14
+ }),
15
+ ],
16
+ resolve: {
17
+ conditions: ["browser", "development|production"],
18
+ },
19
+ ssr: {
20
+ resolve: {
21
+ conditions: ["node", "development|production"],
22
+ },
23
+ },
24
+ });
@@ -0,0 +1,11 @@
1
+ {
2
+ "dependencies": {
3
+ "@webstudio-is/image": "workspace:*",
4
+ "@webstudio-is/react-sdk": "workspace:*",
5
+ "@webstudio-is/sdk": "workspace:*",
6
+ "@webstudio-is/sdk-components-animation": "workspace:*",
7
+ "@webstudio-is/sdk-components-react": "workspace:*",
8
+ "@webstudio-is/sdk-components-react-radix": "workspace:*",
9
+ "@webstudio-is/sdk-components-react-remix": "workspace:*"
10
+ }
11
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "compilerOptions": {
3
+ "customConditions": ["webstudio"]
4
+ }
5
+ }
@@ -0,0 +1,13 @@
1
+ import { ResourceRequest } from "@webstudio-is/sdk";
2
+
3
+ declare module "react-router" {
4
+ interface AppLoadContext {
5
+ EXCLUDE_FROM_SEARCH: boolean;
6
+ getDefaultActionResource?: (options: {
7
+ url: URL;
8
+ projectId: string;
9
+ contactEmail: string;
10
+ formData: FormData;
11
+ }) => ResourceRequest;
12
+ }
13
+ }
@@ -0,0 +1,39 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+
3
+ import { Links, Meta, Outlet, useMatches } from "react-router";
4
+ // @todo think about how to make __generated__ typeable
5
+ // @ts-ignore
6
+ import { CustomCode, projectId, lastPublished } from "./__generated__/_index";
7
+
8
+ const Root = () => {
9
+ // Get language from matches
10
+ const matches = useMatches();
11
+
12
+ const lastMatchWithLanguage = matches.findLast((match) => {
13
+ // @ts-ignore
14
+ const language = match?.data?.pageMeta?.language;
15
+ return language != null;
16
+ });
17
+
18
+ // @ts-ignore
19
+ const lang = lastMatchWithLanguage?.data?.pageMeta?.language ?? "en";
20
+
21
+ return (
22
+ <html
23
+ lang={lang}
24
+ data-ws-project={projectId}
25
+ data-ws-last-published={lastPublished}
26
+ >
27
+ <head>
28
+ <meta charSet="utf-8" />
29
+ <meta name="viewport" content="width=device-width,initial-scale=1" />
30
+ <Meta />
31
+ <Links />
32
+ <CustomCode />
33
+ </head>
34
+ <Outlet />
35
+ </html>
36
+ );
37
+ };
38
+
39
+ export default Root;
@@ -0,0 +1,34 @@
1
+ import type { LoaderFunctionArgs } from "react-router";
2
+ import { sitemap } from "__SITEMAP__";
3
+
4
+ export const loader = (arg: LoaderFunctionArgs) => {
5
+ const host =
6
+ arg.request.headers.get("x-forwarded-host") ||
7
+ arg.request.headers.get("host") ||
8
+ "";
9
+
10
+ const urls = sitemap.map((page) => {
11
+ const url = new URL(`https://${host}${page.path}`);
12
+
13
+ return `
14
+ <url>
15
+ <loc>${url.href}</loc>
16
+ <lastmod>${page.lastModified.split("T")[0]}</lastmod>
17
+ </url>
18
+ `;
19
+ });
20
+
21
+ return new Response(
22
+ `<?xml version="1.0" encoding="UTF-8"?>
23
+ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
24
+ ${urls.join("")}
25
+ </urlset>
26
+ `,
27
+ {
28
+ headers: {
29
+ "Content-Type": "application/xml",
30
+ },
31
+ status: 200,
32
+ }
33
+ );
34
+ };
@@ -0,0 +1,294 @@
1
+ import {
2
+ type MetaFunction,
3
+ type LinksFunction,
4
+ type LinkDescriptor,
5
+ type ActionFunctionArgs,
6
+ type LoaderFunctionArgs,
7
+ type HeadersFunction,
8
+ data,
9
+ redirect,
10
+ useLoaderData,
11
+ } from "react-router";
12
+ import {
13
+ isLocalResource,
14
+ loadResource,
15
+ loadResources,
16
+ formIdFieldName,
17
+ formBotFieldName,
18
+ } from "@webstudio-is/sdk/runtime";
19
+ import {
20
+ ReactSdkContext,
21
+ PageSettingsMeta,
22
+ PageSettingsTitle,
23
+ } from "@webstudio-is/react-sdk/runtime";
24
+ import {
25
+ projectId,
26
+ Page,
27
+ siteName,
28
+ favIconAsset,
29
+ pageFontAssets,
30
+ pageBackgroundImageAssets,
31
+ breakpoints,
32
+ } from "__CLIENT__";
33
+ import {
34
+ getResources,
35
+ getPageMeta,
36
+ getRemixParams,
37
+ contactEmail,
38
+ } from "__SERVER__";
39
+ import * as constants from "__CONSTANTS__";
40
+ import css from "__CSS__?url";
41
+ import { sitemap } from "__SITEMAP__";
42
+
43
+ const customFetch: typeof fetch = (input, init) => {
44
+ if (typeof input !== "string") {
45
+ return fetch(input, init);
46
+ }
47
+
48
+ if (isLocalResource(input, "sitemap.xml")) {
49
+ // @todo: dynamic import sitemap ???
50
+ const response = new Response(JSON.stringify(sitemap));
51
+ response.headers.set("content-type", "application/json; charset=utf-8");
52
+ return Promise.resolve(response);
53
+ }
54
+
55
+ return fetch(input, init);
56
+ };
57
+
58
+ export const loader = async (arg: LoaderFunctionArgs) => {
59
+ const url = new URL(arg.request.url);
60
+ const host =
61
+ arg.request.headers.get("x-forwarded-host") ||
62
+ arg.request.headers.get("host") ||
63
+ "";
64
+ url.host = host;
65
+ url.protocol = "https";
66
+
67
+ const params = getRemixParams(arg.params);
68
+ const system = {
69
+ params,
70
+ search: Object.fromEntries(url.searchParams),
71
+ origin: url.origin,
72
+ };
73
+
74
+ const resources = await loadResources(
75
+ customFetch,
76
+ getResources({ system }).data
77
+ );
78
+ const pageMeta = getPageMeta({ system, resources });
79
+
80
+ if (pageMeta.redirect) {
81
+ const status =
82
+ pageMeta.status === 301 || pageMeta.status === 302
83
+ ? pageMeta.status
84
+ : 302;
85
+ throw redirect(pageMeta.redirect, status);
86
+ }
87
+
88
+ // typecheck
89
+ arg.context.EXCLUDE_FROM_SEARCH satisfies boolean;
90
+
91
+ if (arg.context.EXCLUDE_FROM_SEARCH) {
92
+ pageMeta.excludePageFromSearch = arg.context.EXCLUDE_FROM_SEARCH;
93
+ }
94
+
95
+ return data(
96
+ {
97
+ host,
98
+ url: url.href,
99
+ system,
100
+ resources,
101
+ pageMeta,
102
+ },
103
+ // No way for current information to change, so add cache for 10 minutes
104
+ // In case of CRM Data, this should be set to 0
105
+ {
106
+ status: pageMeta.status,
107
+ headers: {
108
+ "Cache-Control": "public, max-age=600",
109
+ },
110
+ }
111
+ );
112
+ };
113
+
114
+ export const headers: HeadersFunction = () => {
115
+ return {
116
+ "Cache-Control": "public, max-age=0, must-revalidate",
117
+ };
118
+ };
119
+
120
+ export const meta: MetaFunction<typeof loader> = ({ data }) => {
121
+ const metas: ReturnType<MetaFunction> = [];
122
+ if (data === undefined) {
123
+ return metas;
124
+ }
125
+
126
+ const origin = `https://${data.host}`;
127
+
128
+ if (siteName) {
129
+ metas.push({
130
+ "script:ld+json": {
131
+ "@context": "https://schema.org",
132
+ "@type": "WebSite",
133
+ name: siteName,
134
+ url: origin,
135
+ },
136
+ });
137
+ }
138
+
139
+ return metas;
140
+ };
141
+
142
+ export const links: LinksFunction = () => {
143
+ const result: LinkDescriptor[] = [];
144
+
145
+ result.push({
146
+ rel: "stylesheet",
147
+ href: css,
148
+ });
149
+
150
+ if (favIconAsset) {
151
+ result.push({
152
+ rel: "icon",
153
+ href: constants.imageLoader({
154
+ src: `${constants.assetBaseUrl}${favIconAsset}`,
155
+ // width,height must be multiple of 48 https://developers.google.com/search/docs/appearance/favicon-in-search
156
+ width: 144,
157
+ height: 144,
158
+ fit: "pad",
159
+ quality: 100,
160
+ format: "auto",
161
+ }),
162
+ type: undefined,
163
+ });
164
+ }
165
+
166
+ for (const asset of pageFontAssets) {
167
+ result.push({
168
+ rel: "preload",
169
+ href: `${constants.assetBaseUrl}${asset}`,
170
+ as: "font",
171
+ crossOrigin: "anonymous",
172
+ });
173
+ }
174
+
175
+ for (const backgroundImageAsset of pageBackgroundImageAssets) {
176
+ result.push({
177
+ rel: "preload",
178
+ href: `${constants.assetBaseUrl}${backgroundImageAsset}`,
179
+ as: "image",
180
+ });
181
+ }
182
+
183
+ return result;
184
+ };
185
+
186
+ const getRequestHost = (request: Request): string =>
187
+ request.headers.get("x-forwarded-host") || request.headers.get("host") || "";
188
+
189
+ export const action = async ({
190
+ request,
191
+ context,
192
+ }: ActionFunctionArgs): Promise<
193
+ { success: true } | { success: false; errors: string[] }
194
+ > => {
195
+ try {
196
+ const url = new URL(request.url);
197
+ url.host = getRequestHost(request);
198
+
199
+ const formData = await request.formData();
200
+
201
+ const system = {
202
+ params: {},
203
+ search: {},
204
+ origin: url.origin,
205
+ };
206
+
207
+ const resourceName = formData.get(formIdFieldName);
208
+ let resource =
209
+ typeof resourceName === "string"
210
+ ? getResources({ system }).action.get(resourceName)
211
+ : undefined;
212
+
213
+ const formBotValue = formData.get(formBotFieldName);
214
+
215
+ if (formBotValue == null || typeof formBotValue !== "string") {
216
+ throw new Error("Form bot field not found");
217
+ }
218
+
219
+ const submitTime = parseInt(formBotValue, 16);
220
+ // Assumes that the difference between the server time and the form submission time,
221
+ // including any client-server time drift, is within a 5-minute range.
222
+ // Note: submitTime might be NaN because formBotValue can be any string used for logging purposes.
223
+ // Example: `formBotValue: jsdom`, or `formBotValue: headless-env`
224
+ if (
225
+ Number.isNaN(submitTime) ||
226
+ Math.abs(Date.now() - submitTime) > 1000 * 60 * 5
227
+ ) {
228
+ throw new Error(`Form bot value invalid ${formBotValue}`);
229
+ }
230
+
231
+ formData.delete(formIdFieldName);
232
+ formData.delete(formBotFieldName);
233
+
234
+ if (resource) {
235
+ resource.body = Object.fromEntries(formData);
236
+ } else {
237
+ if (contactEmail === undefined) {
238
+ throw new Error("Contact email not found");
239
+ }
240
+
241
+ resource = context.getDefaultActionResource?.({
242
+ url,
243
+ projectId,
244
+ contactEmail,
245
+ formData,
246
+ });
247
+ }
248
+
249
+ if (resource === undefined) {
250
+ throw Error("Resource not found");
251
+ }
252
+ const { ok, statusText } = await loadResource(fetch, resource);
253
+ if (ok) {
254
+ return { success: true };
255
+ }
256
+ return { success: false, errors: [statusText] };
257
+ } catch (error) {
258
+ console.error(error);
259
+
260
+ return {
261
+ success: false,
262
+ errors: [error instanceof Error ? error.message : "Unknown error"],
263
+ };
264
+ }
265
+ };
266
+
267
+ const Outlet = () => {
268
+ const { system, resources, url, pageMeta, host } =
269
+ useLoaderData<typeof loader>();
270
+ return (
271
+ <ReactSdkContext.Provider
272
+ value={{
273
+ ...constants,
274
+ resources,
275
+ breakpoints,
276
+ onError: console.error,
277
+ }}
278
+ >
279
+ {/* Use the URL as the key to force scripts in HTML Embed to reload on dynamic pages */}
280
+ <Page key={url} system={system} />
281
+ <PageSettingsMeta
282
+ url={url}
283
+ pageMeta={pageMeta}
284
+ host={host}
285
+ siteName={siteName}
286
+ imageLoader={constants.imageLoader}
287
+ assetBaseUrl={constants.assetBaseUrl}
288
+ />
289
+ <PageSettingsTitle>{pageMeta.title}</PageSettingsTitle>
290
+ </ReactSdkContext.Provider>
291
+ );
292
+ };
293
+
294
+ export default Outlet;
@@ -0,0 +1,6 @@
1
+ import { redirect } from "react-router";
2
+ import { url, status } from "__REDIRECT__";
3
+
4
+ export const loader = () => {
5
+ throw redirect(url, status);
6
+ };
@@ -0,0 +1,87 @@
1
+ import { renderToString } from "react-dom/server";
2
+ import { type LoaderFunctionArgs, redirect } from "react-router";
3
+ import { isLocalResource, loadResources } from "@webstudio-is/sdk/runtime";
4
+ import {
5
+ ReactSdkContext,
6
+ xmlNodeTagSuffix,
7
+ } from "@webstudio-is/react-sdk/runtime";
8
+ import { Page, breakpoints } from "__CLIENT__";
9
+ import { getPageMeta, getRemixParams, getResources } from "__SERVER__";
10
+ import { assetBaseUrl, imageLoader } from "__CONSTANTS__";
11
+ import { sitemap } from "__SITEMAP__";
12
+
13
+ const customFetch: typeof fetch = (input, init) => {
14
+ if (typeof input !== "string") {
15
+ return fetch(input, init);
16
+ }
17
+
18
+ if (isLocalResource(input, "sitemap.xml")) {
19
+ // @todo: dynamic import sitemap ???
20
+ const response = new Response(JSON.stringify(sitemap));
21
+ response.headers.set("content-type", "application/json; charset=utf-8");
22
+ return Promise.resolve(response);
23
+ }
24
+
25
+ return fetch(input, init);
26
+ };
27
+
28
+ export const loader = async (arg: LoaderFunctionArgs) => {
29
+ const url = new URL(arg.request.url);
30
+ const host =
31
+ arg.request.headers.get("x-forwarded-host") ||
32
+ arg.request.headers.get("host") ||
33
+ "";
34
+ url.host = host;
35
+ url.protocol = "https";
36
+
37
+ const params = getRemixParams(arg.params);
38
+
39
+ const system = {
40
+ params,
41
+ search: Object.fromEntries(url.searchParams),
42
+ origin: url.origin,
43
+ };
44
+
45
+ const resources = await loadResources(
46
+ customFetch,
47
+ getResources({ system }).data
48
+ );
49
+ const pageMeta = getPageMeta({ system, resources });
50
+
51
+ if (pageMeta.redirect) {
52
+ const status =
53
+ pageMeta.status === 301 || pageMeta.status === 302
54
+ ? pageMeta.status
55
+ : 302;
56
+ return redirect(pageMeta.redirect, status);
57
+ }
58
+
59
+ // typecheck
60
+ arg.context.EXCLUDE_FROM_SEARCH satisfies boolean;
61
+
62
+ let text = renderToString(
63
+ <ReactSdkContext.Provider
64
+ value={{
65
+ imageLoader,
66
+ assetBaseUrl,
67
+ resources,
68
+ breakpoints,
69
+ onError: console.error,
70
+ }}
71
+ >
72
+ <Page system={system} />
73
+ </ReactSdkContext.Provider>
74
+ );
75
+
76
+ // Xml is wrapped with <svg> to prevent React from hoisting elements like <title>, <meta>, and <link> out of their intended scope during rendering.
77
+ // More details: https://github.com/facebook/react/blob/7c8e5e7ab8bb63de911637892392c5efd8ce1d0f/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js#L3083
78
+ text = text.replace(/^<svg>/g, "").replace(/<\/svg>$/g, "");
79
+
80
+ // React has issues rendering certain elements, such as errors when a <link> element has children.
81
+ // To render XML, we wrap it with an <svg> tag and add a suffix to avoid React's default behavior on these elements.
82
+ text = text.replaceAll(xmlNodeTagSuffix, "");
83
+
84
+ return new Response(`<?xml version="1.0" encoding="UTF-8"?>\n${text}`, {
85
+ headers: { "Content-Type": "application/xml" },
86
+ });
87
+ };