Fix: 'Set opt-in notification as seen' returns 400 on repeat visits#23056
Open
Fix: 'Set opt-in notification as seen' returns 400 on repeat visits#23056
Conversation
…dempotency in setting opt-in seen status
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes repeated 400 errors caused by the “set opt-in notification as seen” request being triggered on every General page load after the notification had already been seen, and makes the backend endpoint safely idempotent.
Changes:
- Restore conditional rendering of the opt-in notification so it only mounts (and triggers its “seen” side-effect) when actually shown.
- Make the REST endpoint return success when the “seen” user meta is already set, avoiding
update_user_meta()’s “unchanged value returns false” behavior. - Add a WP test that calls the endpoint twice and asserts both responses are 200/success.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
packages/js/src/general/components/opt-in-container.js |
Prevents unnecessary mounting/API calls by returning null when isOpen is false. |
src/general/user-interface/opt-in-route.php |
Adds an early-success path when the meta is already set; reuses a computed $meta_key. |
tests/WP/General/User_Interface/Set_Opt_In_Seen_Test.php |
Adds coverage to ensure the endpoint remains idempotent on repeated calls. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Pull Request Test Coverage Report for Build 685e10595c107c93c4c7f33daa345ed2a716fc28Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Context
When a user visits the General page after the task list opt-in notification has already been seen, the browser console logs:
Error setting opt-in notification as seen: Object { success: false, status: 400 }. This happens on every page load after the first time.Root cause
This was introduced by #22960, which replaced the
Toastwith aModalNotification(HeadlessUIDialog). As part of that change,OptInContainerwas refactored to always renderTaskListOptInNotificationand control visibility via theisOpenprop, instead of conditionally mounting/unmounting it.Previously,
TaskListOptInNotificationonly mounted when the notification needed to be shown. ItsuseEffect(with[]deps) calledsetOptInNotificationSeen("task_list")on mount — which was correct, since mounting only happened once when the notification was actually visible.After #22960, the component always mounts (even when
isOpen=false), so two things go wrong:useEffectfires on every page load regardless of whether the notification is visible, sending a POST to/yoast/v1/seen-opt-in-notificationevery time.'1', soupdate_user_meta()returnsfalse(no change was made). The endpoint interpretsfalseas failure and returns a 400 status.Summary
This PR can be summarized in the following changelog entry:
Relevant technical choices:
opt-in-container.js):OptInContainernow returnsnullwhenisOpenis false, soTaskListOptInNotificationonly mounts when the notification is actually visible. This prevents theuseEffectfrom firing unnecessary API calls. TheModalNotificationcomponent works fine being mounted/unmounted — there's no need to keep it always mounted.opt-in-route.php): Before callingupdate_user_meta, the endpoint checks if the meta is already set totrue. If so, it returns{ success: true, status: 200 }immediately. This is a safety net against the well-knownupdate_user_metafootgun where unchanged values returnfalse.Set_Opt_In_Seen_Test.php): Added a test that calls the endpoint twice and verifies both calls return 200.Test instructions
Test instructions for the acceptance test before the PR gets merged
This PR can be acceptance tested by following these steps:
_yoast_wpseo_task_list_opt_in_notification_seenfromwp_usermetafor your user.seen-opt-in-notificationerrors in the console.Relevant test scenarios
Check the console for any 400 errors related to
seen-opt-in-notification.Test instructions for QA when the code is in the RC
Impact check
This PR affects the following parts of the plugin, which may require extra testing:
Other environments
[shopify-seo], added test instructions for Shopify and attached theShopifylabel to this PR.[yoast-doc-extension], added test instructions for Yoast SEO for Google Docs and attached theGoogle Docs Add-onlabel to this PR.Documentation
Quality assurance
grunt build:imagesand commited the results, if my PR introduces new images or SVGs.Innovation
innovationlabel.Fixes 2896