admin管理员组文章数量:1394585
I'm new to Next.js and i'm having a hard time with passing the data from _document to the _app.
I need to pass pathname server-side from _document.ts
to _app.ts
and then into App
ponent so that i can inject custom tags in Head
element server-side. Every page will have specific links.
eg <link rel="x-default" hrefLang="x-default" href=";>
will be on page .
Current implementation looks like this:
getInitialProps in _document.tx
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
const { lang } = ctx.query;
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
pathname: ctx.asPath,
locale: getLocaleFromLanguage(lang as SupportedLanguages),
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
part of _app.ts
function App({ Component, pageProps }) {
console.log(pageProps)
let { locale } = pageProps;
if (!locale) {
locale = DEFAULT_LOCALE;
}
useEffect(() => {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles) {
jssStyles.parentElement.removeChild(jssStyles);
}
smoothscroll.polyfill();
}, []);
when i console.log(pageProps)
i only get eg { locale: 'en' }
, there is no pathname
property passed from _document.ts.
Do you have idea how can i pass props from _document to _app
I'm new to Next.js and i'm having a hard time with passing the data from _document to the _app.
I need to pass pathname server-side from _document.ts
to _app.ts
and then into App
ponent so that i can inject custom tags in Head
element server-side. Every page will have specific links.
eg <link rel="x-default" hrefLang="x-default" href="https://example./about-us">
will be on page https://example./about-us
.
Current implementation looks like this:
getInitialProps in _document.tx
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
const { lang } = ctx.query;
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
pathname: ctx.asPath,
locale: getLocaleFromLanguage(lang as SupportedLanguages),
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
part of _app.ts
function App({ Component, pageProps }) {
console.log(pageProps)
let { locale } = pageProps;
if (!locale) {
locale = DEFAULT_LOCALE;
}
useEffect(() => {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles) {
jssStyles.parentElement.removeChild(jssStyles);
}
smoothscroll.polyfill();
}, []);
when i console.log(pageProps)
i only get eg { locale: 'en' }
, there is no pathname
property passed from _document.ts.
Do you have idea how can i pass props from _document to _app
Share Improve this question asked Jul 3, 2020 at 8:30 Mare Mare 812 silver badges7 bronze badges 2- I have same issue like this. Did you solve it? – elyas.m Commented Jul 26, 2020 at 6:26
- @elyas.m nope, had to find totally different approach.. – Mare Commented Aug 17, 2020 at 12:04
1 Answer
Reset to default 9I had this issue and I used the req object to send my params from the _app to the _document. Based on NextJs resolution order we can use the req (IningMessage) object. NextJs resolution order on the server-side :
- app.getInitialProps
- page.getInitialProps
- document.getInitialProps
- app.render
- page.render
- document.render
and on the client-side:
- app.getInitialProps
- page.getInitialProps
- app.render
- page.render
Example (TS): I Sent a property named direction from the _app's getInitialProps function to _document.
(ctx.req as any).direction = organization.direction;
And in _document's getInitialProps :
const direction = ctx.req.direction;
本文标签: javascriptHow to pass pathname from document to app in NextjsStack Overflow
版权声明:本文标题:javascript - How to pass pathname from _document to _app in Next.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744097054a2590472.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论