node.js - 将数据放在 ServersideProps 上
问题描述
是否可以通过 Next.js 中的 getServersideProps() 从客户端的服务器端配置文件中获取数据?如何将数据放入 props 中?或者如何以其他方式在客户端上获取它?我publicRuntimeConfig
在 next.config.js 中尝试过,但它是未定义的,我不知道为什么......
解决方案
很难确切地说出发生了什么,但我从经验中有一个想法:您需要确保在包含任何使用的模块nextJSApp.prepare()
之前next/config
调用。
举个例子,
// SomeComponent.tsx
import { getConfig } from 'next/config'
const config = getConfig()
export interface X { ... }
// server.ts
import { X } from './SomeComponent'
app.prepare().then(...)
这失败了,因为模块首先加载并且配置app.prepare
在完成之前尚未初始化。
对此的解决方案是,import(...)
如果您只需要一个类型,则使用 TypeScript 的语法,或者require
在运行时使用节点的动态解析。
推荐阅读
- excel - 使用 maatwebsite excel / laravel 将日期数据从 excel 文件导入数据库
- mongodb - 在 Spring Boot 中格式化 mongo 聚合结果
- mysql - 使用 MySQL 生成 JSON 输出
- next.js - Next.js 动态页面
- css - CSS Grid:段落标签破坏布局
- javascript - 如何阻止自定义模式扩展到屏幕之外
- reactjs - 删除 Contentful CreateClient API 请求的默认标头
- python - 未找到 Python Office 365 REST API 模块
- javascript - 如何连接到通过 Rust 托管的 Web Socket 服务器?
- javascript - 如何仅对对象中的某些属性求和