Skip to content
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

Preload lang chunks #486

Closed
jeanphilippeds opened this issue Oct 14, 2019 · 20 comments · Fixed by #823
Closed

Preload lang chunks #486

jeanphilippeds opened this issue Oct 14, 2019 · 20 comments · Fixed by #823

Comments

@jeanphilippeds
Copy link

What problem does this feature solve?

Hello,

I'm working on a 30-lang website and it would be great to preload some lang chunks.

Ex:

Btw, is it possible to do so today?

Thanks,

This feature request is available on Nuxt community (#c319)
@ghost ghost added the cmty:feature-request label Oct 14, 2019
@rchl
Copy link
Collaborator

rchl commented Oct 14, 2019

This is how it works right now with lazyLoad enabled. Do you want to customize it more or don't know about existing behavior?

@jeanphilippeds
Copy link
Author

jeanphilippeds commented Oct 14, 2019

Indeed, it's linked to the lazyload feature. I can't disable it, otherwise it would bundle translations for 30 countries, having a huge impact on the bundle size.

@rchl
Copy link
Collaborator

rchl commented Oct 14, 2019

By preload you mean use the preload meta tag?

@jeanphilippeds
Copy link
Author

Exactly!

@jeanphilippeds
Copy link
Author

jeanphilippeds commented Oct 17, 2019

I managed to do it, although I'm not really proud of the solution:

In the layout:

if (process.client){
    import('~/path/to/translations/messages.en_GB.yml')
    import(`~/path/to/translations/messages.${__NUXT__.state.layout.locale}.yml`)
}

@rchl
Copy link
Collaborator

rchl commented Oct 20, 2019

I thought I've understood what you were requesting but your last comment has confused me again.

Apparently you are not talking about preload tags (https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content).

Lazy loading itself is loading all messages for the current locale and fallback locale so I'm not sure what are you missing exactly and how importing those yml files helps you.

@jeanphilippeds
Copy link
Author

I'll try to be clear enough:

Conditions:

  • I'm using Nuxt-i18n
  • lazy feature is activated

What is the problem?

  • On client side, when JS is executed, lang files are loaded sequentially:
    • First, the browser gets the fallback lang file (in my case: en_GB)
    • Then, it fetches the current locale lang file (let's say fr_FR)
      It has an impact on my website performance as the user is waiting for the server to answer, twice.

What solutions?

  • Use a preload tag to get those 2 files sooner. But I did not manage to configure it properly.
  • Use webpack function import() to:
    • Get the lang files when the JS execution starts.
    • The browser caches it so it's already loaded when it's needed later.

@rchl
Copy link
Collaborator

rchl commented Oct 21, 2019

Now I understand, thanks :)
Since fallback is always required, it would make sense to have it in the main bundle already.
For the current language, I'd try to add preload tags.

No idea how to do those things yet but will investigate at some point. :)

@stale
Copy link

stale bot commented Dec 20, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Dec 20, 2019
@stale stale bot closed this as completed Dec 27, 2019
@rchl rchl reopened this Dec 29, 2019
@stale stale bot removed the wontfix label Dec 29, 2019
@tvld

This comment has been minimized.

@tvld

This comment has been minimized.

@rchl

This comment has been minimized.

@tvld

This comment has been minimized.

@stale
Copy link

stale bot commented Mar 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 20, 2020
@stale
Copy link

stale bot commented Mar 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@rchl rchl removed the wontfix label Mar 20, 2020
@stale
Copy link

stale bot commented May 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 19, 2020
@rchl rchl removed the stale label May 20, 2020
@tvld
Copy link

tvld commented May 20, 2020

It's still a relevant issue. To avoid delay or screen flash for users that need the non-english, preload of the native might be preferable first... ))?

@stale
Copy link

stale bot commented Jul 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 19, 2020
@rchl rchl removed the stale label Jul 19, 2020
rchl added a commit that referenced this issue Aug 3, 2020
Instead of client importing and processing messages (translations)
itself on load pass already loaded messages from the server through
nuxtState. This will in most cases allow avoiding triggering an extra
network request to fetch lang file for given locale.

Slight behavior change: The client will no longer import the lang file
itself for the initially used locale. That means that if the lang file
has exported a function, it will only be called on the server and not
the client. This matches behavior of "asyncData" and I think it makes
more sense in general.

Resolves #486
Resolves #663
@rchl
Copy link
Collaborator

rchl commented Aug 3, 2020

With #823 we'll load translations on the server and pass that to the client so extra network requests (combined with #820) will be completely avoided.

That means that there is no need to add extra preload tags.

@rchl rchl closed this as completed in #823 Aug 3, 2020
rchl added a commit that referenced this issue Aug 3, 2020
Instead of client importing and processing messages (translations)
itself on load, pass already loaded messages from the server through
"nuxtState". This will in most cases allow avoiding triggering an extra
network request to fetch lang file for the given locale.

Slight behavior change: The client will no longer import the lang file
itself for the initially used locale. That means that if the lang file
has exported a function, it will only be called on the server and not on
the client. This matches behavior of "asyncData" and I think it makes
more sense in general.

Resolves #486
Resolves #663
@rchl
Copy link
Collaborator

rchl commented Aug 4, 2020

Released in v6.13.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants