-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Cloudflare Workers adapter (#18)
* feat: add Cloudflare Workers adapter * update README * rename `adapters` to `handlers`
- Loading branch information
Showing
23 changed files
with
3,710 additions
and
2,144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/build | ||
.env | ||
.dev.vars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* By default, Remix will handle hydrating your app on the client for you. | ||
* 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` ✨ | ||
* For more information, see https://remix.run/file-conventions/entry.client | ||
*/ | ||
|
||
import { RemixBrowser } from '@remix-run/react' | ||
import { startTransition, StrictMode } from 'react' | ||
import { hydrateRoot } from 'react-dom/client' | ||
|
||
startTransition(() => { | ||
hydrateRoot( | ||
document, | ||
<StrictMode> | ||
<RemixBrowser /> | ||
</StrictMode> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* By default, Remix will handle generating the HTTP Response for you. | ||
* 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` ✨ | ||
* For more information, see https://remix.run/file-conventions/entry.server | ||
*/ | ||
|
||
import type { AppLoadContext, EntryContext } from '@remix-run/cloudflare' | ||
import { RemixServer } from '@remix-run/react' | ||
import { isbot } from 'isbot' | ||
import { renderToReadableStream } from 'react-dom/server' | ||
|
||
const ABORT_DELAY = 5000 | ||
|
||
export default async function handleRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext, | ||
// This is ignored so we can keep it in the template for visibility. Feel | ||
// free to delete this parameter in your app if you're not using it! | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
loadContext: AppLoadContext | ||
) { | ||
const controller = new AbortController() | ||
const timeoutId = setTimeout(() => controller.abort(), ABORT_DELAY) | ||
|
||
const body = await renderToReadableStream( | ||
<RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />, | ||
{ | ||
signal: controller.signal, | ||
onError(error: unknown) { | ||
if (!controller.signal.aborted) { | ||
// Log streaming rendering errors from inside the shell | ||
console.error(error) | ||
} | ||
responseStatusCode = 500 | ||
}, | ||
} | ||
) | ||
|
||
body.allReady.then(() => clearTimeout(timeoutId)) | ||
|
||
if (isbot(request.headers.get('user-agent') || '')) { | ||
await body.allReady | ||
} | ||
|
||
responseHeaders.set('Content-Type', 'text/html') | ||
return new Response(body, { | ||
headers: responseHeaders, | ||
status: responseStatusCode, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Outlet, Scripts } from '@remix-run/react' | ||
|
||
export function Layout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html lang='en'> | ||
<head> | ||
<meta charSet='utf-8' /> | ||
<meta name='viewport' content='width=device-width, initial-scale=1' /> | ||
</head> | ||
<body> | ||
{children} | ||
<Scripts /> | ||
</body> | ||
</html> | ||
) | ||
} | ||
|
||
export default function App() { | ||
return <Outlet /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { LoaderFunctionArgs } from '@remix-run/cloudflare' | ||
import { useLoaderData } from '@remix-run/react' | ||
|
||
export const loader = (args: LoaderFunctionArgs) => { | ||
const extra = args.context.extra | ||
const cloudflare = args.context.cloudflare | ||
return { cloudflare, extra } | ||
} | ||
|
||
export default function Index() { | ||
const { cloudflare, extra } = useLoaderData<typeof loader>() | ||
return ( | ||
<div> | ||
<h1>Remix and Hono</h1> | ||
<h2>Var is {cloudflare.env.MY_VAR}</h2> | ||
<h3> | ||
{cloudflare.cf ? 'cf,' : ''} | ||
{cloudflare.ctx ? 'ctx,' : ''} | ||
{cloudflare.caches ? 'caches are available' : ''} | ||
</h3> | ||
<h4>Extra is {extra}</h4> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { PlatformProxy } from 'wrangler' | ||
|
||
interface Env { | ||
MY_VAR: string | ||
} | ||
|
||
type GetLoadContextArgs = { | ||
request: Request | ||
context: { | ||
cloudflare: Omit<PlatformProxy<Env>, 'dispose' | 'caches' | 'cf'> & { | ||
caches: PlatformProxy<Env>['caches'] | CacheStorage | ||
cf: Request['cf'] | ||
} | ||
} | ||
} | ||
|
||
declare module '@remix-run/cloudflare' { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface AppLoadContext extends ReturnType<typeof getLoadContext> { | ||
// This will merge the result of `getLoadContext` into the `AppLoadContext` | ||
extra: string | ||
} | ||
} | ||
|
||
export function getLoadContext({ context }: GetLoadContextArgs) { | ||
return { | ||
...context, | ||
extra: 'stuff', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "example-cloudflare-workers", | ||
"private": true, | ||
"sideEffects": false, | ||
"type": "module", | ||
"scripts": { | ||
"build": "remix vite:build", | ||
"deploy": "npm run build && wrangler deploy", | ||
"dev": "remix vite:dev", | ||
"start": "wrangler dev", | ||
"typecheck": "tsc", | ||
"preview": "npm run build && wrangler dev" | ||
}, | ||
"dependencies": { | ||
"@remix-run/cloudflare": "^2.14.0", | ||
"@remix-run/react": "^1.19.3", | ||
"hono": "^4.6.9", | ||
"isbot": "^4.1.0", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"remix-hono": "^0.0.16" | ||
}, | ||
"devDependencies": { | ||
"@hono/vite-dev-server": "^0.16.0", | ||
"@remix-run/dev": "^2.14.0", | ||
"@types/react": "^18.2.20", | ||
"@types/react-dom": "^18.2.7", | ||
"typescript": "^5.1.6", | ||
"vite": "^5.1.0", | ||
"vite-tsconfig-paths": "^4.2.1", | ||
"wrangler": "^3.86.0" | ||
}, | ||
"engines": { | ||
"node": ">=20.0.0" | ||
} | ||
} |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// server/index.ts | ||
import { Hono } from 'hono' | ||
|
||
const app = new Hono<{ | ||
Bindings: { | ||
MY_VAR: string | ||
} | ||
}>() | ||
|
||
app.use(async(c, next) => { | ||
await next() | ||
c.header('X-Powered-By', 'Remix and Hono') | ||
}) | ||
|
||
app.get('/api', (c) => { | ||
return c.json({ | ||
message: 'Hello', | ||
var: c.env.MY_VAR | ||
}) | ||
}) | ||
|
||
|
||
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"include": [ | ||
"worker-configuration.d.ts", | ||
"env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx", | ||
"**/.server/**/*.ts", | ||
"**/.server/**/*.tsx", | ||
"**/.client/**/*.ts", | ||
"**/.client/**/*.tsx" | ||
], | ||
"compilerOptions": { | ||
"lib": [ | ||
"DOM", | ||
"DOM.Iterable", | ||
"ES2022" | ||
], | ||
"types": [ | ||
"@remix-run/cloudflare", | ||
"vite/client", | ||
"@cloudflare/workers-types/2023-07-01" | ||
], | ||
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"moduleResolution": "Bundler", | ||
"resolveJsonModule": true, | ||
"target": "ES2022", | ||
"strict": true, | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"~/*": [ | ||
"./app/*" | ||
] | ||
}, | ||
// Remix takes care of building everything in `remix build`. | ||
"noEmit": true | ||
} | ||
} |
Oops, something went wrong.