ReferenceError
An error occurred.
document is not defined
Browser APIs are not available on the server.
If the code is in a framework component, try to access these objects after rendering using lifecycle methods or use a client:only directive to make the component exclusively run on the client.
The error you're encountering stems from trying to access the document object during the server-side rendering (SSR) phase of your Astro component. In Node.js (the server environment), the document and window objects are not available because they are part of the browser's API, which runs on the client side.
In your script, you're directly accessing document at the top level, which means this code runs during SSR:
javascript
const loginWrapper = document.querySelector(".login-wrapper") as HTMLDivElement;
const loginContent = document.querySelector("#login-content") as HTMLDivElement;
const accountButtonText = document.querySelector('#account-button-text') as HTMLSpanElement;
const accountButton = document.querySelector('.account-button') as HTMLButtonElement;
To resolve this issue, you need to ensure that any code that accesses browser-specific APIs runs only on the client side. Here's how you can fix it:
Use the client:load Directive: This tells Astro to hydrate the component on the client side, and the script will run only after the component is loaded in the browser.
Move document Access Inside Lifecycle Hooks or Functions: Ensure that any access to document happens inside event handlers or functions that run on the client side.
Here's how you can apply these solutions to your code:
Updated Code:
astro
---
import { Input, Button } from '../';
const { class: additionalClasses } = Astro.props;
---
<!-- Add the client:load directive to the script tag -->
<script client:load>
import { actions, isInputError } from "astro:actions";
// Move all document access inside the initialize function
function initialize() {
const loginWrapper = document.querySelector(".login-wrapper") as HTMLDivElement;
const loginContent = document.querySelector("#login-content") as HTMLDivElement;
const accountButtonText = document.querySelector('#account-button-text') as HTMLSpanElement;
const accountButton = document.querySelector('.account-button') as HTMLButtonElement;
let currentUser = null;
// Rest of your functions here, making sure to reference the elements correctly
// Initial setup
function initializeLoginState() {
// Your existing code...
}
// Call the initialization function when the script loads
initializeLoginState();
// Event listeners and other code interacting with the DOM
}
// Call initialize after the component has loaded
initialize();
</script>
<div class="login-wrapper">
<div id="login-content">
<form class="login-form" id="sign-in">
<div class="form-fields">
<Input name="email" type="email" placeholder="Email/username..." />
<Input name="password" type="password" placeholder="Password..." />
</div>
<Button type="submit" id="submit-button">
<span id="button-text">Log in to Your Account</span>
<span id="loading-icon" class="hidden">
<svg class="animate-spin h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<!-- SVG Path -->
</svg>
</span>
</Button>
</form>
</div>
</div>
<style>
/* Your existing styles */
</style>
Explanation:
client:load Directive: By adding client:load to the <script> tag, you ensure that the script runs only on the client side after the component has been loaded. This prevents any server-side execution of code that requires the document object.
astro
<script client:load>
// Your JavaScript code
</script>
Move document Access Inside Functions: Instead of accessing document at the top level of your script (which would still throw errors because the script module itself is evaluated on the server side), you should place all the document references inside functions that run on the client side.
javascript
function initialize() {
const loginWrapper = document.querySelector(".login-wrapper") as HTMLDivElement;
// Rest of your code...
}
// Call initialize after the component has loaded
initialize();
Reorganize Your Script: Ensure that any DOM manipulation or event listeners are set up within functions that are called on the client side. This way, the code that depends on the browser environment isn't executed during SSR.
Additional Tips:
Avoid Top-Level document Access: Even with the client:load directive, accessing document at the top level can cause issues because the module itself is imported during SSR. Always wrap document access within client-side functions.
Use onMount in Framework Components: If you're using a framework like Svelte or SolidJS within Astro, you can use lifecycle methods like onMount to run code after the component has been mounted in the DOM.
Check for null Elements: When querying elements with document.querySelector, always check if the element exists to avoid null reference errors.
javascript
const loginWrapper = document.querySelector(".login-wrapper") as HTMLDivElement;
if (!loginWrapper) return;