Skip to content

[SSR] Client data fetching in beforeMount mixin double-fetches #9252

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

Closed
phil294 opened this issue Dec 25, 2018 · 3 comments
Closed

[SSR] Client data fetching in beforeMount mixin double-fetches #9252

phil294 opened this issue Dec 25, 2018 · 3 comments

Comments

@phil294
Copy link

phil294 commented Dec 25, 2018

Moved from vue-ssr-docs#210.

Version

2.5.21

Reproduction link

https://github.com/phil294/vue-hackernews-ssr-alternative-data-fetching

Steps to reproduce

In SSR docs, option 2, Fetch data after the matched view is rendered, calls asyncData in beforeMount.

The pseudo-syntax is

if (asyncData)
  asyncData()

This does not check if asyncData was already called by the server. Thus, guided by the official docs, this leads to duplicate fetches. In the case of dynamic module registration, even to duplicate modules.

In the linked repro, I cloned vue-hackernews and changed the fetch method accordingly. I also added a console.log to when fetchIdsByType is called which happens when asyncData is called.

What is expected?

Data is fetched on server, client does not fetch it again (as is the case with the usual router.beforeResolve solution (step 1).

What is actually happening?

Data is fetched twice, also on client, because there is no check on whether the data is already there, effectively making option 2 unusable.

@LinusBorg
Copy link
Member

This has to be solved in your business logic Since Vue won't decide for you how "fresh" you want your data to be. Certainly the example could be improved in this way, but that should be solved in the repository for the SSR docs (linked from within said docs):

https://github.com/vuejs/vue-ssr-docs

@phil294
Copy link
Author

phil294 commented Dec 26, 2018

@LinusBorg I had created this very issue @ ssr-docs repo already (210) where you adivced me to open it here instead. So according to you, there is no proper place to open this issue. I am confused. :-)

Regarding the issue itself: The usecase is: Prevent entry-client from calling asyncData hooks at page load. The data was already put into the DOM, there is no need to call asyncData in the frontend again; this would negate the meaning of SSR. I highly doubt this is business logic-dependent.

Now, in the docs, there are two approaches mentioned for client data fetching.

  • The "first approach" (as in HN demo etc) calls the asyncDataHooks in router.beforeResolve(...), thus preventing double-fetching.
  • The "second approach" (quoted in OP) calls the asyncDataHooks in beforeMount(...) via a global Mixin, which also happens on the initial route. This approach does not seem sane and needs to be wrapped somehow. That is the problem here.

Should we reopen the issue at ssr-docs then?
Merry Christmas!

@LinusBorg
Copy link
Member

Sorry about the confusion, i must have misinterpreted your original issue in the docs repository. I've reopened it, let's continue there

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

No branches or pull requests

2 participants