-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from noclocks/develop
Develop
- Loading branch information
Showing
11 changed files
with
13,579 additions
and
8,566 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const appConfig = { | ||
youtube_video_id: "fdi_z9NLT_w?si=iSeO4L8snTfPbG5H", | ||
youtube_video_title: "Villard Bastien" | ||
}; |
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,19 +1,159 @@ | ||
import type { EntryContext } from "@remix-run/node"; | ||
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 { renderToString } from "react-dom/server"; | ||
import { config } from "dotenv"; | ||
import * as isbotModule from "isbot"; | ||
import { renderToPipeableStream } from "react-dom/server"; | ||
|
||
// Load .env variables | ||
config(); | ||
|
||
const ABORT_DELAY = 5_000; | ||
|
||
export default function handleRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext | ||
remixContext: EntryContext, | ||
loadContext: AppLoadContext, | ||
) { | ||
const prohibitOutOfOrderStreaming = | ||
isBotRequest(request.headers.get("user-agent")) || remixContext.isSpaMode; | ||
|
||
return prohibitOutOfOrderStreaming | ||
? handleBotRequest( | ||
request, | ||
responseStatusCode, | ||
responseHeaders, | ||
remixContext, | ||
) | ||
: handleBrowserRequest( | ||
request, | ||
responseStatusCode, | ||
responseHeaders, | ||
remixContext, | ||
); | ||
} | ||
|
||
// We have some Remix apps in the wild already running with isbot@3 so we need | ||
// to maintain backwards compatibility even though we want new apps to use | ||
// isbot@4. That way, we can ship this as a minor Semver update to @remix-run/dev. | ||
function isBotRequest(userAgent: string | null) { | ||
if (!userAgent) { | ||
return false; | ||
} | ||
|
||
// isbot >= 3.8.0, >4 | ||
if ("isbot" in isbotModule && typeof isbotModule.isbot === "function") { | ||
return isbotModule.isbot(userAgent); | ||
} | ||
|
||
// isbot < 3.8.0 | ||
if ("default" in isbotModule && typeof isbotModule.default === "function") { | ||
return isbotModule.default(userAgent); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function handleBotRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext, | ||
) { | ||
let html = renderToString( | ||
<RemixServer context={remixContext} url={request.url} /> | ||
); | ||
html = "<!DOCTYPE html>\n" + html; | ||
return new Response(html, { | ||
headers: { "Content-Type": "text/html" }, | ||
status: responseStatusCode, | ||
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); | ||
}); | ||
} |
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,10 @@ | ||
// This is a 'catch-all' route that will match any URL that hasn't already been matched by another route. | ||
// Navigate to the Home (index) route to see this route in action. | ||
|
||
import { redirect } from "@remix-run/react"; | ||
|
||
// Provide a `loader` function to handle the data fetching for this route. | ||
export const loader = async () => { | ||
// Redirect to the home page | ||
return redirect('/'); | ||
}; |
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,48 @@ | ||
declare global { | ||
interface Window { | ||
gtag: ( | ||
option: string, | ||
gaTrackingId: string, | ||
options: Record<string, unknown>, | ||
) => void; | ||
} | ||
} | ||
|
||
/** | ||
* @example | ||
* https://developers.google.com/analytics/devguides/collection/gtagjs/pages | ||
*/ | ||
export const pageview = (url: string, trackingId: string) => { | ||
if (!window.gtag) { | ||
console.warn( | ||
"window.gtag is not defined. This could mean your google analytics script has not loaded on the page yet.", | ||
); | ||
return; | ||
} | ||
window.gtag("config", trackingId, { | ||
page_path: url, | ||
}); | ||
}; | ||
|
||
/** | ||
* @example | ||
* https://developers.google.com/analytics/devguides/collection/gtagjs/events | ||
*/ | ||
export const event = ({ | ||
action, | ||
category, | ||
label, | ||
value, | ||
}: Record<string, string>) => { | ||
if (!window.gtag) { | ||
console.warn( | ||
"window.gtag is not defined. This could mean your google analytics script has not loaded on the page yet.", | ||
); | ||
return; | ||
} | ||
window.gtag("event", action, { | ||
event_category: category, | ||
event_label: label, | ||
value: value, | ||
}); | ||
}; |
Oops, something went wrong.