ozzyonfire / shopify-next-app

Shopify app template on Next.js with app directory
https://shopify-next-app-six.vercel.app
MIT License
64 stars 7 forks source link

[Bug]: Getting params from useSearchParams() instead of page props #1

Closed AbhinavRobinson closed 1 year ago

AbhinavRobinson commented 1 year ago

I was working on deploying this to Vercel, it builds successfully but something goes wrong after the /exitframe route, the app stops responding inside shopify. (endless loading)

Env Variables

MONGODB_URI=<db>
SHOPIFY_API_KEY=<key>
NEXT_PUBLIC_SHOPIFY_API_KEY=<key>
SHOPIFY_API_SECRET=<secret>
HOST=<vercel-app>
NEXT_PUBLIC_HOST=<vercel-app>
SCOPES=<scopes>

App works fine in dev mode.

PS: Thanks for the awesome repo :)

AbhinavRobinson commented 1 year ago

Turns out app does not like a / at the end of the url in env. But the app still get's stuck at exitiframe.

AbhinavRobinson commented 1 year ago

Updating the exitiframe page fixed this for me, using the useSearchParams() hook

"use client";

import { Redirect } from "@shopify/app-bridge/actions";
import { useAppBridge, Loading } from "@shopify/app-bridge-react";
import { useEffect } from "react";
import { useSearchParams } from "next/navigation";

export default function ExitIframe() {
  const searchParams = useSearchParams();
  const app = useAppBridge();
  useEffect(() => {
    const redirectUri = searchParams.get("redirectUri");
    if (app && redirectUri) {
      const redirect = Redirect.create(app);
      const decodedRedirectUri = decodeURIComponent(redirectUri);
      redirect.dispatch(Redirect.Action.REMOTE, decodedRedirectUri);
    }
  }, [app, searchParams]);

  return <Loading />;
}
ozzyonfire commented 1 year ago

Great thanks - this was left over from when I adapted it from the "pages" router. Good find!