I encountered this error during the build: document is not defined
I understand that it has to do with what's explained on this page: https://docs.astro.build/en/guides/troubleshooting/#document-or-window-is-not-defined
However, I'm not sure how I can deal with it in my case. Here is a minimal reproducible example.
src/pages/example/index.astro
---
---
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example</title>
</head>
<body>
<h1>Test</h1>
<div id="output"></div>
<script src="./script.js"></script>
</body>
</html>
src/pages/example/script.js
document.addEventListener('DOMContentLoaded', () => {
console.log('DOM fully loaded and parsed');
const outputDiv = document.getElementById('output');
if (outputDiv) {
outputDiv.textContent = 'This text was added by JavaScript!';
}
alert('Window width: ' + window.innerWidth);
});