7

Is the SF team planning on deprecating support for the window object?

If so is there a lwc way to get the current url for the loaded page of the component? I couldn't find anything going through the current documentation.

2 Answers 2

9

You can check Secure Wrappers and Locker API Viewer to get good idea - but it seems to have access and no documentation which says the access would be revoked.

However, the recommended way of getting page reference is through using NavigationMixin & CurrentPageReference.

Sample code JS:

import { LightningElement, wire, api, track } from 'lwc';
import { NavigationMixin, CurrentPageReference } from 'lightning/navigation';

export default class Poc extends NavigationMixin(LightningElement) {
    @wire(CurrentPageReference)
    getpageRef(pageRef) {
        console.log('data => ', JSON.stringify(pageRef));
    }
}

Output:

When above component was used in Aura component (Name:POC_AURA) which was loaded directly (isUrlAddressable interface), we get below object as logged:

{
  "type": "standard__component",
  "attributes": {
    "componentName": "c__POC_AURA"
  },
  "state": {
    "c__id": "some_id"
  }
}

When the same LWC component is put in record page of account, we get below logged:

{
  "type": "standard__recordPage",
  "attributes": {
    "objectApiName": "Account",
    "recordId": "0010K000028HCgAQAW",
    "actionName": "view"
  },
  "state": {

  }
}

You can refer to different pageReference types that you get.

3
  • 2
    This should be selected as the answer as it has salesforce recommended ways of getting the url. Commented Aug 30, 2019 at 16:23
  • Dude, you are the man! Thanks so much!
    – Arthlete
    Commented Aug 30, 2019 at 17:26
  • yes this is especially correct when accessing current url in public site or community
    – brahma
    Commented Feb 18, 2021 at 16:49
4

The best I can find is that when you use window, locker service returns a secureWindow object with some differences. You can find the differences here but it doesn't go into detail what those differences are.

You can test functionality in the locker console

1
  • 1
    Wow, thanks for this link, that's awesome! Never seen that before. I'm definitely bookmarking this.
    – Arthlete
    Commented Aug 30, 2019 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .