create-puck-app 0.12.0-canary.da2bc0f → 0.12.0-canary.f882878

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-puck-app",
3
- "version": "0.12.0-canary.da2bc0f",
3
+ "version": "0.12.0-canary.f882878",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -0,0 +1,4 @@
1
+ /** @type {import('eslint').Linter.Config} */
2
+ module.exports = {
3
+ extends: ["@remix-run/eslint-config", "@remix-run/eslint-config/node"],
4
+ };
@@ -0,0 +1,42 @@
1
+ # `remix` recipe
2
+
3
+ The `remix` recipe showcases a Remix Run app with Puck, using it to provide an authoring tool for any root-level route in your Remix app.
4
+
5
+ ## Demonstrates
6
+
7
+ - Remix Run V2 implementation
8
+ - JSON database implementation with HTTP API
9
+ - Dynamic routes to use puck for any root-level route on the platform
10
+ - Option to disable client-side JavaScript for Puck pages
11
+
12
+ ## Usage
13
+
14
+ Run the generator and enter `next` when prompted
15
+
16
+ ```
17
+ npx create-puck-app my-app
18
+ ```
19
+
20
+ Start the server
21
+
22
+ ```
23
+ yarn dev
24
+ ```
25
+
26
+ Navigate to the homepage at https://localhost:3000. To edit the homepage, access the Puck editor at https://localhost:3000/edit.
27
+
28
+ You can do this for any **base** route on the application, **even if the page doesn't exist**. For example, visit https://localhost:3000/hello-world and you'll receive a 404. You can author and publish a page by visiting https://localhost:3000/hello-world/edit. After publishing, go back to the original URL to see your page.
29
+
30
+ ## Using this recipe
31
+
32
+ To adopt this recipe you will need to:
33
+
34
+ - **IMPORTANT** Add authentication to `/edit` routes. This can be done by modifying the example routes `/app/routes/_index.tsx` and `/app/routes/edit.tsx` or the example model in `/app/models/page.server.ts`. **If you don't do this, Puck will be completely public.**
35
+ - Integrate your database into the API calls in `/app/models/page.server.ts`
36
+ - Implement a custom puck configuration in `puck.config.tsx`
37
+
38
+ By default, this recipe will have JavaScript enable on all routes - like a usual react app. If you know that your Puck content doesn't need react, then you can disable JS uncommenting the relevant code in `/app/root.tsx` and the example route `/app/routes/_index.tsx`. Check the network tab for no JS downloads, and verify that the page still works.
39
+
40
+ ## License
41
+
42
+ MIT © [Measured Co.](https://github.com/measuredco)
@@ -0,0 +1,18 @@
1
+ /**
2
+ * By default, Remix will handle hydrating your app on the client for you.
3
+ * You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨
4
+ * For more information, see https://remix.run/file-conventions/entry.client
5
+ */
6
+
7
+ import { RemixBrowser } from "@remix-run/react";
8
+ import { startTransition, StrictMode } from "react";
9
+ import { hydrateRoot } from "react-dom/client";
10
+
11
+ startTransition(() => {
12
+ hydrateRoot(
13
+ document,
14
+ <StrictMode>
15
+ <RemixBrowser />
16
+ </StrictMode>
17
+ );
18
+ });
@@ -0,0 +1,137 @@
1
+ /**
2
+ * By default, Remix will handle generating the HTTP Response for you.
3
+ * You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨
4
+ * For more information, see https://remix.run/file-conventions/entry.server
5
+ */
6
+
7
+ import { PassThrough } from "node:stream";
8
+
9
+ import type { AppLoadContext, EntryContext } from "@remix-run/node";
10
+ import { createReadableStreamFromReadable } from "@remix-run/node";
11
+ import { RemixServer } from "@remix-run/react";
12
+ import isbot from "isbot";
13
+ import { renderToPipeableStream } from "react-dom/server";
14
+
15
+ const ABORT_DELAY = 5_000;
16
+
17
+ export default function handleRequest(
18
+ request: Request,
19
+ responseStatusCode: number,
20
+ responseHeaders: Headers,
21
+ remixContext: EntryContext,
22
+ loadContext: AppLoadContext
23
+ ) {
24
+ return isbot(request.headers.get("user-agent"))
25
+ ? handleBotRequest(
26
+ request,
27
+ responseStatusCode,
28
+ responseHeaders,
29
+ remixContext
30
+ )
31
+ : handleBrowserRequest(
32
+ request,
33
+ responseStatusCode,
34
+ responseHeaders,
35
+ remixContext
36
+ );
37
+ }
38
+
39
+ function handleBotRequest(
40
+ request: Request,
41
+ responseStatusCode: number,
42
+ responseHeaders: Headers,
43
+ remixContext: EntryContext
44
+ ) {
45
+ return new Promise((resolve, reject) => {
46
+ let shellRendered = false;
47
+ const { pipe, abort } = renderToPipeableStream(
48
+ <RemixServer
49
+ context={remixContext}
50
+ url={request.url}
51
+ abortDelay={ABORT_DELAY}
52
+ />,
53
+ {
54
+ onAllReady() {
55
+ shellRendered = true;
56
+ const body = new PassThrough();
57
+ const stream = createReadableStreamFromReadable(body);
58
+
59
+ responseHeaders.set("Content-Type", "text/html");
60
+
61
+ resolve(
62
+ new Response(stream, {
63
+ headers: responseHeaders,
64
+ status: responseStatusCode,
65
+ })
66
+ );
67
+
68
+ pipe(body);
69
+ },
70
+ onShellError(error: unknown) {
71
+ reject(error);
72
+ },
73
+ onError(error: unknown) {
74
+ responseStatusCode = 500;
75
+ // Log streaming rendering errors from inside the shell. Don't log
76
+ // errors encountered during initial shell rendering since they'll
77
+ // reject and get logged in handleDocumentRequest.
78
+ if (shellRendered) {
79
+ console.error(error);
80
+ }
81
+ },
82
+ }
83
+ );
84
+
85
+ setTimeout(abort, ABORT_DELAY);
86
+ });
87
+ }
88
+
89
+ function handleBrowserRequest(
90
+ request: Request,
91
+ responseStatusCode: number,
92
+ responseHeaders: Headers,
93
+ remixContext: EntryContext
94
+ ) {
95
+ return new Promise((resolve, reject) => {
96
+ let shellRendered = false;
97
+ const { pipe, abort } = renderToPipeableStream(
98
+ <RemixServer
99
+ context={remixContext}
100
+ url={request.url}
101
+ abortDelay={ABORT_DELAY}
102
+ />,
103
+ {
104
+ onShellReady() {
105
+ shellRendered = true;
106
+ const body = new PassThrough();
107
+ const stream = createReadableStreamFromReadable(body);
108
+
109
+ responseHeaders.set("Content-Type", "text/html");
110
+
111
+ resolve(
112
+ new Response(stream, {
113
+ headers: responseHeaders,
114
+ status: responseStatusCode,
115
+ })
116
+ );
117
+
118
+ pipe(body);
119
+ },
120
+ onShellError(error: unknown) {
121
+ reject(error);
122
+ },
123
+ onError(error: unknown) {
124
+ responseStatusCode = 500;
125
+ // Log streaming rendering errors from inside the shell. Don't log
126
+ // errors encountered during initial shell rendering since they'll
127
+ // reject and get logged in handleDocumentRequest.
128
+ if (shellRendered) {
129
+ console.error(error);
130
+ }
131
+ },
132
+ }
133
+ );
134
+
135
+ setTimeout(abort, ABORT_DELAY);
136
+ });
137
+ }
@@ -0,0 +1,27 @@
1
+ import { Data } from "@measured/puck";
2
+ import fs from "fs";
3
+
4
+ // Replace with call to your database
5
+ export const getPage = (path: string) => {
6
+ const allData: Record<string, Data> | null = fs.existsSync("database.json")
7
+ ? JSON.parse(fs.readFileSync("database.json", "utf-8"))
8
+ : null;
9
+
10
+ return allData ? allData[path] : null;
11
+ };
12
+
13
+ // Replace with call to your database
14
+ export const setPage = (path: string, data: Data) => {
15
+ const existingData = JSON.parse(
16
+ fs.existsSync("database.json")
17
+ ? fs.readFileSync("database.json", "utf-8")
18
+ : "{}"
19
+ );
20
+
21
+ const updatedData = {
22
+ ...existingData,
23
+ [path]: data,
24
+ };
25
+
26
+ fs.writeFileSync("database.json", JSON.stringify(updatedData));
27
+ };
@@ -0,0 +1,43 @@
1
+ import { cssBundleHref } from "@remix-run/css-bundle";
2
+ import type { LinksFunction } from "@remix-run/node";
3
+ import {
4
+ Links,
5
+ LiveReload,
6
+ Meta,
7
+ Outlet,
8
+ Scripts,
9
+ ScrollRestoration,
10
+ // useMatches,
11
+ } from "@remix-run/react";
12
+
13
+ export const links: LinksFunction = () => [
14
+ ...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
15
+ ];
16
+
17
+ export default function App() {
18
+ /**
19
+ * Disable client-side JS - Optional
20
+ * @see https://remix.run/docs/en/main/guides/disabling-javascript
21
+ */
22
+ // const matches = useMatches();
23
+ // const includeScripts = matches.some((match) => match.handle?.hydrate);
24
+
25
+ return (
26
+ <html lang="en">
27
+ <head>
28
+ <meta charSet="utf-8" />
29
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
30
+ <Meta />
31
+ <Links />
32
+ </head>
33
+ <body>
34
+ <Outlet />
35
+ <ScrollRestoration />
36
+ {/* Conditionally render scripts - Optional */}
37
+ {/* {includeScripts ? <Scripts /> : null} */}
38
+ <Scripts />
39
+ <LiveReload />
40
+ </body>
41
+ </html>
42
+ );
43
+ }
@@ -0,0 +1,2 @@
1
+ export { default } from "./_index";
2
+ export * from "./_index";
@@ -0,0 +1,5 @@
1
+ export { default } from "./edit";
2
+ // I think a bug in remix means loader needs to be explicitly exported here
3
+ export { action, loader } from "./edit";
4
+ // For meta and links etc.
5
+ export * from "./edit";
@@ -0,0 +1,49 @@
1
+ import { Render, type Config } from "@measured/puck";
2
+ import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
3
+ import { json } from "@remix-run/node";
4
+ import { useLoaderData } from "@remix-run/react";
5
+
6
+ import puckConfig from "../../puck.config";
7
+ import { getPage } from "~/models/page.server";
8
+
9
+ /**
10
+ * Disable client-side JS - Optional
11
+ * If you know that your Puck content doesn't need react.
12
+ * Then you can disable JS for this route.
13
+ * @see https://remix.run/docs/en/main/guides/disabling-javascript
14
+ */
15
+
16
+ // export const handle = { hydrate: false };
17
+
18
+ export const loader = async ({ params }: LoaderFunctionArgs) => {
19
+ // Get path, and default to slash for root path.
20
+ const puckPath = params.puckPath || "/";
21
+ // Get puckData for this path, this could be a database call.
22
+ const puckData = getPage(puckPath);
23
+ if (!puckData) {
24
+ throw new Response(null, {
25
+ status: 404,
26
+ statusText: "Not Found",
27
+ });
28
+ }
29
+ // Return the data.
30
+ return json({ puckData });
31
+ };
32
+
33
+ export const meta: MetaFunction<typeof loader> = ({ data }) => {
34
+ const title = data?.puckData?.root?.title || "Page";
35
+
36
+ return [{ title }];
37
+ };
38
+
39
+ export default function Page() {
40
+ const { puckData } = useLoaderData<typeof loader>();
41
+
42
+ /**
43
+ * TypeStript error
44
+ * Type 'Config<Props>' is not assignable to type 'Config'. Use 'as Config' for now.
45
+ * @see https://github.com/measuredco/puck/issues/185
46
+ */
47
+
48
+ return <Render config={puckConfig as Config} data={puckData} />;
49
+ }
@@ -0,0 +1,70 @@
1
+ import { Puck, type Data, type Config } from "@measured/puck";
2
+ import styles from "@measured/puck/puck.css";
3
+ import type {
4
+ ActionFunctionArgs,
5
+ LinksFunction,
6
+ LoaderFunctionArgs,
7
+ MetaFunction,
8
+ } from "@remix-run/node";
9
+ import { json } from "@remix-run/node";
10
+ import { useLoaderData, useSubmit } from "@remix-run/react";
11
+ import invariant from "tiny-invariant";
12
+
13
+ import puckConfig from "../../puck.config";
14
+ import { getPage, setPage } from "~/models/page.server";
15
+
16
+ export const action = async ({ params, request }: ActionFunctionArgs) => {
17
+ const puckPath = params.puckPath || "/";
18
+ const formData = await request.formData();
19
+ const puckData = formData.get("puckData");
20
+
21
+ invariant(puckData, "Missing data");
22
+ invariant(typeof puckData === "string", "Invalid data");
23
+
24
+ setPage(puckPath, JSON.parse(puckData));
25
+
26
+ return json({ ok: true });
27
+ };
28
+
29
+ export const links: LinksFunction = () => [
30
+ { rel: "stylesheet", href: styles, id: "puck-css" },
31
+ ];
32
+
33
+ export const loader = async ({ params }: LoaderFunctionArgs) => {
34
+ const puckPath = params.puckPath || "/";
35
+ const initialData = getPage(puckPath) || {
36
+ content: [],
37
+ root: {},
38
+ };
39
+ return json({ puckPath, initialData });
40
+ };
41
+
42
+ export const meta: MetaFunction<typeof loader> = ({ data }) => {
43
+ const title = data?.initialData?.root?.title || "Untitled page";
44
+
45
+ return [{ title: `Editing: ${title}` }];
46
+ };
47
+
48
+ export default function Edit() {
49
+ const { initialData } = useLoaderData<typeof loader>();
50
+ const submit = useSubmit();
51
+
52
+ /**
53
+ * TypeStript error
54
+ * Type 'Config<Props>' is not assignable to type 'Config'. Use 'as Config' for now.
55
+ * @see https://github.com/measuredco/puck/issues/185
56
+ */
57
+
58
+ return (
59
+ <Puck
60
+ config={puckConfig as Config}
61
+ data={initialData}
62
+ onPublish={async (data: Data) => {
63
+ // Use form data here because it's the usual remix way.
64
+ let formData = new FormData();
65
+ formData.append("puckData", JSON.stringify(data));
66
+ submit(formData, { method: "post" });
67
+ }}
68
+ />
69
+ );
70
+ }
@@ -0,0 +1 @@
1
+ {"/":{"content":[{"type":"HeadingBlock","props":{"title":"Edit this page by adding /edit to the end of the URL","id":"HeadingBlock-1694032984497"}}],"root":{"props": {"title":""}}}}
@@ -0,0 +1,6 @@
1
+ node_modules
2
+
3
+ /.cache
4
+ /build
5
+ /public/build
6
+ .env
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "{{appName}}",
3
+ "version": "1.0.0",
4
+ "private": true,
5
+ "scripts": {
6
+ "build": "remix build",
7
+ "dev": "remix dev --manual",
8
+ "start": "remix-serve ./build/index.js",
9
+ "typecheck": "tsc"
10
+ },
11
+ "dependencies": {
12
+ "@measured/puck": "{{puckVersion}}",
13
+ "@remix-run/css-bundle": "^2.2.0",
14
+ "@remix-run/node": "^2.2.0",
15
+ "@remix-run/react": "^2.2.0",
16
+ "@remix-run/serve": "^2.2.0",
17
+ "isbot": "^3.6.8",
18
+ "react": "^18.2.0",
19
+ "react-dom": "^18.2.0"
20
+ },
21
+ "devDependencies": {
22
+ "@remix-run/dev": "^2.2.0",
23
+ "@remix-run/eslint-config": "^2.2.0",
24
+ "@types/react": "^18.2.20",
25
+ "@types/react-dom": "^18.2.7",
26
+ "eslint": "^8.38.0",
27
+ "typescript": "^5.1.6"
28
+ }
29
+ }
@@ -0,0 +1,25 @@
1
+ import type { Config } from "@measured/puck";
2
+
3
+ type Props = {
4
+ HeadingBlock: { title: string };
5
+ };
6
+
7
+ export const config: Config<Props> = {
8
+ components: {
9
+ HeadingBlock: {
10
+ fields: {
11
+ title: { type: "text" },
12
+ },
13
+ defaultProps: {
14
+ title: "Heading",
15
+ },
16
+ render: ({ title }) => (
17
+ <div style={{ padding: 64 }}>
18
+ <h1>{title}</h1>
19
+ </div>
20
+ ),
21
+ },
22
+ },
23
+ };
24
+
25
+ export default config;
@@ -0,0 +1,12 @@
1
+ /** @type {import('@remix-run/dev').AppConfig} */
2
+ export default {
3
+ ignoredRouteFiles: ["**/.*"],
4
+ // appDirectory: "app",
5
+ // assetsBuildDirectory: "public/build",
6
+ // publicPath: "/build/",
7
+ // serverBuildPath: "build/index.js",
8
+ /**
9
+ * @see https://github.com/measuredco/puck/issues/112
10
+ */
11
+ browserNodeBuiltinsPolyfill: { modules: { crypto: true } },
12
+ };
@@ -0,0 +1,2 @@
1
+ /// <reference types="@remix-run/dev" />
2
+ /// <reference types="@remix-run/node" />
@@ -0,0 +1,22 @@
1
+ {
2
+ "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
3
+ "compilerOptions": {
4
+ "lib": ["DOM", "DOM.Iterable", "ES2022"],
5
+ "isolatedModules": true,
6
+ "esModuleInterop": true,
7
+ "jsx": "react-jsx",
8
+ "moduleResolution": "Bundler",
9
+ "resolveJsonModule": true,
10
+ "target": "ES2022",
11
+ "strict": true,
12
+ "allowJs": true,
13
+ "forceConsistentCasingInFileNames": true,
14
+ "baseUrl": ".",
15
+ "paths": {
16
+ "~/*": ["./app/*"]
17
+ },
18
+
19
+ // Remix takes care of building everything in `remix build`.
20
+ "noEmit": true
21
+ }
22
+ }