Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  73] [ 3]  / answers: 1 / hits: 10133  / 5 Years ago, sun, april 28, 2019, 12:00:00

I'm new to next.js and as a first step, before I start developing the actual app, I'm following the docs to learn the basics, and right now, I'm struggled trying to get the prefetch working, since all the preloaded requests are returning 404 error.



So what's wrong with my code? How can I solve this problem?



The demo repository is on github.



enter


More From » reactjs

 Answers
6

Notice that only express server knows how to process URL of this kind /post/:id, next.js doesn't know it so next.js tries to prefetch unexistent pages (and you may see in the Chrome console output).



You may fix this behaviour easily: just need rewrite your links this way



<Link href={`/post/?id=${show.id}`} as={`/post/${show.id}`} prefetch>


As a result only one query to prefetch post.js page will be executed.
enter



This technique is called route masking you may read more about in in the Next.js tutorial



An update:
It seems that the question is more about how prefetch feature actually works in Next.js so I will try to explain it.
Without prefetch prop on the Link Next.js will load related chunk on demand (when a user clicks link) so it will cause a small delay to load and parse javascript. The prefetch prop allows you to remove this delay because javascript will be loaded as soon as possible after application start. In both cases new page will be rendered in the browser like in a usual React app.


[#7859] Thursday, April 25, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hailey

Total Points: 355
Total Questions: 91
Total Answers: 91

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
hailey questions
Sun, Jul 11, 21, 00:00, 3 Years ago
Mon, Nov 2, 20, 00:00, 4 Years ago
Fri, Jul 24, 20, 00:00, 4 Years ago
Fri, Aug 30, 19, 00:00, 5 Years ago
;