next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Custom route `post/`id` flashing 404 before finally rendering page.

Open tgrecojs opened this issue 9 years ago • 49 comments

I'm currently experiencing a problem with my post/:id routes where next flashes the 404 page before successfully rendering my component. At first, I thought this was a redux related issues as my post/id pages are checking the app state to filter whichever component should be shown using shallow routing.

After discussing this in the slack channel, I added another API call that would retrieve each blog post but quickly found that I still encountered the same issue. I know that this has happened for at least one other developer so i'm wondering if anyone can point me in the right direction? I'm trying to finish up this personal portfolio I built with next.

Below, you can see exactly what's happening. Everything looks A1 except for that 404 that blinks whenever a specific post is rendered.

next.js blog post err

The benefits i've gained are tremendous however there have been some minor issues like this one that have got me a little puzzled 🤔 . Anyways, let me know if I can provide any additional info. Thanks 😄

tgrecojs avatar Aug 22 '17 19:08 tgrecojs

Same issue +1

moaxaca avatar Aug 23 '17 02:08 moaxaca

How are you rendering the Link component to that url? Are you using <Link href="/post/1"> or <Link href="/post?id=1" as="/post/1">?

sergiodxa avatar Aug 23 '17 02:08 sergiodxa

Currently, i'm using <Link prefetch href={/blog/${x.id}} />. I've tried <Link prefetch href={/blog/${x.id}} as={/blog/${x.id}} /> in the past and just plugged it in again. Still having the same issue.

tgrecojs avatar Aug 23 '17 02:08 tgrecojs

Your href and as props shouldn't be the same, href is the real url, if you have pages/blog.js and receive id as a query then href should be /blog?id=${x.id} and as is your pretty custom url in this case /blog/${x.id}.

sergiodxa avatar Aug 23 '17 22:08 sergiodxa

@sergiodxa I just put this code in place.

<Link href={`/blog?id=${x.id}`} as={`blog/${x.id}`}>
    <a>Read Post</a>
 </Link>

Now it's re-rendering the /blog page on every click while adding the id to the URL.

url error gif

tgrecojs avatar Aug 23 '17 23:08 tgrecojs

@tgrecojs Hope this code snippet helps

    const href = `/journal?home=${this.state.article.path[0]}&articlePath=${this.state.article.path[1]}&file=${this.state.article.path[2]}`
    const as = `/journal/${this.state.article.path[0]}/${this.state.article.path[1]}/${this.state.article.path[2]}`

Would render as https://www.someURL.com/journal/articlePath/morePath/evenMorePath

ugiacoman avatar Aug 24 '17 00:08 ugiacoman

I understand how the path's work @ugiacoman that's not my issue. If you can see in my initial report, i'm successfully navigating to each blog post however the issue is that weird 404 error.

tgrecojs avatar Aug 24 '17 03:08 tgrecojs

@tgrecojs I made an example repo https://github.com/sergiodxa/next-custom-query (deployed: https://next-custom-query.now.sh/blog), there you can see how to use the custom urls without reloading and a 404 using the next/link component as described above.

sergiodxa avatar Aug 24 '17 03:08 sergiodxa

@sergiodxa I appreciate that! I just checked it out and added some async calls to the google blogger API which is what i'm currently pinging. Below, you can see how it's navigating to the route but it's not correctly loading the data.

Removed gif

Now this issue is a slightly different reproduction of this issue than what is shown in my first comment. When I first ran into this problem, I thought it was because I was using redux and that the application state wasn't carrying over the post/id pages. This led me to integrating a new API call for each post, just like I integrated in the app you've created, however I still found that I had the same problem.

I was running into some trouble forking your repo 😬 so you can find my code here -> https://github.com/tgrecojs/next-custom-query-async. Let me know you're thoughts! 😄

tgrecojs avatar Aug 24 '17 04:08 tgrecojs

It looks you're getting a 400, when forking your project I'm getting that same error with a usageLimits error message. That's probably why you're getting that error.

sergiodxa avatar Aug 24 '17 06:08 sergiodxa

Ok. I'll investigate a little further tomorow but I want to point out that the video I posted when opening the issue is not making an API request on the /blog/:id route. Instead, it's filtering the posts from my redux store which is collected on the /blog route transition so that couldn't be the reason I arrived at this issue. I only implemented the 2nd API call (made inside blog/:id) as I thought it had something to do with my redux-store but found myself receiving the same error but that's not the current implementation in my code.

As soon as I realized that it threw the same bug, I switched back to using only one API call (Inside /blog) and then filtering for the necessary blog post by id.

tgrecojs avatar Aug 24 '17 06:08 tgrecojs

@sergiodxa I can confirm that it's not usage limits. You'll see that the id isn't always being correctly passed to the /blog/:id route... I've attached the 400 error that i'm receiving. You'll see that id is undefined and (in this instance) it seems to be why i'm getting the error.

I've included another video below. Any thoughts as to why it may be behaving this way? I haven't changed the <Link /> tags inside this repository yet still having the issue 😬 .

next-custom-query err

Again, I want to point out that the video above shows my reproducing the error inside of the example repo you've created - https://github.com/sergiodxa/next-custom-query.

tgrecojs avatar Aug 24 '17 17:08 tgrecojs

I'm also getting 404 flashing without any custom routes. Just a file called orders.js in pages/ with this code:

import { Component } from 'react';

class Orders extends Component {
  render() {
    return (
      <div>
        <h2>My Orders</h2>
      </div>
    )
  }
}

export default Orders;

wesbos avatar Aug 25 '17 13:08 wesbos

Seems like a pretty critical issue 😅

timneutkens avatar Aug 26 '17 07:08 timneutkens

I tried to replicate it and is because the trailing slash at the end of the URL, if you go to /orders you don't see the 404 but if you go to /orders/ then you see that 404 flashing before the content, this happens either in dev and production mode.

sergiodxa avatar Aug 26 '17 08:08 sergiodxa

Ah this is a known issue. https://github.com/zeit/next.js/issues/1189

timneutkens avatar Aug 26 '17 09:08 timneutkens

My issue didn't arise due to a trailing slash so there's got to be something else, right?

I've made some changes in my code and i've deployed 2 different versions, one using prefetch and one that doesn't. I want to note that neither application is now able to successfully load a post/:id route.

With Prefetch - https://tgrecojs-hltqztsjpx.now.sh/

One noticed that when I use prefetch in the link, it now throws a Page Does Not Exist Error when rendering the links for each blog post. You can see a screenshot of my console below.

screen shot 2017-08-26 at 1 06 25 pm

Without Prefetch- https://tgrecojs-qzpspdjrin.now.sh/

When I don't use prefetch, the error shown above no longer exists but it does not render the application. When I initially posted this issue, my component was initializing my blog for each post/:id component. I've since removed that since now individual posts never render.

Let me know if I can provide any more info that can help diagnose this issue. 🤔

tgrecojs avatar Aug 26 '17 17:08 tgrecojs

@tgrecojs I have almost the exact same problem. prefetch did not solve my problem.

The behavior I get is the following:

  • The user clicks on the link
  • A 404 error shows up
  • The page reloads (??)
  • After reloading, the correct page is displayed

I hope this can help debug the issue!

Extra info: Node v6.10.3, Chrome Version 60.0.3112.90

EDIT: Deployed the app so you guys can test it https://datahub.now.sh/, it's the link /@[username] that has this behaviour

Theo- avatar Aug 27 '17 12:08 Theo-

@Theo- the href in that next/link is user?id=1, can you try /user?id=1 instead? I think the 404 is because Next.js is trying to get to /current/path/user?id=1 instead of /user?id=1, but when the page reload it works fine because of the server render.

sergiodxa avatar Aug 27 '17 19:08 sergiodxa

@sergiodxa that did it. Thank you!

Theo- avatar Aug 27 '17 19:08 Theo-