最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get URL params when using Custom Document in NextJS - Stack Overflow

programmeradmin1浏览0评论

I am using NextJS to product SSR pages, and these pages are language-specific. I would like to set the lang property to declare the language of the text.

So far I have:

import Document, { Html, Head, Main, NextScript } from "next/document"
import Router from "next/router"

class MyDocument extends Document {
  render() {
    const { lanaguage } = Router.router.query
    return (
      <Html lang={language}>
        <Head />
        <body className="antialiased text-white text-base font-sans">
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

However, Router.router is always null when using a Custom Document (_document.js). Is there another way to get the URL params/query when using a Custom Document?

I am using NextJS to product SSR pages, and these pages are language-specific. I would like to set the lang property to declare the language of the text.

So far I have:

import Document, { Html, Head, Main, NextScript } from "next/document"
import Router from "next/router"

class MyDocument extends Document {
  render() {
    const { lanaguage } = Router.router.query
    return (
      <Html lang={language}>
        <Head />
        <body className="antialiased text-white text-base font-sans">
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

However, Router.router is always null when using a Custom Document (_document.js). Is there another way to get the URL params/query when using a Custom Document?

Share Improve this question edited Oct 1, 2022 at 10:54 juliomalves 50.6k23 gold badges178 silver badges169 bronze badges asked Jul 14, 2020 at 10:59 CharklewisCharklewis 5,7016 gold badges40 silver badges82 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I have been able to solve this by using:

import Document, { Html, Head, Main, NextScript } from "next/document"

class MyDocument extends Document {
  render() {
    const { lanaguage } = this.props.__NEXT_DATA__.query
    return (
      <Html lang={lanaguage}>
        <Head />
        <body className="antialiased text-white text-base font-sans">
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

I am unsure if it is bad practice to use this.props.__NEXT_DATA__. However, it does have the information I require.

Since Next.js v10, if you're using the built-in i18n routing, Next will automatically set the lang attribute of the document.

From the documentation:

Since Next.js knows what language the user is visiting it will automatically add the lang attribute to the <html> tag.

Next.js doesn't know about variants of a page so it's up to you to add the hreflang meta tags using next/head. You can learn more about hreflang in the Google Webmasters documentation.

发布评论

评论列表(0)

  1. 暂无评论