Next.js - How to add a <link> tag inside the <head> with literal onload attribute string value?

这一生的挚爱 提交于 2020-12-15 06:23:08

问题


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'" />

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'" />

Without the @ts-ignore it says:

Property 'onload' does not exist on type 'DetailedHTMLProps<LinkHTMLAttributes, HTMLLinkElement>'. Did you mean 'onLoad'? ts(2322)

And if I use onLoad instead of onload I get:

Type 'string' is not assignable to type '(event: SyntheticEvent<HTMLLinkElement, Event>) => void'. ts(2322)

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'">

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

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.

来源:https://stackoverflow.com/questions/64121456/next-js-how-to-add-a-link-tag-inside-the-head-with-literal-onload-attribut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!