Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix storefrontRedirect on soft navigations #1880

Merged
merged 2 commits into from
Mar 22, 2024
Merged

Fix storefrontRedirect on soft navigations #1880

merged 2 commits into from
Mar 22, 2024

Conversation

blittle
Copy link
Contributor

@blittle blittle commented Mar 21, 2024

WHY are these changes introduced?

Fix storefrontRedirect on soft navigations (click a link in the app to transition to a route that has a redirect). Also change the redirect status code from 301 to 302. 302 is not permanent and safer.

WHAT is this pull request doing?

  1. Strip out the remix _data search parameter when querying if a redirect exists.
  2. If it's a soft navigation, use Remix's internal headers, else use standard http status code if a document request
  3. Change from 301 to 302 in both types of responses.

HOW to test your changes?

  1. Add a link in the skeleton template: <Link to="/doesNotExist?return_to=%2Fcollections">Redirect</Link>
  2. Before this change, clicking that link will render an error page.
  3. After this change, clicking that link should redirect the user to /collections.
  4. Also, make sure that "right click and open in a new tab" works.

Post-merge steps

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

Copy link
Contributor

shopify bot commented Mar 21, 2024

Oxygen deployed a preview of your bl-fix-redirect branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
skeleton ✅ Successful (Logs) Preview deployment Inspect deployment March 22, 2024 2:24 PM
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment March 22, 2024 2:24 PM
vite ✅ Successful (Logs) Preview deployment Inspect deployment March 22, 2024 2:24 PM
optimistic-cart-ui ✅ Successful (Logs) Preview deployment Inspect deployment March 22, 2024 2:24 PM
subscriptions ✅ Successful (Logs) Preview deployment Inspect deployment March 22, 2024 2:24 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment March 22, 2024 2:24 PM

Learn more about Hydrogen's GitHub integration.

Also change redirect status code from 301 to 302
Copy link
Contributor

@frandiox frandiox left a comment

Choose a reason for hiding this comment

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

LGTM but I've left a couple of comments 👍

packages/hydrogen/src/routing/redirect.ts Show resolved Hide resolved
Comment on lines 43 to 44
const redirectFrom =
pathname + (filteredSearchParams ? '?' + isSoftNavigation : '');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is wrong? Isn't redirectFrom getting ?true here?
Maybe you meant pathname + (isSoftNavigation ? '?' + filteredSearchParams : '') instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, maybe we could simplify this to:

const url = new URL(request.url);
const isSoftNavigation = url.searchParams.has('_data');

url.searchParams.delete('_data');

const redirectFrom = url.toString().replace(url.origin, '');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, this is odd that it worked. I'm going to add to the unit tests and figure out why the API request didn't fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It didn't fail my unit test because the test didn't have an extra query parameter. I added another test to verify that scenario.

'@shopify/hydrogen': patch
---

Fix bug where `storefrontRedirect` would return an error on soft page navigations. Also change the redirect status code from 301 to 302.
Copy link
Contributor

@frandiox frandiox Mar 22, 2024

Choose a reason for hiding this comment

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

Thinking about it, should we consider 301 => 302 a breaking change? 🤔 Maybe not...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I argue it was a bug in the first place 🙈

@blittle blittle merged commit 8707295 into main Mar 22, 2024
13 checks passed
@blittle blittle deleted the bl-fix-redirect branch March 22, 2024 14:42
blittle added a commit that referenced this pull request Mar 22, 2024
* Fix `storefrontRedirect` on soft navigations

Also change redirect status code from 301 to 302

* Add test and PR feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants