Skip to content

Commit

Permalink
fix: use cwd for --experiment-enable-local-persistence (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone authored Apr 7, 2022
1 parent ed4f2ef commit 836ad59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .changeset/chatty-plants-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: use cwd for `--experiment-enable-local-persistence`

This sets up `--experiment-enable-local-persistence` to explicitly use `process.cwd() + wrangler-local-state` as a path to store values. Without it, local mode uses the temp dir that we use to bundle the worker, which gets wiped out on ending wrangler dev. In the future, based on usage, we may want to make the path configurable as well.

Fixes https://github.com/cloudflare/wrangler2/issues/766
20 changes: 16 additions & 4 deletions packages/wrangler/src/dev/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ function useLocalWorker({
const local = useRef<ReturnType<typeof spawn>>();
const removeSignalExitListener = useRef<() => void>();
const [inspectorUrl, setInspectorUrl] = useState<string | undefined>();
// if we're using local persistence for data, we should use the cwd
// as an explicit path, or else it'll use the temp dir
// which disappears when dev ends
const localPersistencePathOrDisableLocalPersistence = enableLocalPersistence
? // Maybe we could make the path configurable as well?
path.join(process.cwd(), "wrangler-local-state")
: // We have to explicitly choose not to use local persistence,
// since it defaults to true. That said, a benefit of just enabling
// it as is, would be that it would persist for all of
// `wrangler dev` surviving, which may be useful anyway.
// TODO: We should revisit this based on usage.
false;
useEffect(() => {
const abortController = new AbortController();
async function startLocalWorker() {
Expand Down Expand Up @@ -160,14 +172,14 @@ function useLocalWorker({
compatibilityDate,
compatibilityFlags,
kvNamespaces: bindings.kv_namespaces?.map((kv) => kv.binding),
kvPersist: enableLocalPersistence,
kvPersist: localPersistencePathOrDisableLocalPersistence,
durableObjects: Object.fromEntries(
(bindings.durable_objects?.bindings ?? []).map<[string, string]>(
(value) => [value.name, value.class_name]
)
),
durableObjectsPersist: enableLocalPersistence,
cachePersist: enableLocalPersistence,
durableObjectsPersist: localPersistencePathOrDisableLocalPersistence,
cachePersist: localPersistencePathOrDisableLocalPersistence,
sitePath: assetPaths?.assetDirectory
? path.join(assetPaths.baseDirectory, assetPaths.assetDirectory)
: undefined,
Expand Down Expand Up @@ -274,7 +286,7 @@ function useLocalWorker({
bindings.vars,
compatibilityDate,
compatibilityFlags,
enableLocalPersistence,
localPersistencePathOrDisableLocalPersistence,
assetPaths,
publicDirectory,
rules,
Expand Down

0 comments on commit 836ad59

Please sign in to comment.