-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
Does HMR actually work with universal-router? #42
Comments
Yeah it works |
I have to say it depends. Currently it's not easy to support HMR with static module import. In the react-starter-kit , it requires dynamic import to make HMR work:
When you change code in However, in cases with out dynamic import: //routes/index.js
import home from './home'
const routes = {
path: '/',
children: [
{
path: '/',
load: () => home,
}
]
}
// client.js
import routes from 'routes';
let router = new UniversalRouter(routes);
if (module.hot) {
module.hot.accept('./routes', () => {
const nextRotues = require('./routes').default;
// How can we replace routes in router ?
});
} The We can get new routes object with changed code in HMR callback, but how can we replace it in router instance? Currently I'm replace it manually: module.hot.accept('./routes', () => {
// eslint-disable-next-line global-require
const routes = require('./routes').default;
router.root = Array.isArray(routes) ? { path: '', children: routes, parent: null } : routes;
router.root.parent = null;
renderApp(history.location);
}); But I believe there should be better solution:
Both of them looks great for me, and personally I would prefer the solution 1 |
@kpaxqin why don't you want to create a new instance of router after hot update? // entry.js
import UniversalRouter from 'universal-router';
import routes from './routes';
let router = new UniversalRouter(routes);
// ...
if (module.hot) {
module.hot.accept('./routes', () => {
const nextRoutes = require('./routes').default;
router = new UniversalRouter(nextRoutes);
});
} or // router.js
import UniversalRouter from 'universal-router';
import routes from './routes';
export default new UniversalRouter(routes); // entry.js
import router from './router';
// ...
if (module.hot) {
module.hot.accept('./router');
} |
@frenzzy Actually it just resolve url and returns component, so its safe to re-create router instance. Thanks for your reply. |
Anyone knows how to intercept the dynamic route value as well as route query params? I can't find the info regarding how to obtain those values when it is using |
@adamchenwei if you are talking about react-starter-kit resolve route approach, then you can may use const route = {
path: '/page',
action(context) { // action method
context.pathname // or context.<any>
},
}; vs const route = {
path: '/page',
load: () => import('./route-action.js'),
};
// route-action.js
function action(context) { // the same action method as above
context.pathname // or context.<any>
}
export default action; You may pass any data to routes via router.resolve({
pathname: window.location.pathname,
dynamicValue: 'here',
}); |
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: