-
Notifications
You must be signed in to change notification settings - Fork 238
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
Replace state on client happens after beforeEach router hook #105
Comments
This is the exact issue I've been trying to figure out as well. I would love to get some help with this. |
@Rufio2031 I am still waiting for an answer but my example works perfect for me |
Thanks @oshnix , I'll try that out tonight. This seems like this is such a fundamental issue but I'm having a hard time finding other people with the same issue and when I do, it seems they also don't have answers either. Does your main.js have access to window? If I remember correctly I'd have to pass it in to the |
@Rufio2031 That's a strange question because |
@oshnix sorry I thought I followed up but I guess I didn't. I was able to get this to work following your code and I wanted to thank you for sharing your solution. At this point I don't care how good it looks :) You're right, I was able to access window from my |
I just used However, I think the best solution is to create the router itself inside the |
@MicroDroid Good thing about |
@oshnix I was saying that instead of defining the I find giving createApp(window.__INITIAL_STATE__);` And your server one is: createApp(null); where passing |
Alternatively you can do as follows: // app.js
export default function createApp() {
const store = createStore();
const router = createRouter(store); // <--- pass `store` to `createRouter` function
const app = new Vue({
router,
store,
render: h => h(App),
});
return { app, router, store };
} // router.js
export default function createRouter(store) {
const router = new Router({
mode: "history",
routes,
});
router.beforeEach((to, from, next) => {
// In the client side get the state from window.__INITIAL_STATE__.
// Because it's not available in client side store at this moment.
const { user } = process.browser ? window.__INITIAL_STATE__ : store.state;
// user is available on both client and server side now
next();
});
return router;
} |
Another approach.
and in entry-server.js:
and in entry-client.js:
This way, store is replaced with the state from ssr before creating the app. |
I want some unauthorized users to prevent from visiting pages on site.
I added beforeEach hook for each route that looks like
And it works for me just fine but when it comes to user side it looks like this hook work before replacing local store state with server side fetched data.
So the page always redirects me to error and I get plenty of warnings about DOM tree not matching server rendered DOM.
The only idea I have is to replace state just after creating store so createApp function will look something like this:
But it doesn't look good. If there's a way to do it right?
The text was updated successfully, but these errors were encountered: