Youth Camp | "Essential Development and Debugging Knowledge for Front-end" Notes

发表于 2022-01-20 14:30 1219 字 7 min read

cos avatar

cos

FE / ACG / 手工 / 深色模式强迫症 / INFP / 兴趣广泛养两只猫的老宅女 / remote

文章系统介绍了前端调试的多平台、多环境特点及常用工具与技巧,重点讲解了 Chrome DevTools 的功能,包括元素检查、控制台输出、源码调试、性能分析、网络请求监控等,并详细说明了移动端(iOS、Android)调试方法、代理抓包流程以及实用技巧如线上修改代码、跨域解决和“小黄鸭调试法”。

This article has been machine-translated from Chinese. The translation may contain inaccuracies or awkward phrasing. If in doubt, please refer to the original Chinese version.

Characteristics of Front-end Debugging

Multi-platform

Browsers, Hybrid apps, Node.js, Mini Programs, Desktop applications…

Multi-environment

Local development environment, production environment

Multi-tool

Chrome DevTools, Charles, Spy-Debugger, Whistle, vConsole…

Multi-technique

Console, BreakPoints, SourceMap, Proxies…

Chrome DevTools

Chrome DevTools is the built-in debugging tool of Google Chrome. It is extremely powerful, and many modern browsers have also adopted this debugging tool. It can dynamically add/remove styles and display them in real-time.

image.png

Forcing State Display

You can force the display of elements that only appear in certain states (such as hover, active, etc.)

image.png

You can also force element states or add pseudo-classes through the style panel on the right side

image.png

You can also filter styles

image.png

Screenshots

Yes, you can take screenshots of nodes, which is quite amazing!

image.png

image.png

Console

The console.log in JS is recorded here, which needs no introduction — everyone has used it.

However, one thing worth mentioning is that you can also output styled text for quick identification during debugging, such as:

console.log('%s %o,%c%s', 'hello', { name: '我是姓名', age: 18 }, 'font-size:20px; color:red; ', 'Welcome to bytedance!');

Here, %s outputs a string, %o outputs an object, and %c outputs styles (similar to C/C++ formatted output, but %c here is for styles).

image.png

console.table can visualize an array of objects in a very convenient way.

const persons = [
  { id: 1, name: '张三', age: 18, des: '好耶' },
  { id: 2, name: '李四', age: 24, des: '我是李四' },
  { id: 3, name: '王五', age: 20, des: '这是什么' },
];

image.png

Tree structure display with dir

image.png

Source Tab

As the name suggests, it displays the content of source code, where you can add breakpoints, perform step-by-step debugging, and more.

image.png

Executing debugger or setting a breakpoint at a certain line in the code will pause execution at that line, after which you can continue with step-by-step debugging. I won’t go into too much detail about the subsequent debugging — just try it hands-on and you’ll understand. (Those who have done backend debugging will find this very familiar, as it’s essentially setting a breakpoint.)

Other features include: XHR/fetch breakpoints that trigger when a request is made, adding breakpoints to element structures (for deletion, etc.), scope which lets you view the scope list (including closures), and CallStack which shows the current JavaScript call stack.

How to Debug Minified Code? SourceMap

Front-end code has an inherent “open source” nature. For security reasons, JavaScript code is typically minified by tools like webpack, resulting in code that is usually compressed into a single line with variables replaced by a, b, etc., making it completely unreadable. So how do we debug minified code?

When webpack bundles, it can additionally produce a Source Map program that maps the minified code back to the actual files. When an exception is thrown, it maps the error back to the original code. The Source Map is removed before going to production.

Performance

The Performance panel can generate reports, including:

image.png

image.png

Network

The panel for viewing network requests, inspecting request headers/responses, etc.

image.png

Application

This panel displays information related to local storage:

  • Local Storage
  • Session Storage
  • IndexedDB
  • Web SQL
  • Cookie

Tip: Clicking “Clear Site Data” in the Storage section of this panel will clear the local storage data for the webpage, eliminating the need to open browser settings to clear it.

image.png

Mobile H5 Debugging

Real Device Debugging

iOS

  1. Connect iPhone to Mac using a Lightning cable
  2. Enable Web Inspector on iPhone (Settings -> Safari -> Advanced -> Enable Web Inspector)
  3. Open the page to debug in Safari on the iPhone
  4. Open Safari on Mac for debugging (Menu Bar -> Develop -> iPhone Device Name -> Select the debug page)
  5. Debug in the Safari Developer Tools popup

If you don’t have an iPhone, you can install Xcode from the Mac App Store and use its built-in iOS simulator

Android

  1. Connect the phone to the computer using a USB cable
  2. Enable Developer Mode on the phone. Check USB Debugging and allow debugging
  3. Open Chrome browser on the computer, enter chrome://inspect/#devices in the address bar and check the “Discover USB devices” option
  4. Allow remote debugging on the phone and access the debug page
  5. Click the inspect button on the computer
  6. Enter the debugging interface

This method is generally not recommended; scanning a QR code directly on the phone provides a better experience

Proxy Debugging

Previously, you could use Fiddler to set up a proxy on your phone for packet capture. Now you can also use proxy tools to debug front-end pages on your phone. The principle is as follows:

  • The computer acts as a proxy server
  • The phone connects to the computer via HTTP proxy
  • All requests from the phone pass through the proxy server

The instructor used Charles as an example in class. I’ll use my previous phone packet capture experience instead, which has been personally verified. For detailed steps, see this blog post. You need to install a certificate to capture HTTPS requests: Using Fiddler for Android Phone Packet Capture

After configuring the proxy, you can access development environment pages from your phone! Other commonly used tools are shown below:

image.png

Common Development and Debugging Tips

Live Editing with Override

  1. Open Overrides under the Sources panel
  2. Click “Select folders for Overrides” and select a local empty folder directory
  3. Grant permission
  4. Modify the code in the page and save after modifications
  5. Open DevTools, click the three dots in the upper right corner -> More tools -> Changes, and you can see all modifications

The recording is very intuitive. Changes won’t disappear after refreshing, and you can also see what was modified.

Using Proxies to Solve Cross-Origin Issues During Development

Browsers have a built-in Same-origin policy. Requests from different origins will cause cross-origin issues, meaning that if any of the protocol, IP, or port number differs, cross-origin issues will occur.

Enabling Local Source Maps

When Source Maps don’t exist in production, you can use the Map Local network mapping feature to access local Source Map files.

Rubber Duck Debugging Method

Legend has it that a programming master always carried a rubber duck. When debugging code, he would place the rubber duck on the desk and explain every line of code in detail to the duck, and soon the problem would be located and fixed.

— “The Pragmatic Programmer”

So this is where it comes from! This is a great method for organizing your problem logic.

Summary and Reflections

Debugging in front-end development is a very important part. This lesson provided a very detailed explanation of using Chrome DevTools on PC and debugging for mobile development, which was very insightful.

Most of the content cited in this article comes from the instructor’s lecture, MDN, and an external blog post: Using Fiddler for Android Phone Packet Capture

喜欢的话,留下你的评论吧~

© 2020 - 2026 cos @cosine
Powered by theme astro-koharu · Inspired by Shoka