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

(wmr) - fix stale hmr module graph #427

Merged
merged 6 commits into from
Mar 14, 2021
Merged

(wmr) - fix stale hmr module graph #427

merged 6 commits into from
Mar 14, 2021

Conversation

JoviDeCroock
Copy link
Member

Resolves #426

This PR aims to point out what's going wrong here, let's look at a simple file-tree.

We got App.js, Header.js and store.js.

import { loadUser } from "./store";

loadUser();
render(<Header />, document.body);
import { useUser } from "./store";

export default function Header() {
  const user = useUser();

  if (!user) {
    return <main>Loading...</main>;
  }

  return <main>ok</main>;
}
const listeners = new Set();
let user;

export function useUser() {
  const [userState, setUserState] = useState(user);
  listeners.add(setUserState);
  return userState;
}

export function loadUser() {
  setTimeout(() => {
    setUser({ name: "toto" });
  }, 100);
}

function setUser(newUser) {
  user = newUser;
  listeners.forEach((listener) => listener(user));
}

When we make a change in store.js this will bubble up to both Header and App as they both use store.js but store doesn't accept its own updates. When this happens we go into our module-graph and mark store as stale so we know we have to cache bust this import by adding ?t=. However we eagerly unmarked modules as stale, this means that Header would import a stale copy while App would have the newest one.

@changeset-bot
Copy link

changeset-bot bot commented Mar 14, 2021

🦋 Changeset detected

Latest commit: 2af3aa7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wmr Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@JoviDeCroock JoviDeCroock requested a review from developit March 14, 2021 09:01
@JoviDeCroock JoviDeCroock changed the title (wmr) - avoid caching hmr (wmr) - fix stale hmr module graphq Mar 14, 2021
@JoviDeCroock JoviDeCroock changed the title (wmr) - fix stale hmr module graphq (wmr) - fix stale hmr module graph Mar 14, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Mar 14, 2021

Size Change: -87 B (0%)

Total Size: 690 kB

Filename Size Change
packages/wmr/demo/dist/about/index.html 657 B +1 B (0%)
packages/wmr/demo/dist/chunks/class-fields.********.js 201 B +1 B (+1%)
packages/wmr/demo/dist/chunks/compat.********.js 15.3 kB +1 B (0%)
packages/wmr/demo/dist/chunks/index.********.js 200 B -102 B (-34%) 🎉
packages/wmr/demo/dist/chunks/prerender.********.js 2.44 kB +2 B (0%)
packages/wmr/demo/dist/class-fields/index.html 635 B +1 B (0%)
packages/wmr/demo/dist/compat/index.html 1.49 kB +1 B (0%)
packages/wmr/demo/dist/env/index.html 714 B +2 B (0%)
packages/wmr/demo/dist/files/index.html 676 B +1 B (0%)
packages/wmr/demo/dist/index.********.js 7.14 kB +2 B (0%)
packages/wmr/demo/dist/index.html 704 B +1 B (0%)
packages/wmr/demo/dist/lazy-and-late/index.html 658 B +1 B (0%)
packages/wmr/wmr.cjs 657 kB +1 B (0%)
ℹ️ View Unchanged
Filename Size Change
packages/wmr/demo/dist/assets/Calendar.********.css 702 B 0 B
packages/wmr/demo/dist/assets/style.********.css 386 B 0 B
packages/wmr/demo/dist/error/index.html 646 B 0 B

compressed-size-action

Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

Love this! Can imagine that this took a while to debug 👍

@marvinhagemeister marvinhagemeister merged commit 24417f6 into main Mar 14, 2021
@marvinhagemeister marvinhagemeister deleted the no-cache-hmr branch March 14, 2021 09:56
@github-actions github-actions bot mentioned this pull request Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reload issue when modules are storing local references
2 participants