Skip to content

Commit 5b154e5

Browse files
Interactivity API: Improve data-wp-context debugging by validating it 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 <cbravobernal@git.wordpress.org> Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
1 parent 83c485b commit 5b154e5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/interactivity/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
## 5.5.0 (2024-04-19)
66

7+
### Enhancements
8+
9+
- Improve data-wp-context debugging by validating it as a stringified JSON Object. ([#61045](https://github.com/WordPress/gutenberg/pull/61045))
10+
711
### Bug Fixes
812

913
- Hooks useMemo and useCallback should return a value. ([#60474](https://github.com/WordPress/gutenberg/pull/60474))

packages/interactivity/src/directives.js

+11
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ export default () => {
241241
const contextStack = useMemo( () => {
242242
if ( defaultEntry ) {
243243
const { namespace, value } = defaultEntry;
244+
// Check that the value is a JSON object. Send a console warning if not.
245+
if (
246+
typeof SCRIPT_DEBUG !== 'undefined' &&
247+
SCRIPT_DEBUG === true &&
248+
! isPlainObject( value )
249+
) {
250+
// eslint-disable-next-line no-console
251+
console.warn(
252+
`The value of data-wp-context in "${ namespace }" store must be a valid stringified JSON object.`
253+
);
254+
}
244255
updateSignals( currentValue.current, {
245256
[ namespace ]: deepClone( value ),
246257
} );

0 commit comments

Comments
 (0)