mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
confirmed SSR support with Remix
This commit is contained in:
@@ -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>
|
||||
);
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
Reference in New Issue
Block a user