-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export the default 'handleRequest' from adapters (#204)
* feat: export the default 'handleRequest' from adapters * chore: re-export * chore: simplify demo server
- Loading branch information
Showing
13 changed files
with
145 additions
and
428 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -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' |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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' |
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,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 GitHub Actions / Unit tests (ubuntu-latest)
Check warning on line 16 in packages/remix-adapter/src/entry.server.tsx GitHub Actions / Unit tests (macOS-latest)
|
||
) { | ||
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) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { config } from './defaultRemixConfig' | ||
export type { GetLoadContextFunction, RequestHandler } from './server' | ||
export { createRequestHandler } from './server' | ||
export { handleRequest } from './entry.server' |
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,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 GitHub Actions / Unit tests (ubuntu-latest)
Check warning on line 11 in packages/remix-edge-adapter/src/entry.server.tsx GitHub Actions / Unit tests (macOS-latest)
|
||
) { | ||
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, | ||
}) | ||
} |
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