Issue with Navigation Menu Reloading Before Redirecting

Hello,

I am experiencing an issue with the navigation menu in my Shopify app. When I navigate from the home page to another page, the home page reloads first before redirecting to the desired page. The same issue occurs when I navigate back to the home page.

Below is the navigation menu code I am using in my PHP-based Shopify app:

<ui-nav-menu>
    <a href="/view/index.php">Home</a>
    <a href="/view/plan.php">Plan</a>
</ui-nav-menu>

To better illustrate the issue, I have recorded a short video:

Could you please help me understand why this is happening and how I can resolve it?

Thank you.

1 Like

Experimenting the exact same issue here but with the Shopify Remix Template. I was able to reproduce the behavior in the just cloned template with the following navigation:

Home Test01 Test02 Test03

Each page just contains a loader with a timeout

Hi, I had the same “page flicker” issue and my inspect suggests this is actually a transition animation instead of page reload. Can you try to inspect your DOM update and see if we are the same?

Please take a look at my post

Hey folks!

Thanks for flagging this issue - the app bridge team will look into this.

Hi Liam. Do you have any updates on this? Our app also refreshes first before it navigates to another page.

Hi Aras,

It does look like it’s still an open issue with us - will connect with the team working on this.

1 Like

Hi Liam, is there any update on the issue I am currently facing in my Next.js app?

@Liam-Shopify We are still experiencing this issue and cannot pass our BFS yearly checkup because of this.

This is what we get in the checkup:

  1. We are encountering an issue when navigating through the app where the current page will flicker before redirecting. Please see this screencast for an example. One option here could be to use a loading page, such as a skeleton page or a blank page with a spinner icon.

Can we skip this step as It’s an issue from Shopifys end ?

1 Like

We are getting the same issue. It would be great to get this resolved!

Same issue here, looking forward to getting this sorted! :crossed_fingers:

By default, defining ui-nav-menu with anchor tag will behave as standard MPAs (multiple page apps). You should handle click event and navigation yourself to prevent the full page reload. Something like

    function handleNavigation(event) {
      if (!(event.target instanceof HTMLAnchorElement)) return;
      event.preventDefault();
      event.stopPropagation();
      customNavigateDependingOnFramework(event.target.pathname);
    }
    document.addEventListener('click', handleNavigation);

Hi Uttam,

My Navbar page is working good when I use “Link” component
Maybe try this:

import { NavMenu } from '@shopify/app-bridge-react'
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'

const Navbar = () => {
  const { t } = useTranslation()
  return (
    <NavMenu>
      <Link to="/" rel="home">
        {t('NavigationMenu.dashboard')}
      </Link>
      <Link to="/analytics">{t('NavigationMenu.analytics')}</Link>
      <Link to="/settings">{t('NavigationMenu.settings')}</Link>
    </NavMenu>
  )
}

export default Navbar

@Henry_Tao, as far as I understand, I don’t believe that is the issue.

These are cases of MPAs where a full page reload is needed. The issue is that before the redirect happens, the side nav does a flicker on the client side that looks like you moved to the new page, but it shows the current one. Then full page load kicks in and loads the new one. This has been ongoing since January 24th, the first time I noticed it.

An ugly temporary workaround is to do something like this:

document.querySelector('ui-nav-menu').addEventListener('click', (event) => {
   if (event.target.nodeName === 'A') && event.target.href.includes('/your-side-nav-links')) {
      const mainScreen = document.getElementById('your-main-dom-element');
      if (mainScreen) mainScreen.innerHTML = '<div class="spinner"></div>';
   }
});

This intercepts the click and prevents the flickering by allowing you to show a fake loading spinner or whatever you want. Not pretty, but better than the current experience.

@oribenez, this has nothing to do with React. Client-side routing with React is fine.

2 Likes

Thank you, @nunof. I tried the code you provided, and it worked perfectly.

Has this been solved officially? Right now i am using Polaris Skeleton components to get around this issue. Its working well for now.

hmm guess delete doesnt actually delete here. is there any update on this for MPA?

Hi @Henry_Tao this code doesn’t appear to work as of today. I end up with both a document request to the new route and the embedded and hmac url parameters as well as a fetch to the route with a Bearer token

<ui-nav-menu>
  <a href="{{ route('shopify') }}" rel="home">Home</a>
  <a href="{{ route('shopify.publications') }}">Publications</a>
  <a href="{{ route('shopify.campaigns') }}">Campaigns</a>
  <a href="{{ route('shopify.videos') }}">Videos</a>
  <a href="{{ route('shopify.billing') }}">Billing</a>
  <a href="{{ route('shopify.help') }}">Help</a>
</ui-nav-menu>

and then at the bottom of my app wrapper

<script>
  // https://community.shopify.dev/t/issue-with-navigation-menu-reloading-before-redirecting/7435
  document.addEventListener('click', (event) => {
    // only handle clicks on links within the ui-nav-menu, rest can use wire:navigate
    console.log('click event', event.target, event.target.closest('ui-nav-menu'));
    if (!event.target.closest('ui-nav-menu')) return;
    console.log('preventing default navigation');
    event.preventDefault();
    event.stopPropagation();
    Livewire.navigate(event.target.pathname);
  });
</script>

When I use on page links outside of ui-nav-menu I only see fetch requests and no flashing of the screen: ui-nav-menu click → blank screen → flashes old screen → flashes in new screen. The flashing happens with the Shopify Email app so I assume that is an ongoing issue unrelated to double loading?

Hello,
I’m also using Remix Shopify boilerplate and I’m facing the same issue!
Any progress on it @Liam-Shopify ?
Thanks !

1 Like

Hi folks - I believe this was resolved previously but there may have been a regression. Digging into this on my side now.

@Liam-Shopify it’s still visible in our app.