Overview Open Chrome DevTools What's New in DevTools DevTools tips Simulate mobile devices with Device Mode Performance insights: Get actionable insights on your website's performance Lighthouse: Optimize website speed Animations: Inspect and modify CSS animation effects Changes: Track your HTML, CSS, and JavaScript changes Coverage: Find unused JavaScript and CSS CSS Overview: Identify potential CSS improvements Issues: Find and fix problems Media: View and debug media players information Memory Inspector: Inspect ArrayBuffer, TypedArray, DataView, and Wasm Memory. Network conditions: Override the user agent string Security: Understand security issues Search: Find text across all loaded resources Sensors: Emulate device sensors WebAuthn: Emulate authenticators Customize DevTools Engineering blog
Overview Open Chrome DevTools What's New in DevTools DevTools tips Simulate mobile devices with Device Mode Performance insights: Get actionable insights on your website's performance Lighthouse: Optimize website speed Animations: Inspect and modify CSS animation effects Changes: Track your HTML, CSS, and JavaScript changes Coverage: Find unused JavaScript and CSS CSS Overview: Identify potential CSS improvements Issues: Find and fix problems Media: View and debug media players information Memory Inspector: Inspect ArrayBuffer, TypedArray, DataView, and Wasm Memory. Network conditions: Override the user agent string Security: Understand security issues Search: Find text across all loaded resources Sensors: Emulate device sensors WebAuthn: Emulate authenticators Customize DevTools Engineering blog

Edit and save files with Workspaces

Published on Updated on

Goal: This tutorial provides hands-on practice in setting up and using Workspaces so that you can use Workspaces in your own projects. Workspaces enable you to save changes that you make within DevTools to source code that's stored on your computer.

Caution

Prerequisites: Before beginning this tutorial, you should know how to:

Overview

Workspaces enable you to save a change that you make in Devtools to a local copy of the same file on your computer. For example, suppose:

  • You have the source code for your site on your desktop.
  • You're running a local web server from the source code directory, so that the site is accessible at localhost:8080.
  • You've got localhost:8080 open in Google Chrome, and you're using DevTools to change the site's CSS.

With Workspaces enabled, the CSS changes that you make within DevTools are saved to the source code on your desktop.

Limitations

If you're using a modern framework, it probably transforms your source code from a format that's easy for you to maintain into a format that's optimized to run as quickly as possible. Workspaces is usually able to map the optimized code back to your original source code with the help of source maps. But there's a lot of variation between frameworks over how they use source maps. Devtools simply can't support all the variations.

Workspaces is known to not work with these frameworks:

  • Create React App

If you run into issues while using Workspaces with your framework of choice, or you get it working after some custom configuration, please start a thread in the mailing list or ask a question on Stack Overflow to share your knowledge with the rest of the DevTools community.

Related feature: Local Overrides

Local Overrides is another DevTools feature that is similar to Workspaces. Use Local Overrides when you want to experiment with changes to a page, and you need to see those changes across page loads, but you don't care about mapping your changes to the page's source code.

Step 1: Setup

Complete this tutorial to get hands-on experience with Workspaces.

Set up the demo

  1. Open the demo. In the bottom-left of the editor, click the button labelled Tools.

  2. Under Tools, select Import / Export > Download Project.

  3. Close the tab.

  4. Unzip the source code and move the unzipped app directory to your desktop. For the rest of this tutorial this directory will be referred to as ~/Desktop/app.

  5. Start a local web server in ~/Desktop/app. Below is some sample code for starting up SimpleHTTPServer, but you can use whatever server you prefer.

    cd ~/Desktop/app
    # If your Python version is 3.X
    # On Windows, try "python -m http.server" or "py -3 -m http.server"
    python3 -m http.server
    # If your Python version is 2.X
    python -m SimpleHTTPServer
  6. Open a tab in Google Chrome and go to locally-hosted version of the site. You should be able to access it via a URL like localhost:8000. The exact port number may be different.

    The locally-hosted demo page opened in Chrome.

Set up DevTools

  1. Open DevTools on the locally-hosted demo page.

  2. Navigate to the Sources > Filesystem tab and click Add. Add folder to workspace.

    The Filesystem tab.
  3. Select the ~/Desktop/app folder you downloaded and unpacked in the previous step.

  4. In the prompt at the top, click Allow to give DevTools permission to read and write to the directory.

    The Allow button in the prompt.

In the Filesystem tab, there is now a green dot next to index.html, script.js, and styles.css. These green dots mean that DevTools has established a mapping between the network resources of the page, and the files in ~/Desktop/app.

The Filesystem tab now shows a mapping between the local files and the network ones.

Step 2: Save a CSS change to disk

  1. Open ~/Desktop/app/styles.css in a text editor. Notice how the color property of h1 elements is set to fuchsia.

    Viewing styles.css in a text editor.
  2. Close the text editor.

  3. Back in DevTools, click the Elements tab.

  4. Change the value of the color property of the <h1> element to your favorite color. To do so:

    1. Click the <h1> element in the DOM Tree.
    2. In the Styles pane, find the h1 { color: fuchsia } CSS rule and change the color to your favorite. In this example, the color is set to green.
    Setting the color property of the h1 element to green.

    The green dot The green dot. next to styles.css:1 in the Styles pane means that any change you make is mapped to ~/Desktop/app/styles.css.

  5. Open ~/Desktop/app/styles.css in a text editor again. The color property is now set to your favorite color.

  6. Reload. Reload the page. The color of the <h1> element is still set to your favorite color. This works because when you made the change, DevTools saved the change to disk. And then, when you reloaded the page, your local server served the modified copy of the file from disk.

Step 3: Save an HTML change to disk

Try changing HTML from the Elements panel

Warning

Warning: The workflow that you're about to try doesn't work. You're trying it now so that you don't waste time later trying to figure out why it's not working.

  1. Open the Elements tab.

  2. Double click the text content of the h1 element, which says Workspaces Demo, and replace it with I ❤️ Cake.

    Attempting to change HTML from the DOM Tree of the Elements panel.
  3. Open ~/Desktop/app/index.html in a text editor. The change that you just made isn't there.

  4. Reload. Reload the page. The page reverts to its original title.

Optional: Why it doesn't work

Note: This section describes why the workflow from Try changing HTML from the Elements panel doesn't work. You can skip this section if you don't care why.

  • The tree of nodes that you see on the Elements panel represents the page's DOM.
  • To display a page, a browser fetches HTML over the network, parses the HTML, and then converts it into a tree of DOM nodes.
  • If the page has any JavaScript, that JavaScript may add, delete, or change DOM nodes. CSS can change the DOM, too, via the content property.
  • The browser eventually uses the DOM to determine what content it should present to browser users.
  • Therefore, the final state of the page that users see may be very different from the HTML that the browser fetched.
  • This makes it difficult for DevTools to resolve where a change made in the Elements panel should be saved, because the DOM is affected by HTML, JavaScript, and CSS.

In short, the DOM Tree !== HTML.

Change HTML from the Sources panel

If you want to save a change to the page's HTML, do it via the Sources panel.

  1. Navigate to Sources > Page.

  2. Click (index). The HTML for the page opens.

  3. Replace <h1>Workspaces Demo</h1> with <h1>I ❤️ Cake</h1>.

  4. Press Command+S (Mac) or Control+S (Windows, Linux, ChromeOS) to save the change.

  5. Reload. Reload the page. The <h1> element is still displaying the new text.

    Changing HTML from the Sources panel.
  6. Open ~/Desktop/app/index.html. The <h1> element contains the new text.

Step 4: Save a JavaScript change to disk

The Sources panel is also the place to make changes to JavaScript. But sometimes you need to access other panels, such as the Elements panel or the Console panel, while making changes to your site. There's a way to have the Sources panel open alongside other panels.

  1. Open the Elements tab.

  2. Press Command+Shift+P (Mac) or Control+Shift+P (Windows, Linux, ChromeOS). The Command Menu opens.

  3. Type QS, then select Show Quick Source. At the bottom of your DevTools window there is now a Quick Source tab.

    Opening the Quick Source tab via Command Menu.

    The tab is displaying the contents of index.html, which is the last file you edited in the Sources panel. The Quick Source tab gives you the editor from the Sources panel, so that you can edit files while having other panels open.

  4. Press Command+P (Mac) or Control+P (Windows, Linux, ChromeOS) to open the Open File dialog.

  5. Type script, then select app/script.js.

    Opening script.js via the Open File dialog.
  6. Notice the Save Changes To Disk With Workspaces link in the demo. It's styled regularly.

  7. Add the following code to the bottom of script.js via the Quick Source tab.

    document.querySelector('a').style = 'font-style:italic';
  8. Press Command+S (Mac) or Control+S (Windows, Linux, ChromeOS) to save the change.

  9. Reload. Reload the page. The link on the page is now italic.

The link on the page is now italic.

Next steps

Success

Congratulations! You now know how to save to sources the changes you make in DevTools to your locally hosted website.

You can set up multiple folders as Workspaces. All such folders are listed in Settings. Settings > Workspace.

Next, learn how to use DevTools to change CSS and debug JavaScript.

Updated on Improve article

We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.