0

I have a website with two pages (A and B). Both have some content which is loaded via renderings. A has a button which opens B in a new tab. A also has some frontend code which calls one of my APIs, which again calls an external API to fetch some data. This data will be displayed on page A. Page B does not rely on that data. All of that happens asynchronously and enables the user to click on the button which opens page B in a new tab.

The problem is although the new tab opens, page B doesn't load until page A has loaded all of the data.

While investigating this problem I figured out that the rendering on page B is not invoking the assigned controller action until the API on page A is done.

Does anyone have an idea on how to solve it?

I'm using sitecore 9.1.1 and I'm happy to provide any additional information if needed.


EDIT:

This is the controller action for page B which doesn't do a whole lot except reading some fields of the item. This action doesn't get called until the API call is done.

public ActionResult Dashboard()
{
    Item datasource = RenderingContext.Current.Rendering.Item;
    var viewModel = _clientOverviewService.GetPageBViewModel(datasource);
    return View(viewModel);
}

The API call basically comes down to this:

switch (httpType)
{
    case HttpType.Post:
        return await httpClient.PostAsync(uri.ToString(), new StringContent(jsonData, Encoding.UTF8, "application/json"));

    case HttpType.Get:
        return await httpClient.GetAsync(uri.ToString());

    ... 
}

This is the JS code where the API call is made. The BigApiCallURL is being transformed to the uri variable in the code block above:

const axiosInstance = await axiosInstanceGenerator('BigApiCallURL');
const response = await axiosInstance.post('', requestPayload);

UPDATE:

A similar behaviour can be found in Sitecore itself. When you open the Content Editor and open an item in the Experience Editor, it usually takes a couple of seconds for the Experience Editor to load. While it loads, click on a different item in the Content Editor to view it and you will notice the process actually being blocked. It will resume once the Experience Editor is done loading.

Maybe both issues are related to each other?

  • So the tab opens but is blank until the API call has finished? Or you can't click the button until the API call is finished? It might be good to show some code examples too. – Richard Seal May 19 at 13:40
  • Exactly. I added some code but wasn't quite sure what exactly you need. I hope it helps understanding my problem. – Flo May 20 at 6:40
  • @Flo, are you able to debug this code? If yes please put the debugger at page B controller and see what happens when call is made to open page B,Where it's getting blocked or delayed? Debugging, it can give you visibility over the reason of this issue.By the way as you mentioned it's not synchronous, so it shouldn't be dependent on Page A method returning call. – Ranjit choudhary May 20 at 16:58
  • @Ranjitchoudhary Yes, I'm able ot debug my code but I don't know how I would/could debug Sitecore internal code. I put a breakpoint on the first line of the controller action for page B and it doesn't get triggered until the API call from page A is finished. That's why I assume that there is some mechanism in Sitecore which blocks it. – Flo May 22 at 5:58
0

It looks like a problem with session state behavior. I have faced similar issue in Sitecore 8, when API calls from frontend came to queue and block one another. It was fixed by adding [SessionStateBehavior.ReadOnly] attribute on controller that I used for fetching data (to prevent blocking upcoming requests while actions are executed). Try to do the same for your controller.

[SessionState(SessionStateBehavior.ReadOnly)]
public class FetchDataController
{
 ...
}
| improve this answer | |
  • Thank you for your idea, but apparently it didn't solve the problem. In the meantime the provider of the external API optimized their code which reduced the time needed to fetch the data drastically. This only increases usability and doesn't solve the actual problem. I think it's a general Sitecore problem, since the same behaviour can be seen in Sitecore itself, as described in my latest edit. – Flo Jun 8 at 12:42
  • @Flo, is your controller action where you fetch data marked as async Task? – x3mxray Jun 8 at 14:44
  • yes it is and the call to the external API is marked with an await. – Flo Jun 9 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.