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 1 commit
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
4 changes: 4 additions & 0 deletions sandpack-react/src/Playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ export default {
title: "Intro/Playground",
};

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? Should the user do this too?


export const Basic: React.FC = () => {
return (
<div style={{ height: "400vh" }}>
<Sandpack
options={{
showTabs: true,
closableTabs: true,
experimental_enableServiceWorker: true,
experimental_enableStableServiceWorkerId: true,
}}
// customSetup={{
// dependencies: {
Expand Down
30 changes: 25 additions & 5 deletions sandpack-react/src/contexts/utils/useClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import type {
SandpackStatus,
} from "../..";
import { generateRandomId } from "../../utils/stringUtils";
import { useAsyncSandpackId } from "../../utils/useAsyncSandpackId";
import {
MAX_SANDPACK_ID_LENGTH,
useAsyncSandpackId,
} from "../../utils/useAsyncSandpackId";

import type { FilesState } from "./useFiles";

Expand Down Expand Up @@ -159,6 +162,26 @@ export const useClient: UseClient = (
}, timeOut);
}

const getStableServiceWorkerId = async () => {
const key = `SANDPACK_INTERNAL:URL-CONSISTENT-ID`;
const fixedId = localStorage.getItem(key);
if (fixedId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should do localStorage.setItem for the id if it turns out it's not set (with a random id). Then we don't have to ask the user to set it.

if (fixedId.length !== MAX_SANDPACK_ID_LENGTH) {
throw new Error(
`${key} must be ${MAX_SANDPACK_ID_LENGTH} characters long`
);
}

return fixedId;
}

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

return undefined;
};

const client = await loadSandpackClient(
iframe,
{
Expand All @@ -180,10 +203,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
6 changes: 3 additions & 3 deletions sandpack-react/src/utils/useAsyncSandpackId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useSandpackId = () => {
* For example, this id will be used to mount SW in the iframe
* so, to keep the URL valid, this must be an 9 character long string
*/
const MAX_ID_LENGTH = 9;
export const MAX_SANDPACK_ID_LENGTH = 9;

export const useAsyncSandpackId = (files: SandpackBundlerFiles) => {
if (typeof useReactId === "function") {
Expand All @@ -31,11 +31,11 @@ export const useAsyncSandpackId = (files: SandpackBundlerFiles) => {

return ensureLength(
sha.replace(/:/g, "sp").replace(/[^a-zA-Z]/g, ""),
MAX_ID_LENGTH
MAX_SANDPACK_ID_LENGTH
);
};
} else {
return () => ensureLength(generateRandomId(), MAX_ID_LENGTH);
return () => ensureLength(generateRandomId(), MAX_SANDPACK_ID_LENGTH);
}
};

Expand Down
Loading