-
Notifications
You must be signed in to change notification settings - Fork 27.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a way to get data on Head and Page from a single database query #44595
Comments
The following example would create a single query, but it is not what I would like to have since it adds an HTTP request that I think is unnecessary. Single query to the Database but adds unnecesary HTTP request overhead// head.jsx
export default async function Head() {
const request = await fetch('http://localhost:3000/api/pageInfo');
const pageInfo = await request.json();
return (
<>
<title>{pageInfo.title}</title>
<meta name="description" content={pageInfo.description} />
<link rel="icon" href="/favicon.ico" />
</>
);
} // page.jsx
export default async function Home() {
const request = await fetch('http://localhost:3000/api/pageInfo');
const pageInfo = await request.json();
return (
<main>
<h1>{pageInfo.title}</h1>
<p>{pageInfo.data} </p>
</main>
);
} // pages/api/pageInfo.js
import { getPageInfoFromDatabase } from '../../database';
export default async function handler(req, res) {
const pageInfo = await getPageInfoFromDatabase();
res.status(200).json(pageInfo);
} |
This looks like a cool proposal! 🙌 The passing of props from Also feels like the export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
} |
@sebmarkbage @leerob @timneutkens if these proposals above are not tenable and Vercel has no plans on creating these features, are there any alternative plans on offering some new APIs like
|
You can use import {cache} from "react";
const getPageInfoFromDatabase = cache(function getPageInfoFromDatabase() {
}); |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Describe the feature you'd like to request
To start, I want to thank you for the awesome work. I have been enjoying Next.js 13 a lot and all these amazing features that you bring to us.
Experimenting with the new app directory architecture, I found it very hard to get the same data in
Head.jsx
andPage.jsx
from a single query from the database.In the following example, I am trying to get the title of the page for the
Head
, and using this title ash1
as well.Right now, this is going to result in two different database queries:
Two separate queries to the Database
I also created a reproduction example that presents query data directly from the page. In the example, it can be tested how the direct call approach results in multiple function call.
https://github.com/Josehower/next-13-data-fetching-demo
Describe the solution you'd like
A possible solution would be either:
Head
,Layout
, andPage
components so that I can perform a single query to get data on all of them without using an API. Similar to how you can pass props from_app.js
to the page componentsA possible API for this communication may be this:
Describe alternatives you've considered
I am aware that Next.js perform a fetch deduping when
Head
andPage
perform a fetch to the same URL (API routes for example). This theoretically would solve my issue but will add some unnecessary HTTP request overhead.The text was updated successfully, but these errors were encountered: