Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM SPA Routing Issues with hash and query parameter

viveksachdeva
Level 7
Level 7

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
dzmitryk7401781
Correct answer by
Level 2
Level 2
17 Replies
sumantap8752196
Level 2
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
viveksachdeva
Level 7
Level 7
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)).
sumantap8752196
Level 2
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
viveksachdeva
Level 7
Level 7
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..
sumantap8752196
Level 2
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
viveksachdeva
Level 7
Level 7

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>

 

sumantap8752196
Level 2
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.
viveksachdeva
Level 7
Level 7

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

 

Nikhil-Kumar
Community Advisor
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.

viveksachdeva
Level 7
Level 7
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.
dzmitryk7401781
Level 2
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.

viveksachdeva
Level 7
Level 7
Yeah. we had to change those links to normal links rather than React link..
dzmitryk7401781
Correct answer by
Level 2
Level 2
kautuk_sahni
Community Manager
Community Manager
@dzmitryk7401781, thank you for sharing the solution with community. Keep doing the great work. Looking forward to see you more in the AEM community.
viveksachdeva
Level 7
Level 7
This is great... Will be very helpful for the ones who use SPA goign forward