Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary hydration fix #1

Merged
merged 1 commit into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/entry.client.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {RemixBrowser} from '@remix-run/react';
import {hydrateRoot} from 'react-dom/client';

hydrateRoot(document, <RemixBrowser />);
hydrateRoot(document.getElementById('root')!, <RemixBrowser />);
29 changes: 28 additions & 1 deletion app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import type {EntryContext} from '@shopify/remix-oxygen';
import {RemixServer} from '@remix-run/react';
import {renderToReadableStream} from 'react-dom/server';
import {renderHeadToString} from 'remix-island';
import {Head} from './root';

const readableString = (value: string) => {
const te = new TextEncoder();
return new ReadableStream({
start(controller) {
controller.enqueue(te.encode(value));
controller.close();
},
});
};

export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
) {
const {readable, writable} = new TransformStream();
const head = readableString(
`<!DOCTYPE html><html><head>${renderHeadToString({
request,
remixContext,
Head,
})}</head><body><div id="root">`,
);
const end = readableString(`</div></body></html>`);

const body = await renderToReadableStream(
<RemixServer context={remixContext} url={request.url} />,
);

Promise.resolve()
.then(() => head.pipeTo(writable, {preventClose: true}))
.then(() => body.pipeTo(writable, {preventClose: true}))
.then(() => end.pipeTo(writable));

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

return new Response(body, {
return new Response(readable, {
status: responseStatusCode,
headers: responseHeaders,
});
Expand Down
17 changes: 11 additions & 6 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import type {Shop} from '@shopify/hydrogen/storefront-api-types';
import styles from './styles/app.css';
import favicon from '../public/favicon.svg';
import {createHead} from 'remix-island';

export const links: LinksFunction = () => {
return [
Expand All @@ -40,25 +41,29 @@ export async function loader({context}: LoaderArgs) {
return {layout};
}

export const Head = createHead(() => (
<>
<Meta />
<Links />
</>
));

export default function App() {
const data = useLoaderData<typeof loader>();

const {name} = data.layout.shop;

return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<>
<Head />
<body>
<h1>Hello, {name}</h1>
<p>This is a custom storefront powered by Hydrogen</p>
<Outlet />
<ScrollRestoration />
<Scripts />
</body>
</html>
</>
);
}

Expand Down
14 changes: 13 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"graphql": "^16.6.0",
"graphql-tag": "^2.12.6",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"remix-island": "^0.1.2"
},
"devDependencies": {
"@remix-run/dev": "1.12.0",
Expand Down