create-puck-app 0.12.0-canary.3f8ebc0 → 0.12.0-canary.53618f5

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.3f8ebc0",
3
+ "version": "0.12.0-canary.53618f5",
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,46 @@
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
+ ## Disabling JavaScript
41
+
42
+ This recipe can be adapted to disable JavaScript. See the [Remix docs](https://remix.run/docs/en/main/guides/disabling-javascript) for steps on how to do this.
43
+
44
+ ## License
45
+
46
+ 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,33 @@
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
+ } from "@remix-run/react";
11
+
12
+ export const links: LinksFunction = () => [
13
+ ...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
14
+ ];
15
+
16
+ export default function App() {
17
+ return (
18
+ <html lang="en">
19
+ <head>
20
+ <meta charSet="utf-8" />
21
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
22
+ <Meta />
23
+ <Links />
24
+ </head>
25
+ <body>
26
+ <Outlet />
27
+ <ScrollRestoration />
28
+ <Scripts />
29
+ <LiveReload />
30
+ </body>
31
+ </html>
32
+ );
33
+ }
@@ -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,34 @@
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
+ export const loader = async ({ params }: LoaderFunctionArgs) => {
10
+ // Get path, and default to slash for root path.
11
+ const puckPath = params.puckPath || "/";
12
+ // Get puckData for this path, this could be a database call.
13
+ const puckData = getPage(puckPath);
14
+ if (!puckData) {
15
+ throw new Response(null, {
16
+ status: 404,
17
+ statusText: "Not Found",
18
+ });
19
+ }
20
+ // Return the data.
21
+ return json({ puckData });
22
+ };
23
+
24
+ export const meta: MetaFunction<typeof loader> = ({ data }) => {
25
+ const title = data?.puckData?.root?.title || "Page";
26
+
27
+ return [{ title }];
28
+ };
29
+
30
+ export default function Page() {
31
+ const { puckData } = useLoaderData<typeof loader>();
32
+
33
+ return <Render config={puckConfig as Config} data={puckData} />;
34
+ }
@@ -0,0 +1,64 @@
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
+ return (
53
+ <Puck
54
+ config={puckConfig as Config}
55
+ data={initialData}
56
+ onPublish={async (data: Data) => {
57
+ // Use form data here because it's the usual remix way.
58
+ let formData = new FormData();
59
+ formData.append("puckData", JSON.stringify(data));
60
+ submit(formData, { method: "post" });
61
+ }}
62
+ />
63
+ );
64
+ }
@@ -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,34 @@
1
+ {
2
+ "name": "{{appName}}",
3
+ "version": "1.0.0",
4
+ "private": true,
5
+ "sideEffects": false,
6
+ "type": "module",
7
+ "scripts": {
8
+ "build": "remix build",
9
+ "dev": "remix dev --manual",
10
+ "start": "remix-serve ./build/index.js",
11
+ "typecheck": "tsc"
12
+ },
13
+ "dependencies": {
14
+ "@measured/puck": "{{puckVersion}}",
15
+ "@remix-run/css-bundle": "^2.2.0",
16
+ "@remix-run/node": "^2.2.0",
17
+ "@remix-run/react": "^2.2.0",
18
+ "@remix-run/serve": "^2.2.0",
19
+ "isbot": "^3.6.8",
20
+ "react": "^18.2.0",
21
+ "react-dom": "^18.2.0"
22
+ },
23
+ "devDependencies": {
24
+ "@remix-run/dev": "^2.2.0",
25
+ "@remix-run/eslint-config": "^2.2.0",
26
+ "@types/react": "^18.2.20",
27
+ "@types/react-dom": "^18.2.7",
28
+ "eslint": "^8.38.0",
29
+ "typescript": "^5.1.6"
30
+ },
31
+ "engines": {
32
+ "node": ">=18.0.0"
33
+ }
34
+ }
@@ -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
+ }