adobe / aem-spa-page-model-manager

Interface between Adobe Experience Manager and Single Page Application framework.
Apache License 2.0
33 stars 24 forks source link

[bug] ModelRouter cannot properly handle query params when page path has no extension #53

Closed dzmitry-kankalovich closed 3 years ago

dzmitry-kankalovich commented 3 years ago

Describe the bug We do URL rewrite on the AEM Dispatcher level, so our URLs and extension-less, e.g. /home. We recently noticed, that when you navigate to the page containing query param with the dot . - e.g. /home/search?q=some.param.with.dots, the router logic breaks unable to load the correct page model.

Package version 1.3.6

To Reproduce Assuming you have two valid AEM pages at /home and /home/search and home links out to /home/search

  1. Go to /home
  2. Click on the valid page link with some query params in it containing dots like /home/search?q=some.param.with.dots
  3. See blank page and an invalid model request in the Network tab: /home/search.?q=some.model.json?q=some.model

Expected behavior Page model should be loaded at /home/search.model.json regardless of what is in query params as long as it complies with URL spec.

Additional context We believe that the problem is in the getModelPath method of the ModelRouter component:

export function getModelPath(url?: string | null): string {
    const localUrl = url || window.location.pathname;

    // The default value model path comes as the the content path
    let endPosition = localUrl.indexOf('.');

    if (endPosition < 0) {
        // If the path is missing extension and has query params instead eg. http://zyx/abc?test=test
        const queryPosition = localUrl.indexOf('?');

        endPosition = (queryPosition < 0) ? localUrl.length : queryPosition;
    }

    return localUrl.substr(0, endPosition);
}

I think the method is better to rewrite to first strip-off all the query params, and then extract page path without .html bit explicitly, rather than rely on dots only.