Skip to content

Commit

Permalink
feat: export the default 'handleRequest' from adapters (#204)
Browse files Browse the repository at this point in the history
* feat: export the default 'handleRequest' from adapters

* chore: re-export

* chore: simplify demo server
  • Loading branch information
ascorbic authored Oct 30, 2023
1 parent b072d63 commit 12054d8
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 428 deletions.
331 changes: 25 additions & 306 deletions package-lock.json

Large diffs are not rendered by default.

81 changes: 1 addition & 80 deletions packages/demo-site/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,80 +1 @@
/**
* 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 { PassThrough } from 'node:stream'

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

const ABORT_DELAY = 5_000

export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
loadContext: AppLoadContext,
) {
const bot = isbot(request.headers.get('user-agent'))
return new Promise((resolve, reject) => {
let shellRendered = false
const body = new PassThrough()
const stream = createReadableStreamFromReadable(body)

const { pipe, abort } = renderToPipeableStream(
<RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />,
{
onShellReady() {
if (!bot) {
shellRendered = true
responseHeaders.set('Content-Type', 'text/html')
resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
}),
)
pipe(body)
}
},
onShellError(error: unknown) {
reject(error)
},
onAllReady() {
// Avoid a bug where responses aren't flushed if there's an outstanding timer.
clearTimeout(timer)
if (bot) {
shellRendered = true
responseHeaders.set('Content-Type', 'text/html')
resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
}),
)
pipe(body)
}
},
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)
}
},
},
)

const timer = setTimeout(() => {
abort()
}, ABORT_DELAY)
})
}
export { handleRequest as default } from '@netlify/remix-adapter'
7 changes: 2 additions & 5 deletions packages/demo-site/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import * as build from '@remix-run/dev/server-build'
import { createRequestHandler } from '@netlify/remix-adapter'
import type { Context } from '@netlify/functions'

const handle = createRequestHandler({
const handler = createRequestHandler({
build,
mode: process.env.NODE_ENV,
})

export default function handler(request: Request, context: Context) {
return handle(request, context)
}
export default handler
32 changes: 1 addition & 31 deletions packages/edge-demo-site/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
import type { AppLoadContext, EntryContext } from '@netlify/remix-runtime'
import { RemixServer } from '@remix-run/react'
import isbot from 'isbot'
import { renderToReadableStream } from 'react-dom/server'

export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
loadContext: AppLoadContext,
) {
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,
})
}
export { handleRequest as default } from '@netlify/remix-edge-adapter'
4 changes: 2 additions & 2 deletions packages/remix-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
},
"homepage": "https://github.com/netlify/remix-compute#readme",
"dependencies": {
"@remix-run/node": "^1.18.1"
"@remix-run/node": "^2.0.0",
"isbot": "^3.7.0"
},
"devDependencies": {
"@netlify/functions": "^2.3.0",
"@types/lambda-tester": "^3.6.1",
"lambda-tester": "^4.0.1"
},
"peerDependencies": {
Expand Down
74 changes: 74 additions & 0 deletions packages/remix-adapter/src/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { PassThrough } from 'node:stream'

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

const ABORT_DELAY = 5_000

export function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
loadContext: AppLoadContext,

Check warning on line 16 in packages/remix-adapter/src/entry.server.tsx

View workflow job for this annotation

GitHub Actions / Unit tests (ubuntu-latest)

'loadContext' is defined but never used

Check warning on line 16 in packages/remix-adapter/src/entry.server.tsx

View workflow job for this annotation

GitHub Actions / Unit tests (macOS-latest)

'loadContext' is defined but never used

Check warning on line 16 in packages/remix-adapter/src/entry.server.tsx

View workflow job for this annotation

GitHub Actions / Unit tests (windows-latest)

'loadContext' is defined but never used
) {
const bot = isbot(request.headers.get('user-agent'))
return new Promise((resolve, reject) => {
let shellRendered = false
const body = new PassThrough()
const stream = createReadableStreamFromReadable(body)

const { pipe, abort } = renderToPipeableStream(
<RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />,
{
onShellReady() {
if (!bot) {
shellRendered = true
responseHeaders.set('Content-Type', 'text/html')
resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
}),
)
pipe(body)
}
},
onShellError(error: unknown) {
reject(error)
},
onAllReady() {
// Avoid a bug where responses aren't flushed if there's an outstanding timer.
clearTimeout(timer)
if (bot) {
shellRendered = true
responseHeaders.set('Content-Type', 'text/html')
resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
}),
)
pipe(body)
}
},
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)
}
},
},
)

const timer = setTimeout(() => {
abort()
}, ABORT_DELAY)
})
}
1 change: 1 addition & 0 deletions packages/remix-adapter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { config } from './defaultRemixConfig'
export type { GetLoadContextFunction, RequestHandler } from './server'
export { createRequestHandler } from './server'
export { handleRequest } from './entry.server'
1 change: 1 addition & 0 deletions packages/remix-adapter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"jsx": "react-jsx",
"outDir": "./build"
},
"include": ["./src"]
Expand Down
3 changes: 2 additions & 1 deletion packages/remix-edge-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"homepage": "https://github.com/netlify/remix-compute#readme",
"dependencies": {
"@netlify/remix-runtime": "2.0.0"
"@netlify/remix-runtime": "2.0.0",
"isbot": "^3.7.0"
},
"devDependencies": {
"@remix-run/dev": "^2.0.0"
Expand Down
31 changes: 31 additions & 0 deletions packages/remix-edge-adapter/src/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { AppLoadContext, EntryContext } from '@netlify/remix-runtime'
import { RemixServer } from '@remix-run/react'
import isbot from 'isbot'
import { renderToReadableStream } from 'react-dom/server'

export async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
loadContext: AppLoadContext,

Check warning on line 11 in packages/remix-edge-adapter/src/entry.server.tsx

View workflow job for this annotation

GitHub Actions / Unit tests (ubuntu-latest)

'loadContext' is defined but never used

Check warning on line 11 in packages/remix-edge-adapter/src/entry.server.tsx

View workflow job for this annotation

GitHub Actions / Unit tests (macOS-latest)

'loadContext' is defined but never used

Check warning on line 11 in packages/remix-edge-adapter/src/entry.server.tsx

View workflow job for this annotation

GitHub Actions / Unit tests (windows-latest)

'loadContext' is defined but never used
) {
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,
})
}
1 change: 1 addition & 0 deletions packages/remix-edge-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import './globals'
export type { GetLoadContextFunction, RequestHandler } from './server'
export { createRequestHandler } from './server'
export { config } from './defaultRemixConfig'
export { handleRequest } from './entry.server'
3 changes: 2 additions & 1 deletion packages/remix-edge-adapter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "./build"
"outDir": "./build",
"jsx": "react-jsx"
},
"include": ["./src"]
// "references": [
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
},
"homepage": "https://github.com/netlify/remix-compute#readme",
"devDependencies": {
"@remix-run/server-runtime": "^1.18.1"
"@remix-run/server-runtime": "^2.1.0"
},
"peerDependencies": {
"@remix-run/server-runtime": "^1.18.1"
"@remix-run/server-runtime": "^2.0.0"
},
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit 12054d8

Please sign in to comment.