【Note】一些随手记的灵感与发现片段
"一些随手记的灵感与发现片段"
[updated] 最后更新于 2025-02-26
Next.js
[2025-02-26] Next.js 的 server action 在返回时,只能返回普通的、可以序列化的对象
考虑该 action:
/** * serverAction:发送信息渠道的邮箱验证邮件 * @param userId * @param email */export async function sendInfoChannelEmailValidation(userId: string, email: string) { try { const token = getMagicLinkToken({ email, userId, timestamp: Date.now() }) const encodeToken = encodeURIComponent(token) const url = `${BASE_URL}/api/auth/email-verify?token=${encodeToken}&type=infoChannelEmailValidation` const sendEmail = await sendVerificationEmail({ identifier: email, url, token, from: "Memenews <no-reply@newsdiy.cn>", theme: { colorScheme: 'auto' }, }, "infoChannelEmailValidation") return { success: sendEmail.success, message: sendEmail.message, } } catch (error) { console.error(`在 serverAction:sendInfoChannelEmailValidation 中发生错误: ${error instanceof Error ? error.message : '未知错误'}`) return { success: false, message: `发送信息渠道的邮箱验证邮件失败: ${error instanceof Error ? error.message : '未知错误'}`, } }}
其中,我没注意到 sendVerificationEmail 的返回值的 data 字段可能会是 SMTPTransport 类型,因此在早版本的代码里,直接返回 sendEmail ,导致在生产环境里我无法调通该 action。
开发环境下 next.js 比较宽松,因此没在开发环境发现这个问题。
Web
[2025-02-26] fetch
的 POST
方法发起请求时,需要注意 Content-Type
的设置。
例如下面的 Server Action:
const result = (await fetch(`${AUTH_SERVER_URL}/api/user/email`, { method: "POST", body: JSON.stringify({ userId, email, validationType: "accountEmailValidation" }) }))
原本 ofetch 之类的库用习惯了,导致我向后端发送请求时永远提示我请求体字段不完整,让我纳闷了很久。后面我才想起来,
要加 Content-Type
头指明请求体是 JSON 格式。
const result = (await fetch(`${AUTH_SERVER_URL}/api/user/email`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ userId, email, validationType: "accountEmailValidation" }) }))
这个坑其实我以前就踩过,只是后来忘了。今天重新 mark 一下。
[2025-02-25] 在 Web 开发时,对于 token 的 URL 传递,需要注意特殊字符的转义(token特别容易触发这一点)。
例如 token:
U2FsdGVkX1/32F2qX7p3faHYPLPjhpJccxgD1JzkupT43s872Ea1BnV7iWKMETES3Xl+ICLBPkamABfmHU7Eww==
其中的 +
号,在 URL 中会被转义为空格,所以需要进行 URL 编码。
对于 URL 的编码转义规则在 RFC 3986 中定义。
DeepseekR1
[2025-02-20] 在应用传统提示词的情况下,似乎 DeepSeekR1 很喜欢使用 当...时
这样的句式。
【END】