MIME type issues with functions?

Refused to execute script from 'https://example.com/myfunction.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. CODE:
<!DOCTYPE html>
<html>
<head>
<script src="./myfunction.js"</script>
</head>
<body>
<button onclick="bodyTest()">TEST</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="./myfunction.js"</script>
</head>
<body>
<button onclick="bodyTest()">TEST</button>
</body>
</html>
function bodyTest(){
return("hello, world!");
}
function bodyTest(){
return("hello, world!");
}
I am not exactly sure what I am doing wrong..
8 Replies
Walshy
Walshy•17mo ago
Are you using Pages Functions? or is this a normal js file you want to execute? If the former, you don't call it in your HTML, it's just invoked when you go to the path
Catt0s
Catt0sOP•17mo ago
Pages function, since the goal is for it to interact with D1 I understand what you mean now, but this means that I need to have a function to get my HTML and return a modified version of that HTML as a response then, right?
Walshy
Walshy•17mo ago
Sure if you want to rewrite the HTML, you can checkout HTMLRewriter - https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/ You can also use it as an API and return like a JSON which you handle client-side.
Catt0s
Catt0sOP•17mo ago
oh cool, thanks this appears to be exactly what i need but if it is rewriting, then how does it get the initial html?
Walshy
Walshy•17mo ago
fetch(req) will fetch from behind the Worker (so an origin)
Catt0s
Catt0sOP•17mo ago
ah ok
Walshy
Walshy•17mo ago
HTMLRewriter docs show that too 🙂
Catt0s
Catt0sOP•17mo ago
ah, i see it now, transform() is what i was looking for
Want results from more Discord servers?
Add your server