Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sw): support fixed id value #1197

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading