-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onLoad prop removed from html.js - works in other files #30686
Comments
For example when I use helm I can push a link element with an unload attribute to the header with no problem |
I think this is the same as: vercel/next.js#12984 Especially relevant:
Also, as laid out in https://www.gatsbyjs.com/docs/custom-html/
So I'd recommend using |
Ahh thanks @LekoArts I'll try out the plugin - that looks promising. I'm very new to Gatsby so I didn't knew about |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Hey again! It’s been 60 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it. Thanks again for being part of the Gatsby community! 💪💜 |
I tried this way, but then also "onload" is removed.
|
A few days ago I was facing this issue. So, what if you really need to define the onload style attribute on html.js? You can do it this way:
exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => { `
getHeadComponents().forEach(el =>
{
if (el.type === 'style' && el.props['data-href'])
{
el.type = 'link'
el.props['href'] = el.props['data-href']
el.props['rel'] = 'stylesheet'
el.props['type'] = 'text/css'
el.props['media'] = "print"
el.props['_force_onload_html_attribute'] = "_force_onload_html_attribute_value"
delete el.props['data-href']
delete el.props['dangerouslySetInnerHTML']
}
})
}
//auxiliary function to find HTML files recursively.
const walk = (dir, filterExtension) => {
try {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(file => {
file = path.join(dir, file);
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
// Recurse into subdir
results = [...results, ...walk(file, filterExtension)];
} else {
// Is an Html file.
if (file.endsWith(filterExtension)) {
results.push(file);
}
}
});
return results;
} catch (error) {
console.error(`Error...`, error);
}
};
// this functions will replace the two temporal html attribute and value to the actual value.
const edit = filePath => {
const oldContent = fs.readFileSync(filePath, {encoding: 'utf8'});
const regex = /_force_onload_html_attribute/;
const replaceVal = 'onload';
const newContent = oldContent.replace(regex, replaceVal);
const regex2 = /_force_onload_html_attribute_value/;
const replaceVal2 = "this.media='all'; this.onload = null"
const newContent2 = newContent.replace(regex2, replaceVal2);
fs.writeFileSync(filePath, newContent2, {encoding: 'utf-8'});
console.log(`Edited file: ${filePath}`);
};
// onPostBuild API
exports.onPostBuild = async ({_,reporter}, pluginOptions) =>
{
const public_dir = `${__dirname}/public`
const filePaths = walk(public_dir, '.html');
filePaths.forEach(filePath => edit(filePath));
} Then, the final result in the HTML is something like: <link data-identity="gatsby-global-css" href="/styles.830f820405b440b0141f.css" rel="stylesheet" type="text/css" media="print" onload="this.media='all'; this.onload = null"/> |
Description
When I use the
onLoad
prop in my centralhtml.js
file it is being removed, while in other files it works as expected.Steps to reproduce
Add a html.js file and add an element with onLoad attribute - for example a
<link onLoad="stuff">
Primajin/gatsby-bug-onload@9658ec9
Expected result
The element is rendered with the onLoad attribute.
Actual result
The element is rendered without the onLoad attribute.
Environment
The text was updated successfully, but these errors were encountered: