Skip to content

Commit

Permalink
fix: prevent dev from infinitely restarting (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone authored Jan 3, 2022
1 parent 562d3ad commit 3426c13
Show file tree
Hide file tree
Showing 2 changed files with 17 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`
17 changes: 12 additions & 5 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,12 @@ function useWorker(props: {
port,
} = props;
const [token, setToken] = useState<CfPreviewToken>();

// This is the most reliable way to detect whether
// something's "happened" in our system; We make a ref and
// mark it once we log our initial message. Refs are vars!
const startedRef = useRef(false);

useEffect(() => {
async function start() {
if (!bundle) return;
Expand All @@ -569,10 +575,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 @@ -624,7 +631,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 @@ -644,7 +650,6 @@ function useWorker(props: {
usageModel,
bindings,
modules,
token,
]);
return token;
}
Expand Down Expand Up @@ -681,6 +686,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 3426c13

Please sign in to comment.