当渲染服务器端(无getInitialProps)时,如何访问_app中的_app中的next.js上下文数据?
我正在从_app.js
文件中删除getInitialProps
,并且需要能够在渲染服务器端或客户端。为此,我需要在_app组件中访问下一个。我该如何访问它?
const getAcceptLanguageString = (): string => {
let req: IncomingMessage | undefined;
if (!isClientSide()) {
return req?.headers['accept-language'] ?? 'en-US';
}
return window.__NEXT_DATA__?.props?.acceptLanguage ?? '';
};
const MyApp = ({
Component,
apolloClient,
router,
}) => {
const acceptLanguage = getAcceptLanguageString();
// Do something with `acceptLanguage`
return <Component />;
};
I am removing getInitialProps
from the _app.js
file and need to be able to get the user's accept-language
either when rendering server-side or client-side. In order to do that I need to have access to the Next.js context in the _app component. How can I get access to that?
const getAcceptLanguageString = (): string => {
let req: IncomingMessage | undefined;
if (!isClientSide()) {
return req?.headers['accept-language'] ?? 'en-US';
}
return window.__NEXT_DATA__?.props?.acceptLanguage ?? '';
};
const MyApp = ({
Component,
apolloClient,
router,
}) => {
const acceptLanguage = getAcceptLanguageString();
// Do something with `acceptLanguage`
return <Component />;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论