fix(edit-button): send the page the editor was on to the CMS - #635
fix(edit-button): send the page the editor was on to the CMS#635kirtesh-cstk wants to merge 1 commit into
Conversation
The edit button linked to the entry and field but said nothing about the page it was clicked from. The CMS cannot work that out from the entry alone: a referenced entry can be rendered on several pages, and a nested one has no page among the entries that directly reference it. Without it the live preview panel falls back to the environment base URL. The redirect URL now carries the current page as preview-url, which the CMS already understands. Live preview's own query parameters are stripped so only the page remains. Co-Authored-By: Claude <noreply@anthropic.com>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
🔒 Security Scan Results
⏱️ SLA Breach Summary
✅ BUILD PASSED - All security checks passed |
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||
contentstackMridul
left a comment
There was a problem hiding this comment.
Approving. Small, self-contained, and safe to merge ahead of release-preview-client#578. The panel ignores preview-url when it can't split it, so there's no ordering constraint.
All checks green: test (7m3s), CodeQL, Analyze (javascript), the three Snyk checks, and security SCA/license/policy.
Two cleanups, neither blocking.
The live-preview parameter list now exists twice. getCurrentPageUrl.ts:117 introduces LIVE_PREVIEW_QUERY_PARAMS, and utils/addLivePreviewQueryTags.ts:7-11 already encodes the same concept inline (live_preview, content_type_uid, entry_uid, preview_timestamp) as the exact inverse operation. Two lists for one idea drift apart: add a parameter to the copier and the stripper won't know about it, and the symptom would be a stray parameter riding back into preview-url. One exported constant used by both would settle it.
preview_variant isn't a real parameter. It's in the strip list but appears nowhere in src outside that array. cslp-buttons and the other three all check out. Harmless, but it reads as protection that doesn't exist.
I also checked the strip list against what the panel actually injects: createLivePreviewUrl sets live_preview, content_type_uid and entry_uid (livePreviewUtils.ts:369-371), and stripLivePreviewQueryParams confirms the same three. The SDK's list covers all of them with room to spare, so nothing leaks back into preview-url.
On correctness: the input is window.location.href, guarded for a missing window, wrapped in try/catch, returning "" on failure, and the call site omits the parameter when it's empty, so the degradation path is the current published behaviour. searchParams.delete on an absent key is a no-op, append encodes the value, and the CMS decodes it through URLSearchParams, so the percent-encoded value survives the trip intact.
The tests get the interesting distinction right: preview parameters dropped, the site's own ?q=fountains kept. That's the case a naive url.search = "" would have broken, and nobody would have noticed until a customer with faceted search complained. Keeping getCurrentPageUrl pure (taking nothing, reading only window.location) is what makes it trivially testable, and the three updated editButtonAction tests show the parameter is genuinely on the wire rather than assumed.
Sends the page an editor was on to the CMS, so Live Preview can stay on it.
Why
The edit button links to the entry and the field, and says nothing about the page it was clicked from. The CMS cannot work that out from the entry alone. A referenced entry can be rendered on several pages, and a nested one has no page among the entries that directly reference it.
Without it the Live Preview panel has to guess, and falls back to the environment base URL when the guess fails.
What changed
generateRedirectUrlappends the current page aspreview-url. The CMS already reads that parameter, so nothing is needed on its side to accept it.getCurrentPageUrlreadswindow.locationand strips live preview's own query parameters (live_preview,content_type_uid,entry_uid,preview_timestamp,preview_variant,cslp-buttons) while keeping the site's own query string. It returns an empty string outside a browser or if the URL cannot be parsed, and the parameter is then omitted.This reads
window.location, so it does not depend on how a site tags its markup.Compatibility
An older CMS ignores a parameter it does not use, so the link keeps working. The paired CMS change treats a missing
preview-urlas no page information and resolves the page the way it does today.The in-iframe path is untouched. Only the standalone website case, where the button opens a new tab, gains the parameter.
Tests
Four new tests for
getCurrentPageUrl: a plain page URL, live preview parameters stripped, the site's own query string kept, and the path preserved.Three existing tests in
editButtonAction.test.tsassert the exact redirect URL and were updated for the new parameter.Full suite: 703 tests across 104 files, all passing.
Note for reviewers
The page URL now appears in the CMS URL and in browser history, including any query string the site carries. Worth a look if pages in your setup put anything sensitive in query parameters. Trimming to origin and path would avoid it, at the cost of preview accuracy for routes driven by the query string.
🤖 Generated with Claude Code