Skip to content

Commit

Permalink
Interactivity API: Improve data-wp-context debugging by validating it…
Browse files Browse the repository at this point in the history
… as a stringified JSON Object. (#61045)

* Initial commit, add warning in JS

* remove package.json element

* Add changelog

* Use only warn

* Remove enqueue

* revert whitespace

* revert whitespace

* Prevent production constant missing errors

Co-authored-by: cbravobernal <[email protected]>
Co-authored-by: sirreal <[email protected]>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent 83c485b commit 5b154e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## 5.5.0 (2024-04-19)

### Enhancements

- Improve data-wp-context debugging by validating it as a stringified JSON Object. ([#61045](https://github.com/WordPress/gutenberg/pull/61045))

### Bug Fixes

- Hooks useMemo and useCallback should return a value. ([#60474](https://github.com/WordPress/gutenberg/pull/60474))
Expand Down
11 changes: 11 additions & 0 deletions packages/interactivity/src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ export default () => {
const contextStack = useMemo( () => {
if ( defaultEntry ) {
const { namespace, value } = defaultEntry;
// Check that the value is a JSON object. Send a console warning if not.
if (
typeof SCRIPT_DEBUG !== 'undefined' &&
SCRIPT_DEBUG === true &&
! isPlainObject( value )
) {
// eslint-disable-next-line no-console
console.warn(
`The value of data-wp-context in "${ namespace }" store must be a valid stringified JSON object.`
);
}
updateSignals( currentValue.current, {
[ namespace ]: deepClone( value ),
} );
Expand Down

0 comments on commit 5b154e5

Please sign in to comment.