Skip to content

Commit

Permalink
feat: use Streaming SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Ponjimon committed Apr 30, 2022
1 parent 7eec4f1 commit a8d7f3f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
5 changes: 5 additions & 0 deletions app/components/LazyLoaded.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const LazyLoaded = () => (
<span>I have been lazily loaded using Streaming SSR!</span>
);

export default LazyLoaded;
3 changes: 3 additions & 0 deletions app/components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Components

You can place your own components in this folder or just delete it. It only exists for demonstrating purposes only.
4 changes: 2 additions & 2 deletions app/entry.client.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hydrate } from 'react-dom';
import { hydrateRoot } from 'react-dom/client';
import { RemixBrowser } from '@remix-run/react';

hydrate(<RemixBrowser />, document);
hydrateRoot(document, <RemixBrowser />);
41 changes: 31 additions & 10 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
import { renderToString } from 'react-dom/server';
import { renderToReadableStream } from 'react-dom/server';
import type { EntryContext } from '@remix-run/cloudflare';
import { RemixServer } from '@remix-run/react';
import isbot from 'isbot';

export default function handleRequest(
export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) {
const markup = renderToString(
<RemixServer context={remixContext} url={request.url} />
);
const controller = new AbortController();

responseHeaders.set('Content-Type', 'text/html');
try {
const stream = await renderToReadableStream(
<RemixServer context={remixContext} url={request.url} />,
{
signal: controller.signal,
onError: error => {
responseStatusCode = 500;
console.error(error);
},
}
);

return new Response('<!DOCTYPE html>' + markup, {
status: responseStatusCode,
headers: responseHeaders,
});
if (isbot(request.headers.get('user-agent'))) {
await stream.allReady;
}

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

return new Response(stream, {
status: responseStatusCode,
headers: responseHeaders,
});
} catch (e) {
return new Response('<!doctype html><p>Loading...</p>', {
status: 500,
headers: { 'Content-Type': 'text/html' },
});
}
}
22 changes: 22 additions & 0 deletions app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { lazy, Suspense, useState } from 'react';

const LazyLoaded = lazy(async () => {
// Artificial delay
await new Promise(resolve => setTimeout(resolve, 1000));

return await import('../components/LazyLoaded');
});

export default function Index() {
const [showLazyLoaded, setShowLazyLoaded] = useState(false);

return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
<h1>Welcome to Remix</h1>
Expand Down Expand Up @@ -26,6 +37,17 @@ export default function Index() {
Remix Docs
</a>
</li>
<li>
{showLazyLoaded ? (
<Suspense fallback="Loading...">
<LazyLoaded />
</Suspense>
) : (
<button type="button" onClick={() => setShowLazyLoaded(true)}>
Click here to Lazy Load
</button>
)}
</li>
</ul>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@remix-run/cloudflare": "^1.4.3",
"@remix-run/cloudflare-pages": "^1.4.3",
"@remix-run/react": "^1.4.3",
"isbot": "^3.4.6",
"react": "^18.1.0",
"react-dom": "^18.1.0"
},
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"module": "es2020",
"lib": ["DOM", "DOM.Iterable", "ES2019"],
"isolatedModules": true,
"esModuleInterop": true,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4358,6 +4358,11 @@ isarray@1.0.0, isarray@~1.0.0:
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=

isbot@^3.4.6:
version "3.4.6"
resolved "https://registry.yarnpkg.com/isbot/-/isbot-3.4.6.tgz#e3dd70034b6a6fad043753de71cd96929a678015"
integrity sha512-EEi3SVCPB4WHtMBaAYzYLgCP7yG9qixseYCf3IG0Yc+howLia+XFPLTT1437rzeViLeDInKOXOdFhs9Fa/xgWQ==

isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
Expand Down

0 comments on commit a8d7f3f

Please sign in to comment.