visionmedia / page.js

Micro client-side router inspired by the Express router
http://visionmedia.github.com/page.js
7.67k stars 687 forks source link

Page base not used for history state (breaks reloading in electron) #585

Closed joezappie closed 1 year ago

joezappie commented 3 years ago

I'm looking to use this with Electron Forge which spins up a dev server with the window name as the base URL: http://localhost:3000/main_window

Upon refreshing the app, it changes the URL to not include "/main_window" as the base URL. I've turned hashbang mode on as that is typically suggested for Electron and routers.

I then tried setting the base for page:

page.base('/main_window');
page('/');

That changes the URL to: http://localhost:3000/#!/

In the other issue referenced, I thought it was due to the page base being reset. After looking into the code it appears that assumption was wrong:

page.base('/main_window');
page('/');
console.log(page.base());

This properly logs "/main_window" so the base variable is still set.

I then added some logging to the "Context(path, state, pageInstance)" function in page.js. There is some code to remove the basePath from the URL:

var re = new RegExp('^' + escapeRegExp(pageBase));
this.path = path.replace(re, '') || '/';

I then noticed in the pushState function its just calling the path but not adding the pageBase back in.

Changing: hashbang && this.path !== '/' ? '#!' + this.path : this.canonicalPath);

To This: hashbang && this.path !== '/' ? page._getBase() + '#!' + this.path : this.canonicalPath);

Made it so reloading the application went back to the correct URL. Would this have any other adverse affects anywhere else? I do currently believe this is a bug as it seems the history should store the original URL and not remove parts of it. Please advise if this is an incorrect assumption or if theres a better way to fix this issue.

Originally posted by @jrj2211 in https://github.com/visionmedia/page.js/issues/494#issuecomment-773757793

joezappie commented 3 years ago

Alternatively, it appears that the canonicalPath is already the correct path that should be saved in the history state. Is there a reason '#!' + this.path is ever used over the canonicalPath? This produces the same output as much suggested changes above:

page._window.history.replaceState(this.state, this.title, this.canonicalPath);

joezappie commented 3 years ago

Are there still active maintainers for this repo?