Skip to content

Commit c7af7ef

Browse files
justin808claude
andcommitted
Add suppress_unused_component_warnings config option
Add ability to suppress console.warn messages about registered components that are not used on a page. This is useful in production environments where many React components are registered globally but only a subset are used on each page. Configuration: config.suppress_unused_component_warnings = true Changes: - Add suppress_unused_component_warnings config option (default: false) - Pass option to railsContext as suppressUnusedComponentWarnings - Update CallbackRegistry to check the option before logging warnings - Update TypeScript types and RBS signatures - Update documentation and tests Closes #2117 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 16b3908 commit c7af7ef

File tree

8 files changed

+43
-7
lines changed

8 files changed

+43
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Changes since the last non-beta release.
2525

2626
#### Added
2727

28+
- **Suppress Unused Component Warnings**: Added `suppress_unused_component_warnings` configuration option to disable console warnings about registered components that are not used on a page. This is useful for production environments where many components are registered globally but only a subset are used on each page. Set `config.suppress_unused_component_warnings = true` in your React on Rails initializer. Note: This only affects React on Rails Pro users. [PR 2125](https://github.com/shakacode/react_on_rails/pull/2125) by [justin808](https://github.com/justin808).
29+
2830
- **Service Dependency Checking for bin/dev**: Added optional `.dev-services.yml` configuration to validate required external services (Redis, PostgreSQL, Elasticsearch, etc.) are running before `bin/dev` starts the development server. Provides clear error messages with start commands and install hints when services are missing. Zero impact if not configured - backwards compatible with all existing installations. [PR 2098](https://github.com/shakacode/react_on_rails/pull/2098) by [justin808](https://github.com/justin808).
2931

3032
#### Changed

docs/api-reference/configuration.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,21 @@ config.component_registry_timeout = 5000 # default (5 seconds)
710710

711711
Set to `0` to wait indefinitely (not recommended for production).
712712

713+
#### suppress_unused_component_warnings
714+
715+
**Type:** Boolean
716+
**Default:** `false`
717+
718+
Suppress console warnings about registered components that are not used on a page:
719+
720+
```ruby
721+
config.suppress_unused_component_warnings = true
722+
```
723+
724+
When many React components are registered globally but only a subset are used on each page, React on Rails Pro will emit console warnings suggesting the unused components may be cleaned up. Set this option to `true` to suppress these warnings, which is useful in production environments where such warnings can be noisy.
725+
726+
> **Note:** This configuration option only affects React on Rails Pro users, as the warnings are generated by the Pro package's component registry timeout feature.
727+
713728
### I18n Configuration
714729

715730
These options are for applications using [react-intl](https://formatjs.io/docs/react-intl/) or similar internationalization libraries. If your application doesn't need i18n, you can skip this section.

packages/react-on-rails-pro/src/CallbackRegistry.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ export default class CallbackRegistry<T> {
5353
this.waitingPromises.forEach((waitingPromiseInfo, itemName) => {
5454
waitingPromiseInfo.reject(this.createNotFoundError(itemName));
5555
});
56-
this.notUsedItems.forEach((itemName) => {
57-
console.warn(
58-
`Warning: ${this.registryType} '${itemName}' was registered but never used. This may indicate unused code that can be removed.`,
59-
);
60-
});
56+
57+
// Only log warnings if not suppressed via configuration
58+
const suppressWarnings = getRailsContext()?.suppressUnusedComponentWarnings;
59+
if (!suppressWarnings) {
60+
this.notUsedItems.forEach((itemName) => {
61+
console.warn(
62+
`Warning: ${this.registryType} '${itemName}' was registered but never used. This may indicate unused code that can be removed.`,
63+
);
64+
});
65+
}
6166
};
6267

6368
onPageLoaded(() => {

packages/react-on-rails/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type ReactComponent = ComponentType<any> | string;
2020
// Keep these in sync with method lib/react_on_rails/helper.rb#rails_context
2121
export type RailsContext = {
2222
componentRegistryTimeout: number;
23+
suppressUnusedComponentWarnings: boolean;
2324
railsEnv: string;
2425
inMailer: boolean;
2526
i18nLocale: string;

packages/react-on-rails/tests/ClientRenderer.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('ClientRenderer', () => {
5959
httpAcceptLanguage: 'en',
6060
serverSide: false,
6161
componentRegistryTimeout: 0,
62+
suppressUnusedComponentWarnings: false,
6263
});
6364
document.body.appendChild(railsContextElement);
6465

@@ -116,6 +117,7 @@ describe('ClientRenderer', () => {
116117
httpAcceptLanguage: 'en',
117118
serverSide: false,
118119
componentRegistryTimeout: 0,
120+
suppressUnusedComponentWarnings: false,
119121
});
120122
document.body.appendChild(railsContextElement);
121123

@@ -145,6 +147,7 @@ describe('ClientRenderer', () => {
145147
httpAcceptLanguage: 'en',
146148
serverSide: false,
147149
componentRegistryTimeout: 0,
150+
suppressUnusedComponentWarnings: false,
148151
});
149152
document.body.appendChild(railsContextElement);
150153

@@ -195,6 +198,7 @@ describe('ClientRenderer', () => {
195198
httpAcceptLanguage: 'en',
196199
serverSide: false,
197200
componentRegistryTimeout: 0,
201+
suppressUnusedComponentWarnings: false,
198202
});
199203
document.body.appendChild(railsContextElement);
200204

react_on_rails/lib/react_on_rails/configuration.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def self.configuration
6464
# If exceeded, an error will be thrown for server-side rendered components not registered on the client.
6565
# Set to 0 to disable the timeout and wait indefinitely for component registration.
6666
component_registry_timeout: DEFAULT_COMPONENT_REGISTRY_TIMEOUT,
67+
# Set to true to suppress warnings about registered components not being used on a page.
68+
# This can be useful in production environments where many components are registered globally
69+
# but only a subset are used on each page.
70+
suppress_unused_component_warnings: false,
6771
generated_component_packs_loading_strategy: nil,
6872
server_bundle_output_path: DEFAULT_SERVER_BUNDLE_OUTPUT_PATH,
6973
enforce_private_server_bundles: false
@@ -82,7 +86,7 @@ class Configuration
8286
:same_bundle_for_client_and_server, :rendering_props_extension,
8387
:make_generated_server_bundle_the_entrypoint,
8488
:generated_component_packs_loading_strategy,
85-
:component_registry_timeout,
89+
:component_registry_timeout, :suppress_unused_component_warnings,
8690
:server_bundle_output_path, :enforce_private_server_bundles
8791

8892
# Class instance variable and mutex to track if deprecation warning has been shown
@@ -144,7 +148,8 @@ def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender
144148
i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil, i18n_yml_safe_load_options: nil,
145149
random_dom_id: nil, server_render_method: nil, rendering_props_extension: nil,
146150
components_subdirectory: nil, auto_load_bundle: nil,
147-
component_registry_timeout: nil, server_bundle_output_path: nil, enforce_private_server_bundles: nil)
151+
component_registry_timeout: nil, suppress_unused_component_warnings: nil,
152+
server_bundle_output_path: nil, enforce_private_server_bundles: nil)
148153
self.node_modules_location = node_modules_location.present? ? node_modules_location : Rails.root
149154
self.generated_assets_dirs = generated_assets_dirs
150155
self.generated_assets_dir = generated_assets_dir
@@ -169,6 +174,7 @@ def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender
169174
self.skip_display_none = skip_display_none
170175
self.rendering_props_extension = rendering_props_extension
171176
self.component_registry_timeout = component_registry_timeout
177+
self.suppress_unused_component_warnings = suppress_unused_component_warnings || false
172178

173179
# Server rendering:
174180
self.server_bundle_js_file = server_bundle_js_file

react_on_rails/lib/react_on_rails/helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ def rails_context(server_side: true)
274274
@rails_context ||= begin
275275
result = {
276276
componentRegistryTimeout: ReactOnRails.configuration.component_registry_timeout,
277+
suppressUnusedComponentWarnings: ReactOnRails.configuration.suppress_unused_component_warnings,
277278
railsEnv: Rails.env,
278279
inMailer: in_mailer?,
279280
# Locale settings

react_on_rails/sig/react_on_rails/configuration.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module ReactOnRails
3232
attr_accessor generated_component_packs_loading_strategy: Symbol?
3333
attr_accessor immediate_hydration: bool
3434
attr_accessor component_registry_timeout: Integer
35+
attr_accessor suppress_unused_component_warnings: bool
3536
attr_accessor server_bundle_output_path: String?
3637
attr_accessor enforce_private_server_bundles: bool
3738

@@ -68,6 +69,7 @@ module ReactOnRails
6869
?auto_load_bundle: bool?,
6970
?immediate_hydration: bool?,
7071
?component_registry_timeout: Integer?,
72+
?suppress_unused_component_warnings: bool?,
7173
?server_bundle_output_path: String?,
7274
?enforce_private_server_bundles: bool?
7375
) -> void

0 commit comments

Comments
 (0)