M
M
MRcracker2021-10-13 10:33:09
React
MRcracker, 2021-10-13 10:33:09

How to optimize the next.js function?

Good afternoon. I am using next.js. I have a menu component. It is located outside of the pages folder. I get menu data from the cms system using the getStaticProps request.

getStaticProps - only works on pages in the pages folder. Outside the folder does not work. Tell me, how can I optimize the files and code so that I do not add a getStaticProps request to each page to get menu data?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Ivanovich, 2021-10-13
@MRcracker

In general, there is such a problem in Next that there is no optimal way to fetch something once, for example, global settings, and then use it in the application. The closest solution would be to use getStaticProps in _app, but for some reason this is currently not possible.
One of the Next developers suggests the following solutions:

  • I want to fetch data once, and only once, until the next build, globally for my entire application. solution
  • I want to share a layout between pages, while persisting React state to allow nested layouts. solution
  • Finally, of course, you can create a shared function fetching your data and use it inside getStaticProps for every page

As for me, it would be worth using this option for your task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question