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

PolarisProvider handleClick not going through react router #306

Open
key88sf opened this issue Oct 16, 2024 · 2 comments · May be fixed by #327
Open

PolarisProvider handleClick not going through react router #306

key88sf opened this issue Oct 16, 2024 · 2 comments · May be fixed by #327

Comments

@key88sf
Copy link

key88sf commented Oct 16, 2024

Hi - I'm using the frontend react template as part of a Ruby/Rails app created with the Shopify App Template for Ruby (https://github.com/Shopify/shopify-app-template-ruby).

Problem:
All internal links which are added via Polaris components end up opening in a new tab, and do not use the proper shopify admin/app URL. Instead they open a URL with the backend server host name.

Example:

<Link url="/pagename">Click Me</Link>

Expected click behavior:

  • Same browser window
  • URL should be https://admin.shopify.com/store/my-store/apps/my-app/pagename

Actual click behavior:

  • New tab opened
  • URL is https://<backend server host name>/pagename

I think the bug is in the PolarisProvider.jsx. The change in this commit switched to using window.open for the click handler for internal links, which caused the new behavior. The old App Bridge v3 version (using navigate()) worked fine.

I believe the proper fix is to do this in PolarisProvider.jsx:

import {useNavigate} from "react-router-dom";
...
const navigate = useNavigate();
const handleClick = useCallback(() => navigate(url), [url]);
@fullammo
Copy link

fullammo commented Jan 10, 2025

The useNavigate method worked for me as well! I have a client side rendered app, Thanks! do you fancy to create a PR for this fix? :P

@siakaramalegos
Copy link

siakaramalegos commented Jan 16, 2025

That worked for me too. For anyone else navigating here looking for the answer, here's the top of my PolarisProvider.jsx file:

import { useCallback } from "react";
import { AppProvider } from "@shopify/polaris";
import "@shopify/polaris/build/esm/styles.css";
import { getPolarisTranslations } from "../../utils/i18nUtils";
+ import {useNavigate} from "react-router-dom";

function AppBridgeLink({ url, children, external, ...rest }) {
+  const navigate = useNavigate();
+  const handleClick = useCallback(() => navigate(url), [url]);
-  const handleClick = useCallback(() => window.open(url), [url]);

@siakaramalegos siakaramalegos linked a pull request Jan 17, 2025 that will close this issue
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 a pull request may close this issue.

3 participants