Developer Quality Of Life Improvement (A series of improvements to Next.js) - Layouts, Middlewares, Error Pages, Validations #16400
ConsoleTVs
started this conversation in
Ideas
Replies: 1 comment
-
Related to Layouts: #8193 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First of all, I want to thank all the people involved in this amazing project. I find most of the decisions and features over the last
year to be amazing and I'm very exited for the direction the project is heading to. That said, I would like to propose a few
quality of life improvements for all the developers that use Next.js.
I will now propose a few features that Next.js could have in future releases to improve the development experience. Please don't hesitate to leave feedback on all topics presented.
Layouts
Most websites and web apps often have a common layout shared across multiple pages. Currently, Next.js does offer an option to modify the base
_app
and_document
of the page. However, this have limitations and it's still not good enough for all cases.More info: https://adamwathan.me/2019/10/17/persistent-layout-patterns-in-nextjs/
Current solutions
Wrap each page with a
<Layout>
component<Layout>
on each page.<Layout>
is disposed and re-renderer each navigation even if both pages have the same<Layout>
.Add a common
<Layout>
to the_app
Add a dynamic
<Layout>
to the_app
using a static property._app
based on it).Add a dynamic
<Layout>
to the_app
using a function._app
).Add a dynamic
<Layout>
to the_app
using a getStaticProps.Proposed solutions
The proposed solution goes around using existing ways of defining
getStatcProps
functions in pages.My suggestion is to export a getLayout function or property. This should be the type of it:
_app
option 1_app
option 2Ability to setup nested
_app
pagesThe hability of adding multiple nested
_app
pages is also of great interest, there may also be that all subpages of a given path use the same sub-layout or even a complete different layout.path
->layout
example.com/*
->main
example.com/blog/*
->blog
example.com/subpage/*
->main
->subpage
The best way would be to use the 2nd option of the layouts solution since it allows you to completly drop the layout behind you. Otherwise, it will always be nested. Therefore, the following files could correspond to the 3 paths given above:
In this last one however, there's _app in
pages/_app
andpages/subpage/_app
. The first_app
is rendered with thefollowing information:
ComponentLayout
:({children}) => children
This is needed because the subpage_app
will already define where the component layout is located and we should not repeat it. This pretty much makes that component dissapear.Component
: The component is replaced with MyApp fromsubpage/_app
The result is then:
Middlewares
Middlewares are also very common between pages, without looking further, most web application use some sort of authentication.
Current solutions
The only way to apply middleware to pages is currently to use a React technique that used HOC components to achieve it. For example, you may define a HOC auth component like:
Then apply it to the page like:
This introduces 2 main concerns:
Proposed solutions
Error Pages
Next.js currentnly only supports custom 404 and 500 pages. My suggestion here is to just allow arbitrary number of errors to be handled correctly on the front-end. For example; a 403, 405 HTTP error codes are also common, or even 422 for invalid request parameters (like invalid dynamic data, see validation chapter below, like /user/invalid-user-id123). I would recommend to just use the current pages structure and allow the dev to add files like what's already done with 404.js. So if the dev wants to add a 405 page:
pages/405.js
. Ideally I would recommend to have a specific errors page, but this is a good enough solution.Validation
Similar to Nuxt.js validation, allowing a page to have a validation phase (sort of as a middleware) will allow us to have a specific place to check for invalid request parameters or invalid properties to the page (as mentioned above, an invalid user ID for example).
Beta Was this translation helpful? Give feedback.
All reactions