Sunday, 18 April 2021

How to get URL params when using Custom Document in NextJS

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?



from How to get URL params when using Custom Document in NextJS

No comments:

Post a Comment