GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — WIP: Improve console dimming on second StrictMode render by billyjanitsch · Pull Request #24306 · react/react

Conversation

@billyjanitsch

@billyjanitsch billyjanitsch commented Apr 8, 2022

Copy link
Copy Markdown
Contributor

Summary

Fixes #24302. The goal is to apply dimming only to string values to avoid interfering with printing and introspection of complex values.

This PR isn't ready for review yet. Putting it up to show a working implementation, but:

  1. It can probably be simplified.
  2. It needs tests.
  3. I need to figure out what to do with the synchronized format implementation in the backend/ directory. The one in hook.js has only one callsite but the one in backend/ has several, some of which seem unrelated to browser console formatting. I don't have any knowledge of the devtools architecture so it'll take me some time to understand.
  4. I'm a bit concerned that this implementation only works when it agrees with the runtime about the list of placeholder symbols. This is also true of the existing implementation, but it would be nice to find a way around that, or at least to double check that this list is consistent across browsers + Node.

Implementation notes

The console API has two forms:

  1. Placeholder form, where the first N+1 arguments are a string containing N placeholders and N placeholder values, followed by any number of arbitrary values, e.g., ['%cA %s', 'color: blue', 'B', 'C'].
  2. Non-placeholder form, where all arguments are arbitrary values, e.g., ['A', 'B', 'C'].

Colors can only be applied using the first form, and only to the contents of the placeholder string. So the basic idea is to coerce the argument list into the placeholder form, apply the color directives, and append additional placeholders for all arguments without one. Some examples of this translation:

  • ['A', 1, 'B'] => ['%c%s %o %s', 'color: X', 'A', 1, 'B']
  • ['A %d', 1, 'B'] => ['%cA %d %c%s', 'color: X', 1, 'color: X', 'B']
  • ['A%cB', 'color: blue', 'C'] => ['%cA%cB %c%s', 'color: X', 'color: blue', 'color: X', 'C']

How did you test this change?

TBD

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@billyjanitsch
Improve console dimming on second StrictMode render
f54f6bd
@facebook-github-bot facebook-github-bot added the CLA Signed label Apr 8, 2022
@billyjanitsch billyjanitsch mentioned this pull request Apr 8, 2022
Console dimming on second StrictMode render forces string cast #24302
Closed
@rickhanlonii
rickhanlonii requested a review from lunaruan April 8, 2022 14:56
@lunaruan lunaruan mentioned this pull request Apr 14, 2022
[DevTools] Avoid Stringifying Objects during Strict Mode Double Logging #24373
Merged
@billyjanitsch

billyjanitsch commented Apr 14, 2022

Copy link
Copy Markdown
Contributor Author

Superseded by #24373.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@billyjanitsch billyjanitsch closed this Apr 14, 2022
@billyjanitsch
billyjanitsch deleted the console-dimming branch April 14, 2022 00:23
lunaruan added a commit that referenced this pull request Apr 14, 2022
@lunaruan @billyjanitsch
don't stringify objects for console log second render (#24373)
d63cd97
Fixes #24302 based on #24306.
---

The current implementation for strict mode double logging stringiness and dims the second log. However, because we stringify everything, including objects, this causes objects to be logged as `[object Object]` etc.

This PR creates a new function that formats console log arguments with a specified style. It does this by:
1. The first param is a string that contains %c: Bail out and return the args without modifying the styles. We don't want to affect styles that the developer deliberately set.
2. The first param is a string that doesn't contain %c but contains string formatting: `[`%c${args[0]}`, style, ...args.slice(1)]` Note: we assume that the string formatting that the developer uses is correct.
3. The first param is a string that doesn't contain string formatting OR is not a string: Create a formatting string where:
   -  boolean, string, symbol -> %s
   -  number -> %f OR %i depending on if it's an int or float
   -  default -> %o
---
Co-authored-by: Billy Janitsch <[email protected]>
lunaruan added a commit to lunaruan/react that referenced this pull request Apr 14, 2022
@lunaruan @billyjanitsch
don't stringify objects for console log second render (react#24373)
c5d77eb
Fixes react#24302 based on react#24306.
---

The current implementation for strict mode double logging stringiness and dims the second log. However, because we stringify everything, including objects, this causes objects to be logged as `[object Object]` etc.

This PR creates a new function that formats console log arguments with a specified style. It does this by:
1. The first param is a string that contains %c: Bail out and return the args without modifying the styles. We don't want to affect styles that the developer deliberately set.
2. The first param is a string that doesn't contain %c but contains string formatting: `[`%c${args[0]}`, style, ...args.slice(1)]` Note: we assume that the string formatting that the developer uses is correct.
3. The first param is a string that doesn't contain string formatting OR is not a string: Create a formatting string where:
   -  boolean, string, symbol -> %s
   -  number -> %f OR %i depending on if it's an int or float
   -  default -> %o
---

Co-authored-by: Billy Janitsch <[email protected]>
rickhanlonii pushed a commit that referenced this pull request Apr 14, 2022
@lunaruan @billyjanitsch @rickhanlonii
don't stringify objects for console log second render (#24373)
cfeeb4b
Fixes #24302 based on #24306.
---

The current implementation for strict mode double logging stringiness and dims the second log. However, because we stringify everything, including objects, this causes objects to be logged as `[object Object]` etc.

This PR creates a new function that formats console log arguments with a specified style. It does this by:
1. The first param is a string that contains %c: Bail out and return the args without modifying the styles. We don't want to affect styles that the developer deliberately set.
2. The first param is a string that doesn't contain %c but contains string formatting: `[`%c${args[0]}`, style, ...args.slice(1)]` Note: we assume that the string formatting that the developer uses is correct.
3. The first param is a string that doesn't contain string formatting OR is not a string: Create a formatting string where:
   -  boolean, string, symbol -> %s
   -  number -> %f OR %i depending on if it's an int or float
   -  default -> %o
---
Co-authored-by: Billy Janitsch <[email protected]>
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
@lunaruan @billyjanitsch
don't stringify objects for console log second render (react#24373)
ae1568c
Fixes react#24302 based on react#24306.
---

The current implementation for strict mode double logging stringiness and dims the second log. However, because we stringify everything, including objects, this causes objects to be logged as `[object Object]` etc.

This PR creates a new function that formats console log arguments with a specified style. It does this by:
1. The first param is a string that contains %c: Bail out and return the args without modifying the styles. We don't want to affect styles that the developer deliberately set.
2. The first param is a string that doesn't contain %c but contains string formatting: `[`%c${args[0]}`, style, ...args.slice(1)]` Note: we assume that the string formatting that the developer uses is correct.
3. The first param is a string that doesn't contain string formatting OR is not a string: Create a formatting string where:
   -  boolean, string, symbol -> %s
   -  number -> %f OR %i depending on if it's an int or float
   -  default -> %o
---
Co-authored-by: Billy Janitsch <[email protected]>
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
@lunaruan @billyjanitsch
don't stringify objects for console log second render (react#24373)
a33d9c2
Fixes react#24302 based on react#24306.
---

The current implementation for strict mode double logging stringiness and dims the second log. However, because we stringify everything, including objects, this causes objects to be logged as `[object Object]` etc.

This PR creates a new function that formats console log arguments with a specified style. It does this by:
1. The first param is a string that contains %c: Bail out and return the args without modifying the styles. We don't want to affect styles that the developer deliberately set.
2. The first param is a string that doesn't contain %c but contains string formatting: `[`%c${args[0]}`, style, ...args.slice(1)]` Note: we assume that the string formatting that the developer uses is correct.
3. The first param is a string that doesn't contain string formatting OR is not a string: Create a formatting string where:
   -  boolean, string, symbol -> %s
   -  number -> %f OR %i depending on if it's an int or float
   -  default -> %o
---
Co-authored-by: Billy Janitsch <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lunaruan lunaruan Awaiting requested review from lunaruan

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CLA Signed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Console dimming on second StrictMode render forces string cast

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@billyjanitsch @facebook-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.