Repository navigation
fix: remove query string from preview cookie path - #3213
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
## What's the purpose of this pull request? When `redirectPath` contains a query string (e.g. /test?id=1), the preview cookie's path attribute was being set to the full string including the query. Since the HTTP Set-Cookie Path attribute only supports URL paths, the query string portion was causing the cookie to not be scoped correctly, breaking preview mode for those routes. ## How it works? Removes the query string from `redirectPath` before assigning it to `options.path` in `setPreviewAndRedirect`. This ensures the cookie Path is always a clean URL path (e.g. /test instead of /test?p=1). The query string is still preserved in the `res.redirect(redirectPath)` call, so the user is redirected to the correct URL with all query parameters intact — only the cookie scoping is affected. ## How to test it? On `storeframework` hCMS I've created a draft for the search page: <img height="250" alt="hCMS" src="https://github.com/user-attachments/assets/20c3114b-d685-44e3-911a-1dae847dd4c8" /> To test it, run this branch's code or use the starters deploy preview (https://sfj-2cf1e42--faststoreqa.preview.vtex.app) and: - Call the preview API route, e.g.: /api/preview?contentType=search&documentId=332345a3-2a79-421d-bce0-7400c82b47e6&versionId=169b9294-4bf1-4b52-a37d-e7811f158bf5 - Verify that the preview mode cookies (`__prerender_bypass` and `__next_preview_data`) are set with Path without the query string - Verify that the browser is redirected to the path with the query string preserved as it is configured in the `previewRedirects` of the store (for the `storeframework` account for example it's configured as `search: "/s?q=headphone"`) - Verify that preview mode works correctly on the target page | With this change | Without this change | | ----- | ----- | | <img width="1608" height="1034" alt="with" src="https://github.com/user-attachments/assets/8fc0cefc-3275-4e6b-b331-617db8b104f9" /> https://sfj-2cf1e42--faststoreqa.preview.vtex.app/api/preview?contentType=search&documentId=332345a3-2a79-421d-bce0-7400c82b47e6&versionId=169b9294-4bf1-4b52-a37d-e7811f158bf5 | <img width="1605" height="1034" alt="without" src="https://github.com/user-attachments/assets/1b0d262c-076d-4ccf-8af4-fbbb088e235e" /> https://storeframework.vtex.app/api/preview?contentType=search&documentId=332345a3-2a79-421d-bce0-7400c82b47e6&versionId=169b9294-4bf1-4b52-a37d-e7811f158bf5 | | Note that the cookies are being set with the path **without** the query string and that the change in hCMS (text "Results Preview test") is being shown | Note that the cookies are being set with the path **with** the query string and that the change in hCMS is **not** being shown | ### Starters Deploy Preview https://sfj-2cf1e42--faststoreqa.preview.vtex.app/ ([PR](https://github.com/vtex-sites/faststoreqa.store/pull/894)) ## References - [Jira task](https://vtex-dev.atlassian.net/browse/SO-576) - [Next.js docs — `setPreviewData` options](https://nextjs.org/docs/pages/building-your-application/configuring/preview-mode#specify-the-preview-mode-duration)
What's the purpose of this pull request?
When
redirectPathcontains a query string (e.g. /test?id=1), the preview cookie's path attribute was being set to the full string including the query. Since the HTTP Set-Cookie Path attribute only supports URL paths, the query string portion was causing the cookie to not be scoped correctly, breaking preview mode for those routes.How it works?
Removes the query string from
redirectPathbefore assigning it tooptions.pathinsetPreviewAndRedirect. This ensures the cookie Path is always a clean URL path (e.g. /test instead of /test?p=1).The query string is still preserved in the
res.redirect(redirectPath)call, so the user is redirected to the correct URL with all query parameters intact — only the cookie scoping is affected.How to test it?
On

storeframeworkhCMS I've created a draft for the search page:To test it, run this branch's code or use the starters deploy preview (https://sfj-2cf1e42--faststoreqa.preview.vtex.app) and:
/api/preview?contentType=search&documentId=332345a3-2a79-421d-bce0-7400c82b47e6&versionId=169b9294-4bf1-4b52-a37d-e7811f158bf5
__prerender_bypassand__next_preview_data) are set with Path without the query stringpreviewRedirectsof the store (for thestoreframeworkaccount for example it's configured assearch: "/s?q=headphone")Starters Deploy Preview
https://sfj-2cf1e42--faststoreqa.preview.vtex.app/ (PR)
References
setPreviewDataoptions