Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM SPA Routing Issues with hash and query parameter

Avatar

Community Advisor

Hey guys,

 

We are using AEM with React and having trouble with SPA Routing. We are facing an intermittent issue when the Link contains # or ?

Page data is available using .model.json on the page URI but sometimes when we have # and ?, model.json is added after full URL rather than just path and hence it fails.

And it happens when on dispatcher only when we dont have .html in the URL.

E.g:

Lets say a React Link points to /en/mypage?cid=campaign

I expect data call for this to be  /en/mypage.model.json but sometimes it is sent as /en/mypage?cid=campaign.model.json

 

This logic is abstracted in the node module and hence difficult to check. Anyone else faced similar issue?

 

1 Accepted Solution

Avatar

Correct answer by
Level 2
17 Replies

Avatar

Level 2
Is this Client or Server Side react?

Avatar

Level 2
So you are using extension less url ? and without looking at your Apache rewriter rule, it is difficult to say whats wrong in the rule

Avatar

Community Advisor
Yes I am using extensionless URLs. Its not really an issue with Apache rewrite because the model.json request that is generated from the browser itself is incorrect(adding mode.json after full URL rather than just the path)).

Avatar

Level 2
Understood, are you using Adobe SDK or own to call model? if Adobe SDK then can you check what is the value of cq:pagemodel_root_url when you have this error, ideally it should have yourUrl.model.json and then React code reads it from there

Avatar

Community Advisor
That part is maintained in header and header doesnt change when we navigate between the pages. and it points only to root page which is fine..

Avatar

Level 2
this issue comes when first time page load or after page is loaded then when you navigate between links? It seems you have custom code to call model.json on every link click, it could be issue with react component state is not updated correctly, so if you post your code then will be better to upderstand

Avatar

Community Advisor

It is happening while routing from one page to another.. cq:pagemodel_root_url always has root page URL and not current page URL. And we are not using custom code to load page model json. Its just a Link tag with a URL and that automatically calls model.json internally

<Link to={myurl}>
Label
</Link>

 

Avatar

Level 2
that code is just <a> in React, but how React component is populating myurl? there must be some React component State properties which is read , check that code, also when you say autometically, there must be some code on componentDidMount() which triggers async call. Sorry without your further code I cannot help you.

Avatar

Community Advisor

We are doing like similar to https://helpx.adobe.com/experience-manager/kt/sites/using/getting-started-spa-wknd-tutorial-develop/...... Does it really matter how we populate the URL.. It is just a JSX variable and it is populated on what author puts in the component. When I say automatically, I mean whenever we pass a URL to React Link, AEM automatically makes a call to model.json of the link specified in to attribute.

 

There isnt anything in componentDidMount.. We just get props in mapped JS for the components and thats what we are using in React Link

 

Avatar

Community Advisor

@viveksachdeva 

Try playing around with the dipatcher re-write rules if it helps.

We guys also implemented similar thing but didn't faced such issue.

Avatar

Community Advisor
The URL modification happens at client side before it reaches Dispatcher. For "#" in the URL, we were facing this issue consistently but query params, it was intermittent.

Avatar

Level 2

We also have the same issue appearing, right after we started to rewrite URLs removing `.html` bit in a page path.

 

Looks to me that URL parsing code in the routing library contains a bug for the case of absent page extension and should be fixed.

Avatar

Community Advisor
Yeah. we had to change those links to normal links rather than React link..

Avatar

Correct answer by
Level 2

Avatar

Administrator
@dzmitryk7401781, thank you for sharing the solution with community. Keep doing the great work. Looking forward to see you more in the AEM community.


Kautuk Sahni

Avatar

Community Advisor
This is great... Will be very helpful for the ones who use SPA goign forward