URL's search query not changing

karolis.stoncius 41 Reputation points
2022-05-31T14:03:12.713+00:00

Hi,
I'm trying to change the url's search query, but it's not updating. My custom SPFx web part calls this function:
207047-image.png
however, the url does not update. When testing locally on the workbench, it works fine.
I tried to listen for the "popstate" event and it does work, the "window.location.search" updates correctly. However the url in the address bar does not change.

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-06-09T09:13:27.47+00:00

    Hi @karolis.stoncius ,
    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [URL's search query not changing]

    Issue Symptom:
    Unable to update url by spfx web part in address bar

    Current status:
    Solved this by changing parent.location.href directly ( parent is the important part ), instead of using history.pushState.

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-06-01T06:16:25.31+00:00

    Hi @karolis.stoncius ,
    Per my test, you could try following code to update urls.

        export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorldWebPartProps> {  
          public onInit(): Promise<void> {  
            ((history) => {  
              var pushState = history.pushState;  
              history.pushState = (state, key, path) => {  
                  pushState.apply(history, [state, key, path]);  
                  this._onUrlChange(path);  
              };  
            })(window.history);  
        return Promise.resolve();  
      }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. karolis.stoncius 41 Reputation points
    2022-06-08T08:10:57.437+00:00

    Solved this by changing parent.location.href directly ( parent is the important part ), instead of using history.pushState.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.