Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cjholowatyj's avatar

Mitigating use of document object in dependency within InertiaJS SSR project

I'm currently running into an issue where I cannot seem to safely wrap calls to a dependency component that calls on the document object in my InertiaJS SSR project. (Using Laravel/Vue/Vite/Typescript with InertiaJS SSR)

Vite compiles successfully, and I can run the project without SSR running, but as soon as I call php artisan inertia:ssr-start it triggers the ReferenceError: document is not defined error shortly after.

What is the best way (if any) to still use the dependency (in this case filepond) in my SSR app and prevent the ReferenceError: document is not defined error?

0 likes
1 reply
LaryAI's avatar
Level 58

When working with server-side rendering (SSR) in a JavaScript framework like Vue with InertiaJS, you may encounter issues with browser-specific globals like document or window because they are not available in the Node.js environment where SSR runs.

To mitigate the use of the document object in your SSR project, you can use a few strategies:

  1. Conditional Import: Import the dependency only on the client-side. This can be done using dynamic imports or by checking if the code is running in a browser environment.

  2. Lifecycle Hooks: Use Vue's lifecycle hooks to ensure that code that depends on the document object only runs on the client-side.

Here's how you can implement these strategies:

Conditional Import

You can conditionally import the filepond library only when the component is mounted on the client-side:

<template>
  <div v-if="isClient">
    <!-- Your FilePond component or related code here -->
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue';

export default defineComponent({
  setup() {
    const isClient = ref(false);

    onMounted(async () => {
      isClient.value = true;
      const { create } = await import('filepond');
      // Initialize FilePond or perform any DOM-related operations here
    });

    return {
      isClient,
    };
  },
});
</script>

Use Lifecycle Hooks

Ensure that any code that interacts with the document object is placed inside the onMounted lifecycle hook, which only runs on the client-side:

<script lang="ts">
import { defineComponent, onMounted } from 'vue';

export default defineComponent({
  setup() {
    onMounted(() => {
      // This code will only run on the client-side
      const element = document.getElementById('some-element');
      if (element) {
        // Perform operations with the element
      }
    });
  },
});
</script>

Environment Check

You can also check if the code is running in a browser environment before accessing the document object:

if (typeof window !== 'undefined' && typeof document !== 'undefined') {
  // Safe to use document or window
}

By using these strategies, you can prevent the ReferenceError: document is not defined error in your InertiaJS SSR project while still utilizing client-side dependencies like filepond.

Please or to participate in this conversation.