reactjs - gatsby ssr - 在其他插件之前将脚本添加到标题
问题描述
我正在使用 Gatsby,需要在其他插件之前在标题中添加一个脚本。
如果我通过添加它gatsby-ssr.js
exports.onRenderBody = ({ setHeadComponents }) => setHeadComponents([#MY-SCRIPT#]);
它是最后添加的。
如何在其他插件之前添加它?
解决方案
除了onRenderBody
,用于onPreRenderHTML
重新排序您的脚本。
exports.onRenderBody = ({ setHeadComponents }) =>
setHeadComponents([
<script key='myscript' src='...' />
]);
exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
const headComponents = getHeadComponents()
// reorder your array with the sort method, by putting your item at top
const orderedComponents = headComponents.sort((item) => (item.key === 'your-key' ? -1 : 1)); const orderedComponents = reorder(headComponents)
replaceHeadComponents(orderedComponents)
}
有关详细信息,请参阅 SSR API 上的 Gatsby 文档。
推荐阅读
- sql - 如何为多个组找到每个组中的最大值,并将其输出到一个表中
- nltk - Python NLTK 分数从 0 到 10
- spring - Spring Security 多个 AuthenticationProviders
- php - 如何在 CI 中结合 get_where 和 like?
- angular - 如何控制使用 ngModel 绑定的值的类型(数字被扭曲为字符串)?
- asp.net-core - HumanizerMetadataProvider 实现不适用于 Razor 页面模型
- java - 在 Android 应用中拍照并将其保存到图库中
- node.js - 在不更改 URL 的情况下快速重定向
- wordpress - 想不起编码博客的名字
- c# - 使用 CsvHelper 将 ENUM 转换为 int