Next.js - 如何添加 <link><head> 内的标签带有文字 onload 属性字符串值? [英] Next.js - How to add a <link> tag inside the <head> with literal onload attribute string value?
问题描述
在 Next.js 项目中,我想在 <head>
中获得一些具有完全相同内容的初始 HTML:
On a Next.js project, I'd like to get some initial HTML with this exact same content inside the <head>
:
<link href="..." rel="stylesheet" media="print" onload="this.media='all'" />
在我的代码中,在 Next.js 的 <Head>
组件中,是:
What I have in my code, inside Next.js's <Head>
component, is:
{ /* @ts-ignore */ }
<link href="..." rel="stylesheet" media="print" onload="this.media='all'" />
没有 @ts-ignore
它说:
类型DetailedHTMLProps<LinkHTMLAttributes, HTMLLinkElement>"上不存在属性onload".您指的是 'onLoad' 吗?ts(2322)
Property 'onload' does not exist on type 'DetailedHTMLProps<LinkHTMLAttributes, HTMLLinkElement>'. Did you mean 'onLoad'? ts(2322)
如果我使用 onLoad
而不是 onload
我得到:
And if I use onLoad
instead of onload
I get:
类型 'string' 不可分配给类型 '(event: SyntheticEvent
Type 'string' is not assignable to type '(event: SyntheticEvent<HTMLLinkElement, Event>) => void'. ts(2322)
问题是我得到的服务器端生成的 HTML 有:
The problem is that the server-side generated HTML that I get has:
<link href="..." rel="stylesheet" media="print" />
只有在页面重新水化后,它才会更新为:
And only once the page has rehydrated it updates to:
<link href="..." rel="stylesheet" media="all" onload="this.media='all'">
我在 GitHub 上发现了这个问题,但它没有帮助,因为我使用的不是 Google 字体而是 Typography.com,所以我不能使用 next-google-fonts
:https://github.com/vercel/next.js/issues/12984
I've found this issue on GitHub but it doesn't help as I'm not using Google Fonts but Typography.com, so I can't use next-google-fonts
: https://github.com/vercel/next.js/issues/12984
我正在考虑将 ref
添加到该 link
标记并使用 setAttribute
设置属性,这有望在服务器上运行-side 也是如此,但想知道是否有更简单的方法.
I'm thinking about adding a ref
to that link
tag and setting the attribute using setAttribute
, which will hopefully work on the server-side as well, but wondering if there's a simpler way to do it.
推荐答案
所以我最终使用带有 dangerouslySetInnerHTML
在 自定义 _document.js
.总而言之,它应该是这样的:
So I eventually fixed this using a <style>
tag with dangerouslySetInnerHTML
in a custom _document.js
. All together it should look like this:
<link rel="preconnect" href="https://fonts.googleapis.com" crossOrigin="anonymous" />
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;600&family=Karla:wght@700&display=swap" as="style" />
<style dangerouslySetInnerHTML={ {
__html: `</style>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;600&family=Karla:wght@700&display=swap"
media="print"
onload="this.media='all';"
/>
<style>`
} }></style>
<noscript>
<link
rel="stylesheet"
type="text/css"
href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;600&family=Karla:wght@700&display=swap" />
</noscript>
生成以下输出:
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin="anonymous"/>
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;600&family=Karla:wght@700&display=swap" as="style"/>
<style></style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;600&family=Karla:wght@700&display=swap" media="print" onload="this.media='all';" />
<style></style>
<noscript><link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;600&family=Karla:wght@700&display=swap"/></noscript>
不漂亮,但比在 <head>
中包含 <div>
更好(并非所有浏览器都正确解释).
Not pretty, but better than having a <div>
inside the <head>
(which is not interpreted correctly by all browsers).
有一个 开放 RFC 来创建一个 RawHTML
组件或扩展 Fragment
以接受 dangerouslySetInnerHTML
以便在没有 hack 的情况下实现这样的事情,但它已经创建了一年多.
There's an open RFC to create a RawHTML
component or extend Fragment
to accept dangerouslySetInnerHTML
so that something like this is possible without hacks, but it's been more than a year since it was created.
此外,还有对此进行了相当长的讨论以及一些不同的似乎有效的解决方案(黑客).
Also, there's quite a long discussion about this as well with a few different solutions (hacks) that seem to work.
您可以在此处查看解决方案:https://andorratechvalley.com/
You can see the solution working here: https://andorratechvalley.com/
这篇关于Next.js - 如何添加 <link><head> 内的标签带有文字 onload 属性字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!