Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz committed Sep 10, 2024
1 parent 2a8ab23 commit 7a6be6e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
2 changes: 0 additions & 2 deletions sandpack-react/src/Playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export default {
title: "Intro/Playground",
};

localStorage.setItem("SANDPACK_INTERNAL:URL-CONSISTENT-ID", "123123123");

export const Basic: React.FC = () => {
return (
<div style={{ height: "400vh" }}>
Expand Down
24 changes: 9 additions & 15 deletions sandpack-react/src/contexts/utils/useClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ export const useClient: UseClient = (
const debounceHook = useRef<number | undefined>();
const prevEnvironment = useRef(filesState.environment);

const experimental_stableServiceWorkerId = useAsyncSandpackId(
filesState.files
);
const asyncSandpackId = useAsyncSandpackId(filesState.files);

/**
* Callbacks
Expand Down Expand Up @@ -163,23 +161,19 @@ export const useClient: UseClient = (
}

const getStableServiceWorkerId = async () => {
const key = `SANDPACK_INTERNAL:URL-CONSISTENT-ID`;
const fixedId = localStorage.getItem(key);
if (fixedId) {
if (fixedId.length !== MAX_SANDPACK_ID_LENGTH) {
throw new Error(
`${key} must be ${MAX_SANDPACK_ID_LENGTH} characters long`
);
if (options?.experimental_enableStableServiceWorkerId) {
const key = `SANDPACK_INTERNAL:URL-CONSISTENT-ID`;
let fixedId = localStorage.getItem(key);

if (!fixedId) {
fixedId = await asyncSandpackId();
localStorage.setItem(key, fixedId);
}

return fixedId;
}

if (options?.experimental_enableStableServiceWorkerId) {
return await experimental_stableServiceWorkerId();
}

return undefined;
return await asyncSandpackId();
};

const client = await loadSandpackClient(
Expand Down

0 comments on commit 7a6be6e

Please sign in to comment.