Skip to content
Open
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
28 changes: 28 additions & 0 deletions app/AutoRefresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";

function AutoRefresh({ children }) {
return children;
}

if (process.env.NODE_ENV === "development") {
AutoRefresh = function AutoRefresh({ children }) {
const router = useRouter();
useEffect(() => {
const ws = new WebSocket("ws://localhost:3001");
ws.onmessage = (event) => {
if (event.data === "refresh") {
router.refresh();
}
};
return () => {
ws.close();
};
}, [router]);
return children;
};
}

export default AutoRefresh;
39 changes: 21 additions & 18 deletions app/layout.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import Link from "./Link";
import HomeLink from "./HomeLink";
import AutoRefresh from "./AutoRefresh";
import { serif } from "./fonts";
import "./global.css";

export default function RootLayout({ children }) {
return (
<html lang="en" className={serif.className}>
<body className="mx-auto max-w-2xl bg-[--bg] px-5 py-12 text-[--text]">
<header className="mb-14 flex flex-row place-content-between">
<HomeLink />
<span className="relative top-[4px] italic">
by{" "}
<Link href="https://danabra.mov" target="_blank">
<img
alt="Dan Abramov"
src="https://pbs.twimg.com/profile_images/1545194945161707520/rqkwPViA_400x400.jpg"
className="relative -top-1 mx-1 inline h-8 w-8 rounded-full"
/>
</Link>
</span>
</header>
<main>{children}</main>
</body>
</html>
<AutoRefresh>
<html lang="en" className={serif.className}>
<body className="mx-auto max-w-2xl bg-[--bg] px-5 py-12 text-[--text]">
<header className="mb-14 flex flex-row place-content-between">
<HomeLink />
<span className="relative top-[4px] italic">
by{" "}
<Link href="https://danabra.mov" target="_blank">
<img
alt="Dan Abramov"
src="https://pbs.twimg.com/profile_images/1545194945161707520/rqkwPViA_400x400.jpg"
className="relative -top-1 mx-1 inline h-8 w-8 rounded-full"
/>
</Link>
</span>
</header>
<main>{children}</main>
</body>
</html>
</AutoRefresh>
);
}
Loading