test cache

This commit is contained in:
Wes Bos 2023-02-21 14:23:59 -05:00
parent 58bb3d3de8
commit 565452d4a8
5 changed files with 45 additions and 4 deletions

View file

@ -4,7 +4,7 @@
publish = "public" publish = "public"
[dev] [dev]
command = "remix dev" command = "remix watch"
port = 3000 port = 3000
[[redirects]] [[redirects]]

7
raw.html Normal file

File diff suppressed because one or more lines are too long

View file

@ -29,6 +29,7 @@ export default function Layout({ children }) {
<p> <p>
Hosted on <a href="https://netlify.com">Netlify</a> Hosted on <a href="https://netlify.com">Netlify</a>
</p> </p>
<p>Rendered Fresh</p>
</center> </center>
</footer> </footer>
</main> </main>

View file

@ -6,14 +6,40 @@ import { renderToPipeableStream } from 'react-dom/server';
const ABORT_DELAY = 5000; const ABORT_DELAY = 5000;
type CachedResponse = {
html:string;
date: Date;
}
const cache = new Map<string, CachedResponse>();
export default function handleRequest( export default function handleRequest(
request: Request, request: Request,
responseStatusCode: number, responseStatusCode: number,
responseHeaders: Headers, responseHeaders: Headers,
remixContext: EntryContext remixContext: EntryContext
) { ) {
console.log(request.url);
// check if we have a cached response in memory
const cachedResponse = cache.get(request.url);
if (cachedResponse) {
// 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 (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) => { return new Promise((resolve, reject) => {
let didError = false; let didError = false;
const chunks: Uint8Array[] = [];
const { pipe, abort } = renderToPipeableStream( const { pipe, abort } = renderToPipeableStream(
<RemixServer context={remixContext} url={request.url} />, <RemixServer context={remixContext} url={request.url} />,
@ -21,6 +47,15 @@ export default function handleRequest(
onShellReady: () => { onShellReady: () => {
const body = new PassThrough(); 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() });
})
responseHeaders.set('Content-Type', 'text/html'); responseHeaders.set('Content-Type', 'text/html');
resolve( resolve(
@ -39,7 +74,7 @@ export default function handleRequest(
didError = true; didError = true;
console.error(error); console.error(error);
}, }
} }
); );

View file

@ -15,8 +15,6 @@ export const links: LinksFunction = () => [
{ rel: 'stylesheet', href: styles }, { rel: 'stylesheet', href: styles },
]; ];
export function loader() { export function loader() {
return { return {
tags: tags(), tags: tags(),