cancel
Showing results for 
Search instead for 
Did you mean: 

Intermittent Incorrect SSR Redirects (request and response mismatch in server.ts level)

ericyew94
Explorer
0 Kudos

I have a Spartacus/SAP Composable Storefront app that uses Angular SSR (express). What I am essentially trying to do is to perform a 301 redirect with the SSR where the replacement URL is meant to be assembled dynamically by calling an external API to retrieve necessary pieces for the replacement URL.

In a service class, I've injected the SSR's Response object with @Inject(RESPONSE). Also, within the same service, the URL redirect logic is done in the subscribe block of the Observable returned by the HTTP client call to the API mentioned earlier. In the subscribe block, I would set the SSR's response object's HTTP status to 301 and also its location with the newly assembled replacement URL.

Once the code has reached the res.render's callback block in the 'server.ts' snippet as shown below, res.redirect will be invoked.

 server.get('*', (req, res) => {
        res.render(indexHtml, {
            req,
            providers: [{provide: APP_BASE_HREF, useValue: req.baseUrl}],
        }, (err, html) => {
            if (res.statusCode === 301 || res.statusCode === 302) {
                res.redirect(res.statusCode, (res.header as any).REDIRECT_URL)
            } 

            //more logic here
        });
 });

All of this is working fine when there aren't much traffics. However, when there are a lot of concurrent requests to our Spartacus app, I realized that in the 'server.ts', the 'req' can sometimes get paired to a completely irrelevant 'res' from another request made to the same application. Hence, resulting in wrong 301 redirects.

At first I was suspecting that it could be an async issue(race condition) whereby I need to block the SSR render before the mentioned Observable is resolved. Thus, I have tried to implement an Angular resolver and placed the external HTTP call in the resolve method. Then, I have implemented the update of the SSR Response object's status and location in the ngOnInit method of the respective component by subscribing to the ActivatedRoute's data to access the data that is required to assemble the replacement URL. However, I am still seeing the same issue whereby the 'res' and 'req' would intermittently get mismatched..

I am wondering what could possibly cause this issue? It could either still be an async(race condition) issue as I might not have implemented the Angular resolver correctly (I am pretty new to Angular and FE development), or that this is a concurrency issue whereby the response from another request is interfering with the request from another thread/session.

I do appreciate some help and direction on this issue as I've been stuck for quite awhile..

Sandra_Rossi
Active Contributor
0 Kudos
Please do not post a question about SAP Web sites (community, SAP Support, etc.) if it's not a question about SAP Web sites! You can use Technology Q&A or any groups.
ericyew94
Explorer
0 Kudos

Hello @Sandra_Rossi, what I am asking is SAP Composable Storefront related. And I have double checked, the breadcrumb is showing that my question is in the Technology Q&A. Do let me know if I am still wrong, I will delete the question and try to ask at the right place. 

Sandra_Rossi
Active Contributor
0 Kudos

No, you're right. It's because a moderator or SAP employee has moved the question since my first comment.

Or there was a bug in the display yesterday.

Sandra_Rossi_0-1711456618664.png

(when I opened the question yesterday, the breadcrumb was showing "Questions about SAP Websites" too)

Accepted Solutions (0)

Answers (0)