Skip to content

fix: remove query string from preview cookie path - #3213

Merged
lariciamota merged 1 commit into
devfrom
fix/preview-redirect-query-string
Feb 19, 2026
Merged

lariciamota merged 1 commit into
devfrom
fix/preview-redirect-query-string

Conversation

@lariciamota

@lariciamota lariciamota commented Feb 18, 2026 •

Copy link
Copy Markdown
Contributor

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:
hCMS

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
with https://sfj-2cf1e42--faststoreqa.preview.vtex.app/api/preview?contentType=search&documentId=332345a3-2a79-421d-bce0-7400c82b47e6&versionId=169b9294-4bf1-4b52-a37d-e7811f158bf5 without 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)

References

@lariciamota lariciamota self-assigned this Feb 18, 2026
@lariciamota lariciamota added the bug Something isn't working label Feb 18, 2026
@coderabbitai

coderabbitai Bot commented Feb 18, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/preview-redirect-query-string

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

Copy link
Copy Markdown

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.

@lariciamota
lariciamota marked this pull request as ready for review February 18, 2026 17:53
@lariciamota
lariciamota requested a review from a team as a code owner February 18, 2026 17:53
@lariciamota
lariciamota requested review from eduardoformiga and lucasfp13 and removed request for a team February 18, 2026 17:53

@lemagnetic lemagnetic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@lariciamota
lariciamota merged commit bc60a59 into dev Feb 19, 2026
3 checks passed
@lariciamota
lariciamota deleted the fix/preview-redirect-query-string branch February 19, 2026 19:18
thiagopereira-vtex pushed a commit that referenced this pull request Mar 10, 2026
## 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants