你能在 next.config 中獲取數據嗎 (Can you fetch data in next.config)


問題描述

你能在 next.config 中獲取數據嗎 (Can you fetch data in next.config)

有什麼方法可以在 next.config.js 文件中獲取數據,我正在嘗試設置 i18n,但希望從 CMS 獲取語言環境數組,而不是由開發人員靜態輸入。


參考解法

方法 1:

From Next.js docs: (https://nextjs.org/docs/api‑reference/next.config.js/introduction)

next.config.js is a regular Node.js module, not a JSON file. It gets used by the Next.js server and build phases, and it's not included in the browser build.

It means that you can do something like this:

module.exports = {
  async headers() {
    const response = await fetch("https://example.com/langs");
    const dt = await response.json();
    return [
      {
        source: "/api/dashboard",
        headers: [{ key: "Lang", value: dt.map((d) => d.lng).join(",") }],
      },
    ];
  },
};

Obviously the above code can be done inside the endpoint handler but I just wanted to give an example to prove it's working.

(by Aiden BarrettSirwan Afifi)

參考文件

  1. Can you fetch data in next.config (CC BY‑SA 2.5/3.0/4.0)

#internationalization #reactjs #Next.js






相關問題

志願者翻譯對 Delphi 2009 應用程序進行本地化的過程? (Process for localization of Delphi 2009 app by volunteer translators?)

Netbeans 菜單和 i18n (Netbeans menu and i18n)

Menggunakan Perpustakaan Penutupan pada aplikasi Phonegap (Android) (Using Closure Library on Phonegap(Android) application)

支持 SDK 但不支持 PhoneGap 的印度語語言 (Indic Language Support for SDK but not PhoneGap)

翻譯 symfony2 路由錯誤 (translation symfony2 routing error)

Django 沒有正確翻譯網站 (Django not translating the site properly)

django模型翻譯404 (django modeltranslation 404)

您是否知道用於編輯/翻譯資源(.rc)文件的好的程序? (Do you know of a good program for editing/translating resource (.rc) files?)

Struts 2 動作調用丟失 xwork i18n 語言設置 (Struts 2 action call loses the xwork i18n language setting)

移動應用測試 (Mobile application testing)

nuxt-i18n 的延遲加載語言環境 (Lazy load locales for nuxt-i18n)

你能在 next.config 中獲取數據嗎 (Can you fetch data in next.config)







留言討論