Skip to content

Commit

Permalink
fix: prevent dev from infinitely restarting
Browse files Browse the repository at this point in the history
In #164, we added `token` to `useWorker`'s `useEffect` array that detects when to start the server process. However, the effect itself sets `token`, so the build would inifinitely restart on every run.

We could potentially fix this by using `useMemo` or something, but that's overkill for the reason we use `token` - to detect whether it's the first or subsequent time we've started the server, and what message to log to the terminal. So we introduce a ref `startedRef` to play that role, and mark it as true when we log the message the first time. This fixes the infinite loop.

This bug shows how urgent it is to write tests for everything else. We'll get there.
  • Loading branch information
threepointone committed Jan 2, 2022
1 parent 1594036 commit 08cb3d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-candles-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: prevent `useWorker`'s inifinite restarts during `dev`
14 changes: 9 additions & 5 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ function useWorker(props: {
port,
} = props;
const [token, setToken] = useState<CfPreviewToken>();

const startedRef = useRef(false);

useEffect(() => {
async function start() {
if (!bundle) return;
Expand All @@ -566,10 +569,11 @@ function useWorker(props: {
return;
}

if (token) {
console.log("⎔ Detected changes, restarting server...");
} else {
if (!startedRef.current) {
console.log("⎔ Starting server...");
startedRef.current = true;
} else {
console.log("⎔ Detected changes, restarting server...");
}

const assets = sitesFolder
Expand Down Expand Up @@ -621,7 +625,6 @@ function useWorker(props: {
apiToken,
})
);
console.log(`⬣ Listening at http://localhost:${port}`);
}
start().catch((err) => {
// we want to log the error, but not end the process
Expand All @@ -641,7 +644,6 @@ function useWorker(props: {
usageModel,
bindings,
modules,
token,
]);
return token;
}
Expand Down Expand Up @@ -678,6 +680,8 @@ function useProxy({
},
});

console.log(`⬣ Listening at http://localhost:${port}`);

const server = proxy.listen(port);

// TODO(soon): refactor logging format into its own function
Expand Down

0 comments on commit 08cb3d5

Please sign in to comment.