onLoad attribute on a link tag in entry-server not running
I'm on @solidjs/start
0.7.7
and trying to add an onload="this.onload=null;this.rel='stylesheet'"
on a link tag. The purpose is more widely explained here.
Problem with this approach is that typescript keeps shouting Type 'string' is not assignable to type 'EventHandlerUnion<HTMLLinkElement, Event> | undefined'
. So I rewrote the single liner above to the following:
And I don't see the console.log anywhere. I checked on the console tab, the terminal I ran "pnpm dev" on... nothing
How should I handle this? I'd like to keep onload="this.onload=null;this.rel='stylesheet'"
, or accept any alternatives that workweb.dev
Defer non-critical CSS | Articles | web.dev
Learn how to defer non-critical CSS with the goal of optimizing the Critical Rendering Path, and improving First Contentful Paint (FCP).
8 Replies
I'd also like to point out that the single liner works if we just ignore the typescript
Have you tried
and
Another alternative
Finally that article is from 2019.
Under CSP inline event handlers will be ignored and only scripts with a valid CSP hash or nonce will be executed.
MDN Web Docs
Content Security Policy (CSP) - HTTP | MDN
Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks,
including Cross-Site Scripting (XSS) and data injection attacks.
These attacks are used for everything from data theft, to site defacement, to malware distribution.
MDN Web Docs
nonce - HTML: HyperText Markup Language | MDN
The nonce global attribute
is a content attribute defining a cryptographic nonce ("number used once") which can be used by
Content Security Policy to determine whether or not a given fetch will
be allowed to proceed for a given element.
@peerreynders Thank you for the suggestions! I guess @ts-ignore seems to be the way to go eh?
You shouldn't be using the string "event handler" (inline event-handling HTML attibute) in the first place (which is what TypeScript is complaining about).
Chrome is notorious for all of a sudden cracking down on these type of things.
So if updating the element in
entry-client.tsx
is too late, injecting a whitelisted inline script is the way to go.You shouldn't be using the string "event handler" (inline event-handling HTML attibute) in the first place (which is what TypeScript is complaining about).I get that part but is it really such a big deal tho?
https://stackoverflow.com/search?tab=newest&q=inline%20event%20handler%20CSP&searchOn=3
More often than not they cause avoidable grief like all of a sudden your site only loading its critical CSS.
And I don't see the console.log anywhere.That's because the JSX uses
addEventListener
. That onload
is run at element creation; so by the time addEventListener
is run it's too late.
The tactic is gaming the preload
mechanic; delaying the switch to the time that DOM element is created.
If you pick your critical CSS to suitably style the above the fold content of the page the link.rel = 'stylesheet'
in entry-client.tsx
should be fine.Stack Overflow
Human verification
Stack Overflow | The World’s Largest Online Community for Developers
gotcha. Thank you for the detailed explanation