Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClaudeFlareでデプロイするための初期化に変更 #19

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ node_modules
/.cache
/build
.env
.dev.vars

.wrangler
33 changes: 20 additions & 13 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
# Welcome to Remix!
# Welcome to Remix + Cloudflare!

- 📖 [Remix docs](https://remix.run/docs)
- 📖 [Remix Cloudflare docs](https://remix.run/guides/vite#cloudflare)

## Development

Run the dev server:

```shellscript
```sh
npm run dev
```

## Deployment

First, build your app for production:
To run Wrangler:

```sh
npm run build
npm run start
```

Then run the app in production mode:
## Typegen

Generate types for your Cloudflare bindings in `wrangler.toml`:

```sh
npm start
npm run typegen
```

Now you'll need to pick a host to deploy it to.
You will need to rerun typegen whenever you make changes to `wrangler.toml`.

### DIY
## Deployment

If you're familiar with deploying Node applications, the built-in Remix app server is production-ready.
First, build your app for production:

Make sure to deploy the output of `npm run build`
```sh
npm run build
```

- `build/server`
- `build/client`
Then, deploy your app to Cloudflare Pages:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

デプロイ先をCloudflare Pagesにしたのは何で?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いや、無料枠が広いし、バックエンドをClaudeflareに上げる予定っぽいから合わせようかなって

```sh
npm run deploy
```

## Styling

Expand Down
143 changes: 23 additions & 120 deletions frontend/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
* For more information, see https://remix.run/file-conventions/entry.server
*/

import { PassThrough } from "node:stream";

import type { AppLoadContext, EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import type { AppLoadContext, EntryContext } from "@remix-run/cloudflare";
import { RemixServer } from "@remix-run/react";
import { isbot } from "isbot";
import { renderToPipeableStream } from "react-dom/server";

const ABORT_DELAY = 5_000;
import { renderToReadableStream } from "react-dom/server";

export default function handleRequest(
export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
Expand All @@ -24,117 +19,25 @@ export default function handleRequest(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
loadContext: AppLoadContext
) {
return isbot(request.headers.get("user-agent") || "")
? handleBotRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
)
: handleBrowserRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
);
}

function handleBotRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) {
return new Promise((resolve, reject) => {
let shellRendered = false;
const { pipe, abort } = renderToPipeableStream(
<RemixServer
context={remixContext}
url={request.url}
abortDelay={ABORT_DELAY}
/>,
{
onAllReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
);

pipe(body);
},
onShellError(error: unknown) {
reject(error);
},
onError(error: unknown) {
responseStatusCode = 500;
// Log streaming rendering errors from inside the shell. Don't log
// errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
}
);

setTimeout(abort, ABORT_DELAY);
});
}

function handleBrowserRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) {
return new Promise((resolve, reject) => {
let shellRendered = false;
const { pipe, abort } = renderToPipeableStream(
<RemixServer
context={remixContext}
url={request.url}
abortDelay={ABORT_DELAY}
/>,
{
onShellReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
);

pipe(body);
},
onShellError(error: unknown) {
reject(error);
},
onError(error: unknown) {
responseStatusCode = 500;
// Log streaming rendering errors from inside the shell. Don't log
// errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
}
);

setTimeout(abort, ABORT_DELAY);
const body = await renderToReadableStream(
<RemixServer context={remixContext} url={request.url} />,
{
signal: request.signal,
onError(error: unknown) {
// Log streaming rendering errors from inside the shell
console.error(error);
responseStatusCode = 500;
},
}
);

if (isbot(request.headers.get("user-agent") || "")) {
await body.allReady;
}

responseHeaders.set("Content-Type", "text/html");
return new Response(body, {
headers: responseHeaders,
status: responseStatusCode,
});
}
2 changes: 1 addition & 1 deletion frontend/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { LinksFunction } from "@remix-run/cloudflare";
import {
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";
import type { LinksFunction } from "@remix-run/node";

import "./tailwind.css";

Expand Down
2 changes: 1 addition & 1 deletion frontend/app/routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MetaFunction } from "@remix-run/node";
import type { MetaFunction } from "@remix-run/cloudflare";

export const meta: MetaFunction = () => {
return [
Expand Down
8 changes: 8 additions & 0 deletions frontend/functions/[[path]].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createPagesFunctionHandler } from "@remix-run/cloudflare-pages";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - the server build file is generated by `remix vite:build`
// eslint-disable-next-line import/no-unresolved
import * as build from "../build/server";

export const onRequest = createPagesFunctionHandler({ build });
9 changes: 9 additions & 0 deletions frontend/load-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { type PlatformProxy } from "wrangler";

type Cloudflare = Omit<PlatformProxy<Env>, "dispose">;

declare module "@remix-run/cloudflare" {
interface AppLoadContext {
cloudflare: Cloudflare;
}
}
20 changes: 13 additions & 7 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
"type": "module",
"scripts": {
"build": "remix vite:build",
"deploy": "pnpm run build && wrangler pages deploy",
"dev": "remix vite:dev",
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
"start": "remix-serve ./build/server/index.js",
"typecheck": "tsc"
"start": "wrangler pages dev ./build/client",
"typecheck": "tsc",
"typegen": "wrangler types",
"preview": "pnpm run build && wrangler pages dev",
"cf-typegen": "wrangler types"
},
"dependencies": {
"@remix-run/node": "^2.12.1",
"@remix-run/react": "^2.12.1",
"@remix-run/serve": "^2.12.1",
"@remix-run/cloudflare": "^2.12.0",
"@remix-run/cloudflare-pages": "^2.12.0",
"@remix-run/react": "^2.12.0",
"isbot": "^4.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "^2.12.1",
"@cloudflare/workers-types": "^4.20241004.0",
"@remix-run/dev": "^2.12.0",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.4",
Expand All @@ -35,7 +40,8 @@
"tailwindcss": "^3.4.4",
"typescript": "^5.1.6",
"vite": "^5.1.0",
"vite-tsconfig-paths": "^4.2.1"
"vite-tsconfig-paths": "^4.2.1",
"wrangler": "3.57.1"
},
"engines": {
"node": ">=20.0.0"
Expand Down
Loading
Loading