mirror of
https://github.com/BradNut/awesome-uses
synced 2025-09-08 17:40:31 +00:00
test cache
This commit is contained in:
parent
58bb3d3de8
commit
565452d4a8
5 changed files with 45 additions and 4 deletions
|
|
@ -4,7 +4,7 @@
|
|||
publish = "public"
|
||||
|
||||
[dev]
|
||||
command = "remix dev"
|
||||
command = "remix watch"
|
||||
port = 3000
|
||||
|
||||
[[redirects]]
|
||||
|
|
|
|||
7
raw.html
Normal file
7
raw.html
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -29,6 +29,7 @@ export default function Layout({ children }) {
|
|||
<p>
|
||||
Hosted on <a href="https://netlify.com">Netlify</a>
|
||||
</p>
|
||||
<p>Rendered Fresh</p>
|
||||
</center>
|
||||
</footer>
|
||||
</main>
|
||||
|
|
|
|||
|
|
@ -6,14 +6,40 @@ import { renderToPipeableStream } from 'react-dom/server';
|
|||
|
||||
const ABORT_DELAY = 5000;
|
||||
|
||||
type CachedResponse = {
|
||||
html:string;
|
||||
date: Date;
|
||||
}
|
||||
const cache = new Map<string, CachedResponse>();
|
||||
|
||||
|
||||
export default function handleRequest(
|
||||
request: Request,
|
||||
responseStatusCode: number,
|
||||
responseHeaders: Headers,
|
||||
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) => {
|
||||
let didError = false;
|
||||
const chunks: Uint8Array[] = [];
|
||||
|
||||
const { pipe, abort } = renderToPipeableStream(
|
||||
<RemixServer context={remixContext} url={request.url} />,
|
||||
|
|
@ -21,6 +47,15 @@ export default function handleRequest(
|
|||
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() });
|
||||
})
|
||||
|
||||
responseHeaders.set('Content-Type', 'text/html');
|
||||
|
||||
resolve(
|
||||
|
|
@ -39,7 +74,7 @@ export default function handleRequest(
|
|||
didError = true;
|
||||
|
||||
console.error(error);
|
||||
},
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -15,8 +15,6 @@ export const links: LinksFunction = () => [
|
|||
{ rel: 'stylesheet', href: styles },
|
||||
];
|
||||
|
||||
|
||||
|
||||
export function loader() {
|
||||
return {
|
||||
tags: tags(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue