-
Update: We’re excited to announce Next.js 13, which includes the new Inside the |
Beta Was this translation helpful? Give feedback.
Replies: 52 comments 184 replies
-
This is just an initial restriction as we want to make sure we provide the right abstraction for it in _app. For example we need to figure out what would happen if someone decides to add The restriction will be lifted at a later point. |
Beta Was this translation helpful? Give feedback.
-
Same here. I understood that "This is just an initial restriction", but: If I had a shared component in |
Beta Was this translation helpful? Give feedback.
-
It looks like that I.e.
Once I move my code from the |
Beta Was this translation helpful? Give feedback.
-
How can SSG be used for other page extensions like MDX (using I'm using an |
Beta Was this translation helpful? Give feedback.
-
@timneutkens When the restriction for using In my case that'd be very helpful since we would want to do some static fetching (e.g. language translations for page content) during build time with This follows the same line of thought as #11424. |
Beta Was this translation helpful? Give feedback.
-
This is actually useful to keep our codebase |
Beta Was this translation helpful? Give feedback.
-
I've read through everything here but I'm still a little fuzzy on what exactly the tradeoff on using If it's only that treeshaking isn't done I can understand why this feature has been deprioritised. But if fetching data in |
Beta Was this translation helpful? Give feedback.
-
I'm implementing dark mode and I need to run a script when a page is visited to determine the user theme before rendering. I'd like to optimize this script using A long time ago when this const script = minify(rawScript);
export default function Home() {
return (
<>
<h1>Hello</h1>
<script dangerouslySetInnerHTML={{__html: script}} />
</>
);
} Please, please, please, tell me if the above approach has issues that I'm not aware of! Now, the export default function Home({ script }) {
return (
<>
<h1>Hello</h1>
<script dangerouslySetInnerHTML={{__html: script}} />
</>
);
}
export async function getStaticProps() {
const script = await minify(rawScript);
return {
props: {
script,
}
}
} This approach has a downside: Furthermore, this also means the script is included in the I feel like Also, I feel like I have a large gap in my understanding because I don't know why I would love to hear your thoughts on this, and what you can recommend me 🙏 |
Beta Was this translation helpful? Give feedback.
-
if the problem is to minimize external api requests -the solution is to combine getting page and global data in one graphql request |
Beta Was this translation helpful? Give feedback.
-
Maybe it's the to implement this, due to the recent NextJS 10 release? Most i18n implementations will use the getStaticProps to fetch .json files and provide them as static information. Like export const getStaticProps = async (context) => {
const locale = context.locale || context.defaultLocale;
const { default: table = {} } = await import(`locales/${locale}.json`);
return { props: { table } };
}; Currently, this needs to be at the bottom of every page. |
Beta Was this translation helpful? Give feedback.
-
Is there currently any workaround to get some static data into the App component or a non-page component that might be used by App? I have dynamic routes and would like to show the pages in the website's navbar, but that's only possible if i can pass the page names/ids to App and that child component, which i would normally use |
Beta Was this translation helpful? Give feedback.
-
I'm sure all projects when reaching a certain size will need to include data for some component that appears in different pages. To fetch and attach this data to each generated page is wasteful both in time and goes against DRY. The issue has an accepted answer but no solution and has been ranking very high among the RFCs for almost a year now, is it being worked on? |
Beta Was this translation helpful? Give feedback.
-
Also, does the lack of updates on this issue have anything to do with the upcoming Server Components integration? I can imagine Next making dramatic architecture simplifications as a result, though I would really really hate to wait that long to get this piece sorted. |
Beta Was this translation helpful? Give feedback.
-
I've also needed solutions around this so I've created two different libs in this global-data-esque space. I've needed both for my own projects but sharing them here in case you find them useful. next-plugin-preval — pre-evaluate async functions and import them as JSON (truly de-duped across bundles)repo linkThis lib ships its own webpack loader that matches any You can import them in Its API looks like this: // header-data.preval.js
import preval from 'next-plugin-preval';
import db from 'your-db';
async function getHeaderData() {
const headerData = await db.query(/* query for header data */);
return headerData;
}
export default preval(getHeaderData()); // header.js
import headerData from './header-data.preval';
const { title } = headerData; // available as static data, deserialized from JSON
function Header() {
return <header>{title}</header>;
}
export default Header; It's pretty simple to understand but since it's a bundler-level optimization, it will not work with ISR or even SSR. Once it leaves the build, there is no refreshing it. next-data-hooks — `getStaticProps` as react hooks (nice for organization)repo link
This is how that looks: // header.js
import { createDataHook } from 'next-data-hooks';
import db from 'your-db';
// step 1: write a `getStaticProps` implementation (don't export it though)
const getStaticProps = async (context) => {
// (you don't have to return `props` though)
return await db.query(/* ... */);
};
// step 2: pass it into this function
const useHeader = createDataHook('Header', getStaticProps);
function Header() {
// step 3: use it your component.
// the data will be there synchronously at first render
const { items } = useHeader();
return // ...
}
// step 4: add the `dataHooks` static prop
Header.dataHooks = [useHeader];
export default Header; The idea is to propagate these things via the // layout.js
import Header from './header';
import Footer from './footer';
function Layout() {
return // ...
}
Layout.dataHooks = [
...Header.dataHooks,
...Footer.dataHooks,
];
export default Layout; You'll have to see the docs for the full story but hopefully, you can see enough of the pattern to make sense of it. Note that this doesn't actually de-dupe the header data from being fetched on every page. This lib's primary goal is organization, not performance. |
Beta Was this translation helpful? Give feedback.
-
Is there a timeline for when this will be released? Would be really useful for SSG apps to have global data. There isn't currently a good workaround if you want to stick with SSG. |
Beta Was this translation helpful? Give feedback.
-
In my case, it would be a great solution to have. Cause the website I am working on has data that are not gonna be changed or may change frequently. Having getStaticProps in the _app can lead to a great developing experience. Cause the website I am working on has a lot of redundant data from page to page. I am more of like thinking to fetch all the data from _app and then use context API or Redux to access them where I needed. |
Beta Was this translation helpful? Give feedback.
-
I cannot believe this was the problem. Been trying for hours.. hope there is a nice solution soon, it will be much better |
Beta Was this translation helpful? Give feedback.
-
Yeah I am also going to put my voice forward to say that the above response from @leerob doesn't actually address the issue or explain why it's not possible. Is there any plan to allow for a global place to get app data statically? |
Beta Was this translation helpful? Give feedback.
-
The first sobering moment in my honey-moon phase with nextJS. Compared to everything next can do, this would seem like a basic feature. |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
Hi everyone! Dropping in from the next.js team to let you know that we have heard the feedback here loud and clear and we are actively discussing the best way that we can solve for this. Same for layouts support - it's something we have actively staffed and are working on right now. As soon as we have something that we are confident enough to commit to publicly, we will share it 😁 |
Beta Was this translation helpful? Give feedback.
-
That's awesome, thanks for the update.
…On Tue, Apr 26, 2022, 8:04 PM Jeff Escalante ***@***.***> wrote:
Hi everyone! Dropping in from the next.js team to let you know that we
have heard the feedback here loud and clear and we are actively discussing
the best way that we can solve for this. Same for layouts support - it's
something we have actively staffed and are working on right now. As soon as
we have something that we are confident enough to commit to publicly, we
will share it 😁
—
Reply to this email directly, view it on GitHub
<#10949 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTB7SP75A5SNNQYJHTO7YTVHCVDRANCNFSM4LFHTLQA>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I did this and it works, will i have some problem doing this? export async function getStaticProps() {
const settings = await getSettings();
if (!settings) {
return { notFound: true,}
};
return {
pageProps: { settings }};
}
function MyApp({ Component, pageProps }: AppProps) {
useEffect(() => {
console.log(pageProps.settings)
},[]);
return (
<Layout>
<DefaultSeo
title={pageProps.settings ? pageProps.settings.meta_title : '' }
description={pageProps.settings ? pageProps.settings.meta_description : ''}
/>
<Component {...pageProps} />
</Layout>
)
}
export default MyApp |
Beta Was this translation helpful? Give feedback.
-
There are three potential solutions that can help people with this problem today:
I can provide example for every solution if anyone's interested. |
Beta Was this translation helpful? Give feedback.
-
I am interested in these examples!
That would be great.
…On Fri, May 6, 2022, 8:25 AM Jón Trausti ***@***.***> wrote:
There are three potential solutions that can help people with this problem
today:
1. Use file system cache to share data between pages.
2. (OR) Use in-memory cache with node's globalThis to share data
between pages. Note that globalThis is only available in Node version
>= 12.0.0.
3. Use Higher order function with higher order component to reduce the
friction of writing getStaticProps for every single page.
I can provide example for every solution if anyone's interested.
—
Reply to this email directly, view it on GitHub
<#10949 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTB7SJCBOTU7KMHHCVVLP3VIU2U5ANCNFSM4LFHTLQA>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Here's an example of higher order function (HOF) with higher order component (HOC) and caching in-memory with the Let's start with the higher order function. This is only applicable if you are used to writing getStaticProps function for every single page with the same data fetching code, which can cause code duplication and more friction when adding more pages. So to fix this we could use something called "Higher order function". Here's an example of a higher order function where we always fetch shared data such as header data. We will call the higher order function
Alright, next In a page component you would only need to call the HOF (Higher order function) if you don't need any additional data fetching for the page. Otherwise if you need to fetch additional data you would do this:
This code will make your life much easier when you are writing endless getStaticProps for the same data. Next we also want to use shared layout component that uses this shared header data. To do this we will write a higher order component called withPage. It's really simple:
So now when you write a page component, you can simply do this:
The higher order component will wrap the HomePage component with Now you have shared getStaticProps and layout combined. However, data fetching will still occur for every single page (multiple API calls, etc.), so how can we solve that in some neat way? That's where caching will come in. As you saw in our HOF above, we called a function called For caching I thought in-memory caching would not be possible, since each page is rendered in isolation. My co-worker then told me about something called
And luckily for us, the
So now data can be shared between pages without having to spam some API for new data every time, unless the cache has expired. I can also provide the disk cache solution if anyone's interested, but the I hope this helps anyone else who wants to solve this problem "today" without too much hassle. Maybe the NextJS team can also use this as an inspiration for a potential solution! :) |
Beta Was this translation helpful? Give feedback.
-
Happy to share the Layouts RFC is finally out: https://twitter.com/vercel/status/1528839321070993409 🥳 |
Beta Was this translation helpful? Give feedback.
-
That is awesome.
What is the usual timeline between an RFC and when it is released for
consumption?
…On Mon, May 23, 2022 at 2:59 PM Lee Robinson ***@***.***> wrote:
Happy to share the Layouts RFC is finally out:
https://twitter.com/vercel/status/1528839321070993409 🥳
—
Reply to this email directly, view it on GitHub
<#10949 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTB7SKXCICBEL5TDHSKN2DVLP5UZANCNFSM4LFHTLQA>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
This is possible with the help of this library. This will include the props declared in config file of this library, at _app level. Configuration (common-props.config.js)can be like:
So this data will be available in _app.js by using useCommonProps hook,
|
Beta Was this translation helpful? Give feedback.
-
Update: We’re excited to announce Next.js 13, which includes the new Inside the |
Beta Was this translation helpful? Give feedback.
Update: We’re excited to announce Next.js 13, which includes the new
app/
directory (beta) with support for nested layouts, colocation of data fetching, streaming, Server Components by default, and much more.Inside the
app/
directory, there is a powerful new way to fetch data with React’suse()
hook and the extended Webfetch()
API. This allows you to colocate data fetching directly inside components for the most flexibility, including inside layouts.