Shopify app not work on production build

I’m experiencing a major issue with my Shopify app where I’m losing authentication in the production build on all routes. Whenever I interact with any element in the app (e.g., clicking buttons), I lose authentication, and I’m redirected to the login page. This issue only occurs in the production build; everything works fine in the development environment. I’m using the Remix scaffold for the app, and my main components include an authentication loader that is supposed to handle session management.

In my app.tsx, I’m using the AppProvider from Shopify and routing through a navigation menu.

My loader function looks like this:

export async function loader({ request, }: LoaderFunctionArgs) {

  const { session } = await authenticate.admin(request);

  console.log(">> Session found for shop:", session.shop);

  const { getStoreSettings } = await import("app/models/settings/Settings.server");
  
  console.log("before get settings:", session.shop);

  const storeSettings = await getStoreSettings(session.shop)
  console.log("after get settings:", storeSettings);

  return ({ storeSettings: storeSettings })
}

In order to troubleshoot, I’ve tried the following:

  • Cleared caches and rebuilt the app.
  • Verified that admin authentication is set on all pages and checked each component.
  • Added console logs to track the store settings in the JWT.
  • Investigated routing configurations and ensured the components render correctly.

When clicking on the settings menu, for instance, console logs do not appear, indicating that the page might not load correctly, eventually redirecting me to the login page. The issue persists despite implementing error boundaries. Shopify support suggested to double-check my routing configuration and ensure that the loader function fetches the API key as expected. However, I still need further guidance on how to handle the authentication loader not returning the correct result, and whether there could be middleware affecting the routing. I would appreciate any input from the community on resolving this issue, particularly insights on debugging authentication in a Remix-based Shopify app or any common pitfalls in production builds.

Thank you!
Jesus Cocaño