Skip to content

Commit

Permalink
fix(sw): support fixed id value (#1197)
Browse files Browse the repository at this point in the history
* fix(sw): support fixed id value

* refactor

* revert

---------

Co-authored-by: codesandbox-bot <codesandbot@codesandbox.io>
  • Loading branch information
danilowoz and codesandbot authored Sep 10, 2024
1 parent 0ae65cc commit 1fb5a56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions sandpack-react/src/Playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const Basic: React.FC = () => {
options={{
showTabs: true,
closableTabs: true,
experimental_enableServiceWorker: true,
experimental_enableStableServiceWorkerId: true,
}}
// customSetup={{
// dependencies: {
Expand Down
25 changes: 18 additions & 7 deletions sandpack-react/src/contexts/utils/useClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,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 @@ -159,6 +157,22 @@ export const useClient: UseClient = (
}, timeOut);
}

const getStableServiceWorkerId = async () => {
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;
}

return await asyncSandpackId();
};

const client = await loadSandpackClient(
iframe,
{
Expand All @@ -180,10 +194,7 @@ export const useClient: UseClient = (
teamId,
experimental_enableServiceWorker:
!!options?.experimental_enableServiceWorker,
experimental_stableServiceWorkerId:
options?.experimental_enableStableServiceWorkerId
? await experimental_stableServiceWorkerId()
: undefined,
experimental_stableServiceWorkerId: await getStableServiceWorkerId(),
sandboxId,
}
);
Expand Down

0 comments on commit 1fb5a56

Please sign in to comment.