import type { GetServerSidePropsContext } from 'next';
const Page: NextPageWithLayout = () => {
return <p>hello world</p>;
};
export async function getServerSideProps(context: GetServerSidePropsContext) {
const { resolvedUrl } = context;
// Fetch data from external API
const res = await fetch(`https://api/title?path=${resolvedUrl}`);
const data = await res.json();
const { title } = data
// Pass data to the page
return { props: { title } };
}
pages/_app.js
import { Fragment } from 'react';
import Header from '@component/Header';
export default function MyApp({ Component, pageProps }) {
const { title } = pageProps;
return (
<Fragment>
<Header title={title} />
<Component {...pageProps} />
</Fragment>
););
}
1条答案
按热度按时间j7dteeu81#
您可以按照此https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props
示例:
pages/xxx.js
pages/_app.js