Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbos committed Feb 21, 2023
1 parent a9fd7d5 commit 72c7df8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 61 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"country-emoji": "^1.5.6",
"isbot": "^3.6.6",
"joi": "^17.8.1",
"normalize.css": "^8.0.1",
"prop-types": "^15.8.1",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 47 additions & 61 deletions src/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,84 +1,70 @@
import { PassThrough } from 'stream';
import type { EntryContext } from '@remix-run/node';
import { Response } from '@remix-run/node';
import { RemixServer } from '@remix-run/react';
import { renderToPipeableStream } from 'react-dom/server';
import { renderToReadableStream } from 'react-dom/server';

const ABORT_DELAY = 5000;

type CachedResponse = {
html:string;
html: string;
date: Date;
}
const cache = new Map<string, CachedResponse>();


export default function handleRequest(
export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) {
console.log('😆');
console.log(request.url);
// check if we have a cached response in memory
const cachedResponse = cache.get(request.url);
if (cachedResponse) {
console.log('Serving from cache', request.url);
// if we have a cached response, check if it's less than 5 seconds old
const now = new Date();
const diff = now.getTime() - cachedResponse.date.getTime();
if (true || diff < 5000) {
// if it's less than 5 seconds old, return the cached response
responseHeaders.set('Content-Type', 'text/html');
return new Response(cachedResponse.html, {
headers: responseHeaders,
status: responseStatusCode,
});
}
}


return new Promise((resolve, reject) => {
let didError = false;
const chunks: Uint8Array[] = [];
// // check if we have a cached response in memory
// const cachedResponse = cache.get(request.url);
// if (cachedResponse) {
// console.log('Serving from cache', request.url);
// // if we have a cached response, check if it's less than 5 seconds old
// const now = new Date();
// const diff = now.getTime() - cachedResponse.date.getTime();
// if (true || diff < 5000) {
// // if it's less than 5 seconds old, return the cached response
// responseHeaders.set('Content-Type', 'text/html');
// return new Response(cachedResponse.html, {
// headers: responseHeaders,
// status: responseStatusCode,
// });
// }
// }

const { pipe, abort } = renderToPipeableStream(
<RemixServer context={remixContext} url={request.url} />,
{
onShellReady: () => {
const body = new PassThrough();

body
.on('data', (data) => {
chunks.push(data);
})
.on('end', () => {
const html = Buffer.concat(chunks).toString('utf8');
cache.set(request.url, { html: html.replace('Rendered Fresh', `Served from Cache ${new Date().toString()}`), date: new Date() });
})
let didError = false;
const chunks: Uint8Array[] = [];

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

resolve(
new Response(body, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
);
const body = await renderToReadableStream(
<RemixServer context={remixContext} url={request.url} />,
{
onError: (error: unknown) => {
didError = true;
console.error(error);
}
}
);
console.log(body);

pipe(body);
},
onShellError: (err: unknown) => {
reject(err);
},
onError: (error: unknown) => {
didError = true;
// body
// .on('data', (data) => {
// console.log('data', data);
// chunks.push(data);
// })
// .on('end', () => {
// const html = Buffer.concat(chunks).toString('utf8');
// cache.set(request.url, { html: html.replace('Rendered Fresh', `Served from Cache ${new Date().toString()}`), date: new Date() });
// })

console.error(error);
}
}
);
const headers = new Headers(responseHeaders);
headers.set("Content-Type", "text/html");
new Response(body, {
headers,
status: didError ? 500 : responseStatusCode,
})

setTimeout(abort, ABORT_DELAY);
});
}

0 comments on commit 72c7df8

Please sign in to comment.