FredKSchott / esm-hmr

a Hot Module Replacement (HMR) API for your ESM-based dev server.
MIT License
413 stars 11 forks source link

Trouble staying on the same route #16

Closed chrisandrewca closed 4 years ago

chrisandrewca commented 4 years ago

Hi! Thanks for the hmr module, super cool.

I'm having trouble staying on the same route. What I mean is if I'm on /product and edit some code & save. I end up back at the root /.

I'm using plain JS with pushState + CustomEvent for location changes and onpopstate for forward/back buttons.

My app looks like

const App = async () => {

  const pages = [
    { path: /^[/]$/, load: async () => (await import('./home')).default },
    { path: /^[/]product$/, load: async () => (await import('./product')).default }
  ];

  onLocationChanged(async () => {
    const location = matchLocation(pages);
    const content = await location.page.load();

    render(content(), window.document.body);
  });

  replaceLocation('/');
};

export default App;

if (import.meta.hot) {
  import.meta.hot.accept();
}
chrisandrewca commented 4 years ago

Hah rubber ducking - it was the replaceLocation() that I'm using to kick off my rendering.... I'll need figure out another way to do that.