Shopify App - Embedded App Navigation Bar adding trailing slashes to links, makes Next.js 404

I’m super confused. Last night my app was working fine, but today I noticed that all of the navigation bar links are turning up 404s in my Next.js app.

The reason, I believe, is that despite creating a link like https://foo.ngrok.io/reports, the navigation bar turns that into something like https://foo-store.myshopify.com/admin/apps/foo/reports/, with a trailing slash. This then causes [https://foo.ngrok.io/reports/](https://foo.ngrok.io/reports/) to load instead of [https://foo.ngrok.io/reports](https://foo.ngrok.io/reports), which nextjs doesn’t currently seem to be able to handle when doing the initial server side render, causing a 404.

Curious why I just suddenly started experiencing this and what I can do to fix it, besides just not using the Navigation bar anymore. Not sure if something changed on the Shopify side, or on the Next.js side, but this seems to be an ongoing issue with Next.js, so did something change with Shopify? I haven’t changed these links in a couple of weeks, but they’re suddenly 404ing.

2 Likes

Same problem here, it was working well yesterday but now links are broken.

Glad to hear it’s not just me. It was broken a couple days ago, then was fine earlier today, then went back to being broken.

I started a new topic with “Bug” in the title to hopefully get more visibility on it.

Same here .. getting 404 while navigating through navigation bar. I have a Next.js app. Everything was working fine until yesterday and I haven’t made any change. Seems like a bug..

I am expierencing the same issue and also I think your idea is correct. The strange thing is, that when I first created the app on Thursday it worked like a champ. I had multiple reboots and also restarted ngrok a few times (I have a paid plan with a reserved url, so it shouldnt matter).

Now I wanted to continue work and suddenly I am getting 404 for all my navigation links.

The app is embedded. White listed urls are set up. Atuh. and install is also working. Also the redirect to the index.

Btw I temporarly fixed this issue by adding some code to my server.js.

The page I want to navigate to is named “template”. The route is “/template” and shopify requests “/template/”.

My server.js is very basic and is basically the same as the tutorial config: https://github.com/shopify/shopify-demo-app-node-react/

I am intercepting the request and remove the “/”. I am new to react and nextjs. So if there is a more elegant way to fix this problem, feel free to tell me. But this works for now.

  router.get('*', verifyRequest(), async (ctx) => {

    if (ctx.url.includes("/template/")) {
      ctx.url = ctx.url.replace("/template/", "/template");
    }
    await handle(ctx.req, ctx.res);
    ctx.respond = false;
    ctx.res.statusCode = 200;
  });
1 Like

Thanks for the fix!

1 Like

Can you all check your links and see if this has been rolled back? They aren’t showing up with the extra / anymore for me.

Looks like they put in a fix for this other issue, so maybe that includes what we’re seeing? https://community.shopify.com/c/Shopify-APIs-SDKs/Embedded-app-TitleBar-links-are-wrong-when-app-URL-is-visited/td-p/651133

1 Like

Yes it is fixed.

I am still seeing this. The link in the embedded navigation, has a trailing slash, causing next.js to 404. It seems like it was fixed for some people?

2 Likes

Yes. Starting to see this again today!! All our apps are affected by this.

Team Shopify: Please look into this and resolve. Thanks.

2 Likes

If you’re using next.js dynamic routing and want to remove the trailing slash add this before you handle the request:

if (ctx.url.includes("/?")) {
  if (ctx.url.substring(0, 2) != '/?') {
    ctx.url = ctx.url.replace("/?", "?") // Remove trailing slash before params          
    ctx.url = ctx.url.replace(/\/\s*$/, "") // Remove trailing slash at the end
  }
}

You probably don’t need to remove the trailing slash at the end but it’s there just in case

2 Likes

Can confirm I am seeing this as of yesterday as well.

1 Like

I am seeing this today too.

Thanks for the fix! I can confirm that this workaround by @DannyUWP is working fine. ??

If you’re using next.js dynamic routing and want to remove the trailing slash add this before you handle the request:> > > if (ctx.url.includes("/?")) {> if (ctx.url.substring(0, 2) != '/?') {> ctx.url = ctx.url.replace("/?", "?") // Remove trailing slash before params > ctx.url = ctx.url.replace(/\/\s*$/, "") // Remove trailing slash at the end> }> }> > > You probably don’t need to remove the trailing slash at the end but it’s there just in case

yup could override the routing, wont work for our setup exactly as the dev environments and live handle these differently.
Think we may just have to rip out the embedded nav and build it into the app. I wander how may apps have broken silently with this?

I completely migrated away from using Next.js so haven’t been having the issue. it’s ridiculous that this keeps surfacing.

1 Like

@hedgerh I am curious about what you are using instead. While overall I like Next.js, the Shopify tutorials keep changing and aren’t up to date, and now this whole thing with the links is making it difficult to not only get something to work, but to keep it working and hope that Shopify doesn’t break things yet again.

1 Like

@breckskier I basically took react + next out of it. Instead, I just serve up plain html after passing data in to it with template strings. You could have koa use a view engine to serve up templates. The important part is that you use the shopify app bridge on the client side if you want to embed your app in the shopify admin.

This is a little old, but check this out if you want a taste of how to set something up that isn’t just a Next.js app: https://github.com/Shopify/app-bridge-tutorial

It has three branches. One of them is minimal react, one is no react at all. I kinda based mine off the shopify cli boilerplate and this one.

I went static html bc my ui is pretty simple. Just serves up iframes.

Happy to go into more detail or help however I can. It was a pain to figure out!

If you stick with Next, you should set something up in Koa to remove the trailing /s off of any urls. that should fix the issue.

1 Like

FYI looks like someone fixed it. Can someone from Shopify please comment on this?? Also, can you please put tests around this?! This keeps breaking.

2 Likes