-
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.
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.
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.