'Page loading skeleton - Spartacus

We would like to implement the page loading skeleton in our Spartacus project, however we have faced some issues. One every page load, the first two requests ('/basesites' and '/pages') are fired automatically by AuthModule.forRoot() before any code is run through storefront not allowing us to know if user is on the page and if the skeleton must be displayed until those requests are complete.

Does anyone have any experience of implementing loading skeleton in Spartacus and faced a similar issue when endpoints are called automatically before cx-storefront is run?



Solution 1:[1]

Sketelon UI before Angular app is bootstrapped

You can add your generic skeleton HTML inside the root component in the index.html file, for example:

...

<body>
  <app-root>...My generic skeleton HTML here...</app-root>
</body>

Angular will replace the content of this element, after the Angular's application is bootstraped (rendered dynamicaly for the first time).

Btw. Angular SSR works similarly - Angular replaces all the server-side-rendered HTML that was placed inside the root element, after Angular bootstraps the app dynamically in the browser.

Why Spartacus calls for /basesites and /pages are blocking the bootstrap of Angular app

First, let's clarify: those calls are not triggered by the AuthModule.forRoot().

Both calls /basesites and /pages happen before Angular bootstraps the app.

  • /basesites call is made during the Angular's APP_INITIAIZER(s) phase. By design, Angular app is not bootstrapped until all APP_INITIALIZER(s) resolve.

    The reason Spartacus fetches the CMS-driven config in this phase is that the rest of the app (services and components) depend on this dynamically loaded config.

  • /pages call is made during the Angular Routing Guards phase. And because Spartacus by default uses the Angular RouterModule with option initialNavigation: 'enabledBlocking' (alias: 'enabled'), Angular app is not bootstrapped until the first Routing Guards phase is resolved.

    The reason Spartacus uses the Angular Router's option initialNavigation: 'enabledBlocking' is to mitigate the UI flickering when CSR (client side rendering) kicks in after displaying the SSR (server side rendered) HTML.

Solution 2:[2]

grep "^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+" test.txt | awk -F" " '{print $1}' | sort | uniq -c

??? Pardon me?

Well, let's start with the regular expression:

^       : beginning of line
[0-9]\+ : a list of digits (at least one)
\.      : a dot

So, your line should start with a beginning of line (obvious), then a list of at least one digit, a dot, ... and this four times (but without the dot at the end).

Like this you have found your IP addresses.

Then you parse that, using a space as a field seperator (awk -F" "), and you show the first column '{print $1}'.

Now you have shown a list of IP addresses, which you would want to count.

Therefore you first sort them (sort) and once you're finished, the count the unique results (uniq -c).

Easy, isn't it? :-)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dominique