Skip to content

Add a DEV-only watcher for content files #797

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

Merged
merged 1 commit into from
Nov 6, 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
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