SAP / spartacus

Spartacus is a lean, Angular-based JavaScript storefront for SAP Commerce Cloud that communicates exclusively through the Commerce REST API.
Apache License 2.0
741 stars 388 forks source link

No OCC 'pages' call when changing the language on the login page #17732

Open skunkdome opened 1 year ago

skunkdome commented 1 year ago

Describe the bug No 'pages' occ call when changing the language on the login page

Tell us the version of Spartacus

To Reproduce Steps to reproduce the behavior:

  1. Go to 'https://spartacus-demo.eastus.cloudapp.azure.com/electronics-spa/en/USD/login'
  2. Open Network Tab in Browser
  3. Change Language
  4. No OCC 'pages' request to see

Expected behavior Would expect a OCC 'pages' request

Screenshots image

mateuszo commented 1 year ago

I found the cause of the problem and a possible workaround.

The cause

Pages data is refreshed on language change only when the route has a cmsRequired property set to true. And that property is true when a given route has a CmsPageGuard (link to the code). The problem is that login route has a LoginGuard which uses CmsPageGuard internally but this is not detected by the router.reducer.

Possible solutions

  1. Modify router.reducer so that it also checks for LoginGuard. This is the simplest one but I'm afraid that this bug will reappear with some other guard some day.
  2. Modify the LoginRouteModule and LoginGuard so that LoginGuard doesn't use CmsPageGuard and CmsPageGuard is listed explicitly in canActivate.
  3. Find some other way of defining cmsRequired property. Perhaps based on the routing config?

Workaround

  1. Remove import of LoginRouteModule from SpartacusFeaturesModule
  2. Instead, create and import a CustomLoginRouteModule that looks like this:
    @NgModule({
    imports: [
    PageLayoutModule,
    RouterModule.forChild([
      {
        // @ts-ignore
        path: null,
        canActivate: [LoginGuard, CmsPageGuard],
        component: PageLayoutComponent,
        data: { cxRoute: 'login' },
      },
    ]),
    ],
    })
    export class CustomLoginRouteModule {}

    This is not ideal because CmsPageGuard will be called twice but at least fixes the problem.