diff --git a/.changeset/fast-bears-relate.md b/.changeset/fast-bears-relate.md new file mode 100644 index 0000000000..e18e3d7144 --- /dev/null +++ b/.changeset/fast-bears-relate.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/react": patch +--- + +Avoid DEV_ONLY_SetSnapshotEntryName on standalone lazy bundle. diff --git a/packages/react/runtime/__test__/snapshotPatch.test.jsx b/packages/react/runtime/__test__/snapshotPatch.test.jsx index 0c0e570eeb..bff2e9df66 100644 --- a/packages/react/runtime/__test__/snapshotPatch.test.jsx +++ b/packages/react/runtime/__test__/snapshotPatch.test.jsx @@ -1609,13 +1609,14 @@ describe('lazy snapshot', () => { const si = new SnapshotInstance(uniqueId); expect(si.type).toBe('https://example.com/main.lynx.bundle:snapshot-1'); }); - it('standalone lazy bundle snapshotCreatorMap set should not generate DEV_ONLY_AddSnapshot', () => { + it('standalone lazy bundle snapshotCreatorMap set should not generate DEV_ONLY_AddSnapshot and DEV_ONLY_SetSnapshotEntryName', () => { initGlobalSnapshotPatch(); expect(__globalSnapshotPatch).toMatchInlineSnapshot(`[]`); const globDynamicComponentEntry = 'https://example.com/main.lynx.bundle'; + const uniqID = `${globDynamicComponentEntry}:${'__snapshot_835da_eff1e_1'}`; - snapshotCreatorMap[`${globDynamicComponentEntry}:${'__snapshot_835da_eff1e_1'}`] = (uniqID) => { + snapshotCreatorMap[uniqID] = (uniqID) => { globalThis.createSnapshot( uniqID, /* v8 ignore start */ @@ -1636,6 +1637,10 @@ describe('lazy snapshot', () => { expect(__globalSnapshotPatch.length).toBe(0); + vi.stubGlobal('__JS__', true); + snapshotCreatorMap[uniqID](uniqID); + expect(__globalSnapshotPatch.length).toBe(0); + deinitGlobalSnapshotPatch(); expect(__globalSnapshotPatch).toMatchInlineSnapshot(`undefined`); }); diff --git a/packages/react/runtime/src/snapshot.ts b/packages/react/runtime/src/snapshot.ts index f12b4d7c38..53d9bc6778 100644 --- a/packages/react/runtime/src/snapshot.ts +++ b/packages/react/runtime/src/snapshot.ts @@ -238,7 +238,11 @@ export function createSnapshot( } // For Lazy Bundle, their entryName is not DEFAULT_ENTRY_NAME. // We need to set the entryName correctly for HMR - if (__DEV__ && __JS__ && __globalSnapshotPatch && entryName && entryName !== DEFAULT_ENTRY_NAME) { + if ( + __DEV__ && __JS__ && __globalSnapshotPatch && entryName && entryName !== DEFAULT_ENTRY_NAME + // `uniqID` will be `https://example.com/main.lynx.bundle:__snapshot_835da_eff1e_1` when loading a standalone lazy bundle after hydration. + && !uniqID.includes(':') + ) { __globalSnapshotPatch.push( SnapshotOperation.DEV_ONLY_SetSnapshotEntryName, uniqID,