Skip to content

Commit

Permalink
feat(remix-island): setup (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Shayne Preston <shayne.preston@protonmail.com>
  • Loading branch information
prests and Shayne Preston authored Nov 30, 2024
1 parent 79aa718 commit 8eb98c9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"remix-hono": "^0.0.16",
"remix-island": "^0.2.0",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

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

30 changes: 30 additions & 0 deletions src/remix-app/Document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Links, Meta, Scripts, ScrollRestoration } from '@remix-run/react';
import { createHead } from 'remix-island';

import { useNonce } from './hooks/nonce';

import type { PropsWithChildren } from 'react';

const Head = createHead(() => (
<>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</>
));

const Document = ({ children }: PropsWithChildren) => {
const nonce = useNonce();

return (
<>
<Head />
{children}

<ScrollRestoration nonce={nonce ?? undefined} />
<Scripts nonce={nonce ?? undefined} />
</>
);
};

export { Document, Head };
2 changes: 1 addition & 1 deletion src/remix-app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { hydrateRoot } from 'react-dom/client';

startTransition(() => {
hydrateRoot(
document,
document.getElementById('root')!,
<StrictMode>
<RemixBrowser />
</StrictMode>
Expand Down
13 changes: 13 additions & 0 deletions src/remix-app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import { createReadableStreamFromReadable } from '@remix-run/node';
import { RemixServer } from '@remix-run/react';
import { isbot } from 'isbot';
import { renderToPipeableStream } from 'react-dom/server';
import { renderHeadToString } from 'remix-island';

import { Head } from './Document';
import NonceProvider from './hooks/nonce';

import type { AppLoadContext, EntryContext } from '@remix-run/node';

const CLOSING_HTML = '</div></body></html>' as const;

const generateOpeningHTML = (nonce: string, headStr: string) =>
`<!DOCTYPE html><html><head><meta charSet="utf-8" nonce="${nonce}" />${headStr}</head><body><div id="root">`;

const handleRequest = (
request: Request,
responseStatusCode: number,
Expand Down Expand Up @@ -36,6 +43,7 @@ const handleBotRequest = (
{
onAllReady() {
shellRendered = true;
const headStr = renderHeadToString({ request, remixContext, Head });
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

Expand All @@ -48,7 +56,9 @@ const handleBotRequest = (
})
);

body.write(generateOpeningHTML(loadContext.nonce, headStr));
pipe(body);
body.write(CLOSING_HTML);
},
onShellError(error: unknown) {
reject(error);
Expand Down Expand Up @@ -84,6 +94,7 @@ const handleBrowserRequest = (
{
onShellReady() {
shellRendered = true;
const headStr = renderHeadToString({ request, remixContext, Head });
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);

Expand All @@ -96,7 +107,9 @@ const handleBrowserRequest = (
})
);

body.write(generateOpeningHTML(loadContext.nonce, headStr));
pipe(body);
body.write(CLOSING_HTML);
},
onShellError(error: unknown) {
reject(error);
Expand Down
30 changes: 7 additions & 23 deletions src/remix-app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from '@remix-run/react';
import { Outlet } from '@remix-run/react';

import { useNonce } from './hooks/nonce';
import { Document } from './Document';
import stylexStylesheet from './main.css?url';

import type { LinksFunction } from '@remix-run/node';
Expand All @@ -12,27 +12,11 @@ const links: LinksFunction = () => [
},
];

const App = () => {
const nonce = useNonce();

return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>

<body>
<Outlet />

<ScrollRestoration nonce={nonce ?? undefined} />
<Scripts nonce={nonce ?? undefined} />
</body>
</html>
);
};
const App = () => (
<Document>
<Outlet />
</Document>
);

export default App;
export { links };

0 comments on commit 8eb98c9

Please sign in to comment.