Dan Abramov 的这篇文章是个陷阱!它提出了一种巧妙的方法,将 JSON 对象以流式传输的方式传输到客户端,在传输完成之前提供 JSON 的结构,然后在更多数据到达时填补空白……结果却变成了一个 React 服务器组件工作原理的隐秘教程。
忽略其隐秘性,它所描述的假想流式 JSON 格式是一个有趣的思维练习:
{ header: "$1", post: "$2", footer: "$3" } /* $1 */ "Welcome to my blog" /* $3 */ "Hope you like it" /* $2 */ { content: "$4", comments: "$5" } /* $4 */ "This is my article" /* $5 */ ["$6", "$7", "$8"] /* $6 */ "This is the first comment" /* $7 */ "This is the second comment" /* $8 */ "This is the third comment"
在每个块之后,可以构建到目前为止的完整 JSON 文档,并且 Dan 建议沿途交错Promise()
对象,用于尚未完全解析的占位符 – 因此在收到上面的块$3
之后(请注意,块可以无序提供)文档将如下所示:
{ header: "Welcome to my blog", post: new Promise(/* ... not yet resolved ... */), footer: "Hope you like it" }
我暂时保留这个想法,以便将来有机会尝试它。
标签: json 、 react 、 javascript
原文: https://simonwillison.net/2025/Jun/1/progressive-json/#atom-everything