next-i18next, next export 和非生成页面上的 404(带有 fallback true/blocking)

huangapple go评论73阅读模式
英文:

next-i18next, next export & 404 on non-generated pages (with fallback true/blocking)

问题

抱歉,以下是您要翻译的内容:

Having followed the documentation on i18next/next-i18next to configure i18n and then the instructions on this locize blog post on how to export static sites with next export, I am able to export localised versions of each page.

The problem is that pages that have not been statically generated return a 404, despite setting fallback: true in the getStaticPaths return object. The page works on my localhost but not when deployed with Vercel.

Code:

const ArticlePage: NextPageWithLayout<Props> = ({ article }: Props) => {
    const { i18n, t } = useTranslation('page/news/article')

    const router = useRouter()

    if (router.isFallback) return <div>Loading...</div>

    return <div>Article</div>
}

export const getStaticPaths: GetStaticPaths = async () => {
    let paths: { params: { aid: string; locale: TLocale } }[] = []

    try {
        const response = await api.get(`/articles?per_page=9999`)
        const articles = response.data.data as IArticle[]

        articles.forEach((a) => {
            getI18nPaths().forEach((p) => {
                paths.push({
                    params: {
                        aid: a.base_64_id,
                        locale: p.params.locale,
                    },
                })
            })
        })

        return {
            paths,
            fallback: true,
        }
    } catch (error) {
        return {
            paths,
            fallback: true,
        }
    }
}

export const getStaticProps: GetStaticProps = async ({ locale, params }) => {
    try {
        const article = await api.get(`/articles/${params?.aid}`)

        return {
            props: {
                ...(await serverSideTranslations(locale || 'en', [
                    'footer',
                    'header',
                    'misc',
                    'page/news/index',
                    'page/news/article',
                ])),
                article: article.data as IArticle,
            },
        }
    } catch (error) {
        return {
            notFound: true,
        }
    }
}

ArticlePage.getLayout = function getLayout(page: ReactElement) {
    return <Layout>{page}</Layout>
}

export default ArticlePage
"i18next": "22.4.9",
"next-i18next": "^13.1.5",
"react-i18next": "^12.1.5",

There is a warning in the console react-i18next:: You will need to pass in an i18next instance by using initReactI18next when entering a non-generated page (alongside the not-found error, of course). An issue raised about this warning is interesting, but I could not find an answer to my issue within: https://github.com/i18next/next-i18next/issues/1917.

Attempts to fix:

  • adding revalidate: 10 to the return object of getStaticProps
  • using fallback: 'blocking'
  • trying a few different variants of localePath in next-i18next.config including the recommendation featured here: https://github.com/i18next/next-i18next#vercel-and-netlify
  • adding react: { useSuspense: false } to next-i18next.config
  • combinations of the above
英文:

Having followed the documentation on i18next/next-i18next to configure i18n and then the instructions on this locize blog post on how to export static sites with next export, I am able to export localised versions of each page.

The problem is that pages that have not been statically generated return a 404, despite setting fallback: true in the getStaticPaths return object. The page works on my localhost but not when deployed with Vercel.

Code:

const ArticlePage: NextPageWithLayout&lt;Props&gt; = ({ article }: Props) =&gt; {
const { i18n, t } = useTranslation(&#39;page/news/article&#39;)
const router = useRouter()
if (router.isFallback) return &lt;div&gt;Loading...&lt;/div&gt;
return &lt;div&gt;Article&lt;/div&gt;
}
export const getStaticPaths: GetStaticPaths = async () =&gt; {
let paths: { params: { aid: string; locale: TLocale } }[] = []
try {
const response = await api.get(`/articles?per_page=9999`)
const articles = response.data.data as IArticle[]
articles.forEach((a) =&gt; {
getI18nPaths().forEach((p) =&gt; {
paths.push({
params: {
aid: a.base_64_id,
locale: p.params.locale,
},
})
})
})
return {
paths,
fallback: true,
}
} catch (error) {
return {
paths,
fallback: true,
}
}
}
export const getStaticProps: GetStaticProps = async ({ locale, params }) =&gt; {
try {
const article = await api.get(`/articles/${params?.aid}`)
return {
props: {
...(await serverSideTranslations(locale || &#39;en&#39;, [
&#39;footer&#39;,
&#39;header&#39;,
&#39;misc&#39;,
&#39;page/news/index&#39;,
&#39;page/news/article&#39;,
])),
article: article.data as IArticle,
},
}
} catch (error) {
return {
notFound: true,
}
}
}
ArticlePage.getLayout = function getLayout(page: ReactElement) {
return &lt;Layout&gt;{page}&lt;/Layout&gt;
}
export default ArticlePage
&quot;i18next&quot;: &quot;22.4.9&quot;,
&quot;next-i18next&quot;: &quot;^13.1.5&quot;,
&quot;react-i18next&quot;: &quot;^12.1.5&quot;,

There is a warning in the console react-i18next:: You will need to pass in an i18next instance by using initReactI18next when entering an non-generated page (alongside the not-found error of course). An issue raised about this warning is interesting but I could not find an answer to my issue within: https://github.com/i18next/next-i18next/issues/1917.

Attempts to fix:

  • adding revalidate: 10 to the return object of getStaticProps
  • using fallback: &#39;blocking&#39;
  • trying a few different variants of localePath in next-i18next.config including the recommendation featured here: https://github.com/i18next/next-i18next#vercel-and-netlify
  • adding react: { useSuspense: false } to next-i18next.config
  • combinations of the above

答案1

得分: 1

next export 不支持 fallback: true,如其文档所述。我应该一直使用 next build - 这也会在构建时生成静态 HTML,同时支持在资源不存在时进行运行时获取。

英文:

next export does not support fallback: true as per their documentation. I should've been using next build - this too will generate static HTML at build time whilst also supporting runtime fetching if the resource does not exist.

huangapple
  • 本文由 发表于 2023年2月16日 19:26:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定