Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Get Ready to Troubleshoot

Learning Objectives

After completing this unit, you'll be able to:

  • Explain the benefits of working with unminified code.
  • Demonstrate how to get unminified Lightning web components.
  • Locate Lightning web components JavaScript in DevTools.
  • Use Ignore List to focus on your code only.
  • Enable custom formatters for proxied decorators.

Lightning Web Components Have Unique JavaScript Challenges

Troubleshooting JavaScript is a skill of its own. Troubleshooting Lightning web components, built with JavaScript, takes that skill and builds on it. One of the first things you discover is that Lightning web components are compiled and minified. Minification makes them smaller and faster for the browser to load. But it also makes them challenging to troubleshoot. Minified code is especially hard to work with because line breaks are removed and variable names are changed. 

Before You Begin

The developer tools in most browsers have similar features. We focus on Chrome DevTools in this module. Let's set up an environment so you can explore DevTools.

We assume that you have your Salesforce DX development environment set up, and that you're comfortable using it to create Lightning web components and deploy them to an org. If you're not familiar with this process yet, complete the Quick Start: Lightning Web Components project before you continue in this module.

Follow Along with Trail Together

Want to follow along with experts as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live. You can find a link to the full session in the Resources section.

Set Up Your Troubleshooting Environment

First, you need to set up a Trailhead Playground with a couple of Lightning web components and prepare it for troubleshooting.

Ready to Get Hands-on with Lightning Web Components?

Create a new Trailhead Playground now to follow along and try out the steps in this module. Scroll to the bottom of this page, click the playground name, and then select Create Playground. It typically takes 3–4 minutes for Salesforce to create your Trailhead Playground. You also use the playground when it's time to complete the hands-on challenges.

Note

Yes, we really mean a brand-new Trailhead playground! If you use an existing org or playground, you can run into problems completing the challenges

Enable Debug Mode

This one step makes troubleshooting code much easier. Although the Lightning web components are still compiled, with Debug Mode enabled in the org, they're not minified. So, the variable names remain the same and the general structure of the code remains, making it much easier to troubleshoot.

  1. From Setup enter Debug Mode in the Quick Find box, and then select Debug Mode.
  2. Check the box next to your user.
  3. Click Enable.

Disable Caching During Development

Disable the secure and persistent browser caching setting to see the effect of any code changes without emptying the cache.

The caching setting improves page reload performance by avoiding extra round trips to the server.

  1. From Setup, enter Session in the Quick Find box, and then select Session Settings.
  2. Deselect the checkbox for Enable secure and persistent browser caching to improve performance.
  3. Click Save.

After disabling the browser caching setting, you can reload a page to test your code changes without clearing your cache. We recommend that you re-enable the browser caching setting after debugging your code.

Grab the Lightning Web Components from GitHub

  1. Complete the instructions in the GitHub repo readme.
  2. From the App Launcher “” in your playground, find and open Solutions (under Items).

Your environment is now ready to do some troubleshooting using the browser DevTools.

Check your understanding of key points from this section with these flashcards.

Read the question or term on each card, then click or tap the card to reveal the correct answer. Click the right-facing arrow to move to the next card, and the left-facing arrow to return to the previous card.

Open DevTools

Let's look at the Solutions app with the Chrome DevTools.

  1. From the App Launcher App Launcher in your playground, find and open Solutions (under Items).
  2. Right-click the browser window and select Inspect and then click the Sources tab.
Note

There are several ways to open the DevTools in the Chrome browser.

From the menu click Customize and control Google Chrome| More tools | Developer tools select Sources.

Press F12 or Ctrl+Shift+I (Windows) or Cmd+Option+I (macOS).

Solutions App with DevTools open. Callouts correspond to the description that follows.

DevTools contains the DevTools Menu (1) and, when the Sources tab is selected, the Sources panel, which contains:

  • The File Navigator pane (2) lists all files requested from this web page.
  • The Code Editor pane (3) displays files selected in the File Navigator.
  • The JavaScript Debugging pane (4) contains the Breakpoint control toolbar and several tools you can use to inspect JavaScript.

DevTools Menu

The DevTools menu has several options that allow you to view different parts of the browser. In this module, we work mostly with Sources and Console.

  • Use the Elements panel to view and change the DOM and CSS.
  • Use the Console panel to read messages such as those created with console.log(), interact with variables, and run JavaScript.
  • Use the Sources panel to view and debug JavaScript files.

Change the Location of DevTools

By default, the tools are docked to the right side of the screen. The tools can also be docked to the left or bottom of the screen or even undocked into a separate window.

Click Customize and control DevTools “” and select the dock side you want to use. (Images in this module show DevTools docked at the bottom.)

Dock side options buttons are Undock into separate window, Dock to left, Dock to bottom, and Dock to right.

Enable Custom Formatters

Lightning web components use JavaScript proxies to enforce that certain types of data are read-only, primarily data that's provisioned via decorators (@api, @track, @wire). In Debug Mode, custom formatters manipulate the proxies into readable parts. Then in Chrome DevTools, you see the real value instead of the proxy. Let's enable the custom formatters.

  1. In DevTools, click Settings “”. The Preferences page opens.
  2. Under Console, select Custom formatters.Console with Custom formatters selected.

Use Ignore List

Ignore List lets you ignore selected JavaScript files when using DevTools, so that you only pause on your own exceptions. This allows you to ignore framework code that you don't want to troubleshoot.

  1. In Settings, click Ignore List.
  2. Click Add pattern.
  3. Enter /components/.*.js$ and then click Add.
  4. Close Settings.

Locate the Lightning Web Components

Under Sources, the Lightning web components are displayed in the File Navigator (under Page). All of the custom Lightning web components for the current page are in the modules/c folder.

  1. In DevTools, click the Sources tab.
  2. In the File Navigator, under Page, expand lightning/n and then modules/c.
    File Navigator under Page, lighting/n and modules/c expanded.
  1. Click display.js. The file opens in the Code Editor.display.js screen
  1. In display.js find class Display. (Click inside the Code window and press Ctrl+F (Windows) or Cmd+F (macOS). In the Find box, enter class Display.)

Notice that the code is compiled, but not minified, so the variable names are not changed.

Note

If you see only a few lines of code in the JavaScript file, then Debug Mode is not enabled in the org. If enabling Debug Mode doesn't reset the code in the code editor, do a hard reload: With DevTools open, right-click the browser Reload button and then click Hard Reload

Check your understanding of key points from this section with these flashcards.

Read the question or term on each card, then click or tap the card to reveal the correct answer. Click the right-facing arrow to move to the next card, and the left-facing arrow to return to the previous card.

You start working with these new tools in the next unit.

Resources

在 Salesforce 帮助中分享 Trailhead 反馈

我们很想听听您使用 Trailhead 的经验——您现在可以随时从 Salesforce 帮助网站访问新的反馈表单。

了解更多 继续分享反馈