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

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

Open
nolanlawson opened this issue Apr 16, 2024 · 1 comment
Labels
bug HMR Hot Module Replacement

Comments

@nolanlawson
Copy link
Contributor

nolanlawson commented Apr 16, 2024

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

@nolanlawson nolanlawson changed the title [HMR] swapStyle() API does not work when swapping back and forth multiple times [HMR] swapStyle() API does not work when swapping back and forth Apr 16, 2024
@nolanlawson nolanlawson changed the title [HMR] swapStyle() API does not work when swapping back and forth [HMR] swap* APIs do not work when swapping content back and forth Apr 16, 2024
@nolanlawson nolanlawson changed the title [HMR] swap* APIs do not work when swapping content back and forth [HMR] swap* APIs do not work when swapping back and forth multiple times Apr 16, 2024
@nolanlawson nolanlawson changed the title [HMR] swap* APIs do not work when swapping back and forth multiple times [HMR] swap* APIs show stale content when swapping back and forth multiple times Apr 16, 2024
nolanlawson added a commit to nolanlawson/lwc-1 that referenced this issue Apr 16, 2024
@nolanlawson
Copy link
Contributor Author

This might not actually be an issue in practice, since our HMR implementation should re-evaluate the stylesheet function every time, not based on its string content. TBD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug HMR Hot Module Replacement
Projects
None yet
Development

No branches or pull requests

1 participant