#Laravel Inertia SSR - document is not defined

1 messages · Page 1 of 1 (latest)

rare scroll
#

Hello Guys! I am getting this kinda errors when I run the inertia ssr node server. document is not defined.

I found that the reason of those errors is because node server can use document or windows global objects.

Now my question is there any solutions to prevent use those third library/package when I run the SSR node server? because I have some package installed and I don't think is a good solution to uninstall all.

I hope you can help me with this issue.

hot ruin
#

Try this, on your vite.config.js you can exclude these packages for SSR build

export default defineConfig(({ mode }) => {
    return {
        ...
        ssr: {
            noExternal: ['package-name'],
        },
    };
});
ocean mirage
#

Now my question is I there any solutions to prevent use those third library/package when I run the SSR node server? because I have some package installed and I don't think is a good solution to uninstall all.
Yeah, you'd need to use SSR-compatible libraries for it to properly work. You could also use dynamic imports for stuff that should only be loaded on the client. So you'd use a mounted hook, since those wouldn't be executed during SSR, for example in Vue you could do this:

onMounted(() => {
  const Quill = await import('quill')
})
rare scroll
#

I found one solution for most of my errors. I have code splitting enabled in my app.jsx file: const pages = import.meta.glob('./Modules/**/*.jsx', {eager: true});. If I disable it in my ssr.jsx file, it solves the external library errors. Now, I need to change some code where I used document. and window.

I will try out what you told me and keep you updated.

btw is there any way to block endpoints for this ssr server? for example I only want ssr on my public site not my admin panel.

Thanks