Safari Developer Tools Help Debug Websites and Apps
Introduction
Safari comes packed with powerful built-in developer tools for inspecting, debugging, and modifying both websites and iOS/iPadOS apps. Learning to leverage Safari's developer tools can help you identify and diagnose issues much faster compared to guessing blindly. Whether you are working on a website, web app, or native iOS app, Safari has you covered with its versatile toolset for peeking under the hood.
In this overview, we'll explore the key components of Safari's developer tools and how to use them for debugging websites and mobile apps. You'll learn how to inspect and modify rendered HTML/CSS, step through JavaScript execution, audit for performance/accessibility issues, simulate mobile devices, and more. By the end, you'll have a solid understanding of how to get the most out of Safari's built-in dev tools to build better experiences. Let's dig in!
Key Components of Safari Developer Tools
Safari's developer tools consist of several panels and features like the Web Inspector, Debugger, Console, Resources tab, Storage Inspector, and Audits. Here's a quick rundown of what each one does:
Web Inspector
The Web Inspector is for inspecting and modifying the rendered HTML and CSS on a webpage. Its key features include:
- View and edit HTML, CSS styles in real-time
- Inspect computed styles, layout details, accessibility tree
- Make changes directly in the Elements panel
- Audit for unused CSS, invalid HTML, contrast issues
- Works for websites and web-based apps
For example, you can tweak CSS values live and see the changes reflected instantly on the page. This is invaluable for debugging layout and styling issues visually.
Debugger
The Debugger is for debugging JavaScript code. It lets you:
- Set breakpoints to pause execution
- Step through code line-by-line
- View call stack, scope variables, event listeners
- Profile CPU usage and memory allocation
- Support debugging websites and native apps
The Debugger makes it easy to halt code execution to examine variable values, track down bugs, and step through your JavaScript functions one line at a time.
Console
The Console allows you to:
- Log diagnostic messages during testing
- Run JavaScript commands and inspect results
- View network requests, errors, warnings
- Quickly test snippets of JS code
- Inspect and modify DOM elements/styles
The Console is perfect for outputting variable values to check logic and test implementations quickly within the browser.
Resources
The Resources panel helps:
- Analyze page load performance
- Audit for unused JavaScript or blocking requests
- View network requests waterfall timeline
- Inspect cookie/local storage/indexedDB data
- Throttle CPU and network to simulate mobile environments
For example, you can throttle the CPU and network to mimic a slow mobile connection, which helps catch performance bottlenecks.
Storage Inspector
Storage Inspector enables you to:
- View, edit, delete cookies
- Inspect localStorage and sessionStorage
- Edit indexedDB and WebSQL databases
- Import/export storage data across environments
The ability to import and export storage data makes it easy to test using real user data from production.
Audits
The Audits panel lets you:
- Run audits for performance, accessibility, best practices
- Identify unused CSS, blocking requests, unoptimized images
- Check color contrast, valid HTML, ARIA roles
- Simulate slow connections and CPUs
- Export reports to share with stakeholders
Audits help surface web vitals metrics, accessibility issues, and performance wins/regressions.
Debugging Websites
When debugging a website in Safari, the Web Inspector and Debugger will likely be your go-to tools. Here are some tips:
- Use Inspector to view and modify HTML/CSS in real-time
- Set JavaScript breakpoints in Debugger to pause execution
- Log messages to Console to output variable values
- Check performance in Resources tab waterfall timeline
- Identify accessibility/performance issues with Audits
- Edit locally stored data like cookies in Storage Inspector
- Throttle CPU and network to mimic mobile devices
For example, the Web Inspector makes it easy to visually inspect and tweak HTML and CSS styles directly in the browser. Seeing the page update live as you make changes is invaluable.
And the Debugger allows setting breakpoints to halt JavaScript execution to examine values and step through code line-by-line to find bugs. The Console can also quickly output variable values with console.log()
for testing.
Debugging Mobile Apps
To debug native iOS or iPadOS apps, enable the Web Inspector in Safari's Develop menu. This allows Safari to connect to the app for debugging. Now you can:
- Inspect native UIKit elements in Web Inspector
- Log messages from Swift/Objective-C code into the Console
- Debug JavaScript in WKWebViews using the Debugger
- Profile memory usage and CPU activity
- Simulate slow networks to test offline behavior
- View app database content in Storage Inspector
The Web Inspector seamlessly bridges the gap between debugging web content and native UIKit components within iOS apps. The Console accepts output from both JavaScript and native app code.
The Debugger remains invaluable for stepping through JS execution in WKWebViews. The Resources tab provides insight into all network requests issued by the app. And Storage Inspector enables testing with real user data from production.
Tips and Best Practices
Here are some tips for getting the most out of Safari's developer tools:
- Learn keyboard shortcuts to speed up workflows
- Profile sites before/after optimization to measure gains
- Audit mobile experiences to surface mobile-specific issues
- Share audit reports with stakeholders to demonstrate improvements
- Filter Console messages to focus on relevant entries
- Enable persistence to preserve changes made during inspection
- Disable caching to avoid stale resources while debugging
- Reset state using Storage Inspector to test initial page loads
- Use conditional breakpoints based on iteration counts or variable values
For example, enabling persistence is useful to keep minor tweaks made during inspection. And conditional breakpoints that halt execution after X iterations can be powerful for tracking down elusive bugs.
Conclusion
Safari's built-in developer tools like the Web Inspector, Debugger, and Console provide powerful capabilities for inspecting, debugging, and modifying both websites and apps. Learning to leverage these Safari developer tools effectively can boost productivity and help you build better experiences. Unique features like debugging native iOS apps and storage inspection offer valuable insights unavailable in other browsers. By following best practices and tailoring these tools to your workflow, you can debug like a pro in Safari.
If you found this overview of Safari's built-in developer tools helpful, be sure to check out DevHunt for discovering and launching new developer tools. DevHunt makes it easy to find, evaluate, and promote innovative tools and technologies for building software.