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

JSON translations don't work in SSR Spartacus 4.x #14372

Closed
fgrecchi-aswatson opened this issue Nov 18, 2021 · 6 comments · Fixed by #15297
Closed

JSON translations don't work in SSR Spartacus 4.x #14372

fgrecchi-aswatson opened this issue Nov 18, 2021 · 6 comments · Fixed by #15297

Comments

@fgrecchi-aswatson
Copy link

Hello,

I am opening a bug after creating a topic here.

I have implemented the Lazy Loading of the translations as in this guide. If I run the application in SSR mode it always falls back in CSR, even if I increase the timeout to over 1 minute.
If I remove the backend configuration and restore the static files, the SSR works fine.
This issue occurs on both version 4.1 and 4.2 of Spartacus.

Steps to reproduce:

ng new electronics-spa --style=scss --package-manager yarn
cd electronics-spa/
ng add @spartacus/schematics@latest --baseUrl https://spartacus-demo.eastus.cloudapp.azure.com:8443 --baseSite=electronics-spa --currency=USD --language=en --ssr --skip-confirmation
cp ./node_modules/@spartacus/assets/i18n-assets ./src/assets -r

(I am using of course mine SAP Cloud baseUrl)

Change the file src/app/spartacus/spartacus-configuration.module.ts in this way (as per the documentation):

providers: [
  provideConfig({
    i18n: {
      backend: {
        loadPath: 'assets/i18n-assets/{{lng}}/{{ns}}.json',
      },
      chunks: translationChunksConfig,
    },
  }),
];

Then finally run

yarn build:ssr
yarn serve:ssr

And when you open the application in the browser it will fall back in the CSR mode without any error on server side.
If you revert the spartacus-configuration.module.ts and re-build and re-run the application, SSR works fine.

I have created a simple repo that reproduces the error: https://github.com/fgrecchi-aswatson/spartacus-ssr-issue

Thanks
Filippo Grecchi

@Xymmer Xymmer added localization SSR bug Something isn't working labels Nov 18, 2021
@Xymmer
Copy link
Contributor

Xymmer commented Nov 18, 2021

seeking feedback from dev time, will come back soon

@Platonn
Copy link
Contributor

Platonn commented Nov 19, 2021

No simple answer for this ticket from top of my head. I’ll need to dive and debug.
Many thanks @fgrecchi-aswatson for providing a REPO for reproducing!

@fgrecchi-aswatson
Copy link
Author

Hello,

a coworker of mine find out this possible solution related to i18next configuration

In projects/core/src/i18n/i18next/i18next-init.ts we added reloadInterval: 0 to the configuration object passed to i18next.init().

So here's the code:

...
          if (config.i18n.backend) {
            i18next.use(i18nextHttpBackend);
            const loadPath = getLoadPath(
              config.i18n.backend.loadPath,
              serverRequestOrigin
            );
            const backend = {
              loadPath,
              ajax: i18nextGetHttpClient(httpClient),
              reloadInterval: 0,
            };
            i18nextConfig = { ...i18nextConfig, backend };
          }
...

I hope this might help.
Filippo

@Platonn Platonn self-assigned this Nov 22, 2021
@Platonn Platonn added this to To Do: Next in Spartacus Bug Board [can close board?] via automation Nov 22, 2021
@Platonn Platonn moved this from To Do: Next to In Progress in Spartacus Bug Board [can close board?] Nov 22, 2021
@Platonn Platonn added the Major label Nov 24, 2021
@Platonn
Copy link
Contributor

Platonn commented Nov 24, 2021

I confirm it's a bug. The JSON translations work in CSR, but don't work OOTB in SSR:

  • for schematics-installed Spartacus 4.2.0
  • for schematics-installed Spartacus 3.4.5
  • for develop 4.2 (our repo)

To reproduce in our repo:
yarn build:assets
cp -r dist/assets/i18n-assets projects/storefrontapp/src/assets

config:

i18n: {
-    resources: translations,
+    backend: {
+          loadPath: 'assets/i18n-assets/{{lng}}/{{ns}}.json',
+    },
}

@Platonn Platonn changed the title Backend translations don't work in SSR JSON translations don't work in SSR Nov 25, 2021
@Platonn Platonn added this to To do in Architecture (Blamed) via automation Feb 3, 2022
@Platonn Platonn moved this from To Do to In Progress in Architecture (Blamed) Mar 2, 2022
@Platonn Platonn changed the title JSON translations don't work in SSR JSON translations don't work in SSR Spartacus 4.x Mar 3, 2022
@Platonn
Copy link
Contributor

Platonn commented Mar 4, 2022

Hi @fgrecchi-aswatson. I confirm, the fix is to set reloadInterval: false in the backend options of i18next
Unfortunately until #11675 is implemented, it's cumbersome for customers to apply quick fix themselves. Currently, to change any logic in the function 18nextInit() customers need to dismantle deeply the modules tree up to the I18nModule and then provide a custom factory function i18nextInit()

Detailed results of investigation:
In 4.0, when switching from the deprecated 3rd party dependency i18next-xhr-backend to its new counterpart i18next-http-backend we didn't notice that there are slight differences between those packages:

Since develop branch now is aimed to be released only as 5.0, we should backport both PRs #14453 and #15297 to release/4.3.x and then release a new 4.3.x patch.

This is a priority task now.

Platonn added a commit that referenced this issue Mar 8, 2022
Previously, the periodical reloading of JSON translations in SSR was enabled by default by `i18next-http-backend`. The never-ending async task `setInterval()` invoked inside `i18next-http-backend` caused the SSR to hang.

To prevent it, we're setting explicitly `reloadInterval: false` in the options of `i18next`.

fixes #14372
Platonn added a commit that referenced this issue Mar 14, 2022
…ort to 4.3.x] (#15319)

Previously, the periodical reloading of JSON assets in SSR was enabled by default by `i18next-http-backend`. The never-ending async task `setInterval()` invoked inside `i18next-http-backend` caused the SSR to hang.

To prevent it, we're setting explicitly `reloadInterval: false` in the options of `i18next`.

fixes #14372

it's a backport of PR #15297 to 4.3.x
@Platonn
Copy link
Contributor

Platonn commented Mar 28, 2022

For reference:
The fixes have been already released in v4.3.1. For more, see the commits history of release/4.3.x. branch.

@Xymmer Xymmer moved this from In Progress to Doc Input in Architecture (Blamed) Apr 20, 2022
@Xymmer Xymmer moved this from In Progress to Done in Spartacus Bug Board [can close board?] Apr 24, 2022
@Xymmer Xymmer added this to the 5.0 milestone Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working localization Major SSR
Projects
Development

Successfully merging a pull request may close this issue.

3 participants