confirmed SSR support with Remix

This commit is contained in:
dswbx
2024-11-25 19:59:46 +01:00
parent 1c94777317
commit eea76ebc28
15 changed files with 144 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
import type { LoaderFunctionArgs } from "@remix-run/node";
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "@remix-run/react";
import { Links, Meta, Outlet, Scripts, ScrollRestoration, useLoaderData } from "@remix-run/react";
import { Api } from "bknd";
import { ClientProvider } from "bknd/ui";
@@ -22,15 +22,26 @@ export function Layout({ children }: { children: React.ReactNode }) {
}
export const loader = async (args: LoaderFunctionArgs) => {
args.context.api = new Api({
host: new URL(args.request.url).origin
const api = new Api({
host: new URL(args.request.url).origin,
headers: args.request.headers
});
return null;
// add api to the context
args.context.api = api;
return {
user: api.getAuthState().user
};
};
export default function App() {
const data = useLoaderData<typeof loader>();
// add user to the client provider to indicate
// that you're authed using cookie
return (
<ClientProvider>
<ClientProvider user={data.user}>
<Outlet />
</ClientProvider>
);

View File

@@ -9,8 +9,9 @@ export const meta: MetaFunction = () => {
export const loader = async (args: LoaderFunctionArgs) => {
const api = args.context.api as Api;
const user = api.getAuthState().user;
const { data } = await api.data.readMany("todos");
return { data };
return { data, user };
};
export default function Index() {

View File

@@ -6,6 +6,7 @@ import "bknd/dist/styles.css";
export default function AdminPage() {
const [loaded, setLoaded] = useState(false);
useEffect(() => {
if (typeof window === "undefined") return;
setLoaded(true);
}, []);
if (!loaded) return null;

View File

@@ -18,7 +18,8 @@
"bknd": "workspace:*",
"isbot": "^4.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"remix-utils": "^7.7.0"
},
"devDependencies": {
"@remix-run/dev": "^2.14.0",

Binary file not shown.