Skip to content

[HMR] swap* APIs show stale content when swapping back and forth multiple times #4154

Open
@nolanlawson

Description

@nolanlawson

The swapComponent/swapStyle/swapTemplate APIs do not work correctly in the following scenario (using swapStyle to illustrate):

/* initial state */ // works - A is rendered
swapStyle(a, b)     // works - B is rendered
swapStyle(b, a)     // works - A is rendered
swapStyle(a, b)     // does not work! A is still rendered

The reason for this is this line:

swappedStyleMap.set(oldStyle, newStyle);

The swappedStyleMap only ever grows – it never shrinks. So after swapping A and B a few times, we end up with the mappings:

A -> B
B -> A

Next, consider this code:

export function getStyleOrSwappedStyle(style: StylesheetFactory): StylesheetFactory {
assertNotProd(); // this method should never leak to prod
const visited: Set<StylesheetFactory> = new Set();
while (swappedStyleMap.has(style) && !visited.has(style)) {
visited.add(style);
style = swappedStyleMap.get(style)!;
}
return style;
}

There is a visited set to avoid infinite loops. However, if we have entries for both A -> B and B -> A, then calling this function with A will always return A. This is why the third swapStyle above doesn't work.

Related: #4115

Minimal repro: nolanlawson@3a135df

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    HMRHot Module Replacementbug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions