1

I want to do some DML operation when my lwc is removed from the DOM, so can anyone please confirm if disconnected callback will run on all below scenario

  1. on close tab.
  2. on browser close.
  3. got disconnected from internet.
  4. shut down the machine.

I know this might seem a silly question, but it is really important in my case.

  • I have tried all these on the playground but did not work, not sure about the SF platform(I doubt, also there is not enough documentation about disconnectedCallback()). Also in 3rd the point you won't be able to update the record if there is no internet connection. – rahul gawale Jun 3 at 12:57
  • Thank you for your response. – gs650x Jun 3 at 12:58
4

I checked this example disconnected callback gets called only when the parent component unloads it.

Check this playground

I have added if:true on the child component which is getting changed via checkbox on the parent. So whenever I uncheck the checkbox. The disconnected callback is getting called. But not when the events mentioned by you.

| improve this answer | |
  • 1
    Thank you Rahul for your response, useful information. – gs650x Jun 3 at 13:17
3

No, disconnectedCallback only works when the app is still loaded and the component is unloaded.

on close tab.

on browser close.

shut down the machine.

Use onunload to do any last second reactions. Note that the best you can probably do is store something in localStorage so you can pick up where you left off.

got disconnected from internet.

Use onconnect to figure out when you're back online (you can't save while offline).

| improve this answer | |
  • That's awesome!!, I could not find this in the docs. Can you share the link, please? – rahul gawale Jun 3 at 13:13
  • 3
    @rahulgawale You won't find this in the LWC docs, as this is just standard browser behavior. Try MDN instead. onunload and onconnect are both in the docs. – sfdcfox Jun 3 at 13:16
  • 1
    Thank you Brain for your response, would you please suggest if is there any way I can get information on how many users are currently using an LWC page within an org? – gs650x Jun 3 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.