question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSR window is undefined from router.ts

See original GitHub issue

I’m trying to use SSR but running into some issues.

  • vue: 2.16.14
  • inertia: 0.11.0
  • inertia-vue: 0.8.0

After compiling JS for SSR I get the following error from node:

ReferenceError: window is not defined at e.n.isBackForwardVisit

Which seems to be related to the following: https://github.com/inertiajs/inertia/blob/8597d5ee0243553a6373e9793acfe87739824750/packages/inertia/src/router.ts#L101

webpack.ssr.mix.js:

mix.js( 'resources/js/ssr.js', 'public/js' )
    .vue({
        version: 2,
        options: { optimizeSSR: true },
        useVueStyleLoader: true, // Set to true or it will use style-loader which doesn't support SSR
    });

mix.disableNotifications();

mix.webpackConfig( {
        output: {
            chunkFilename: 'js/ssr_[name].js?id=[chunkhash]',
            globalObject: 'this'
        },

        target: 'node',
        externalsPresets: { node: true }, // Needed for Webpack 5 https://www.npmjs.com/package/webpack-node-externals
        externals: [webpackNodeExternals({
            modulesFromFile: {
                fileName: 'package.json',
                includeInBundle: 'dependencies',
                excludeFromBundle: 'devDependencies',
            }
        })],

dependencies from package.json:

"dependencies": {
        "@inertiajs/inertia": "^0.11.0",
        "@inertiajs/inertia-vue": "^0.8.0",
        "@inertiajs/server": "^0.1.0",
        "vue": "^2.6.12",

ssr.js:

import Vue from 'vue';
import vuetify from './vuetify';
import { createRenderer } from 'vue-server-renderer';
import { createInertiaApp } from '@inertiajs/inertia-vue';
import { ZiggyVue } from 'ziggy';
import { Ziggy } from './ziggy';
import createServer from '@inertiajs/server';

 Vue.use(ZiggyVue);

createServer(page => createInertiaApp({
    page,
    render: createRenderer().renderToString,
    title: title => title ? `${title} | Company` : 'Laden... | Company',
    resolve: name => require(`@/pages/${name}`),
    setup({ App, props, plugin }) {
        Vue.use(plugin);

        return new Vue({
            vuetify,
            render: h => h(App, props),
        });
    },
}));

If I don’t include the Inertia packages and Vue it will give an error like vue not defined or @inertia/* not defined. But using @inertia/inertia will return the mentioned error.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
claudiodekkercommented, Jan 12, 2022

Hey @ajnsn,

In Vue 2, the check is different than in Vue 3… this.$isServer is set by Vue, so that’s correct: https://vuejs.org/v2/api/#vm-isServer In addition we’re extensively running (and have been extensively running) Inertia with SSR on Vue 2 for months now, so my suspicion is that you’re manually initializing Inertia somehow, somehow, which shouldn’t happen.

Would it be possible for you to provide us with either access to the project where this occurs + steps on how to reproduce this, or with a separate reproduceable (smaller) repository?

Thanks!

0reactions
claudiodekkercommented, Jan 17, 2022

That most likely means you haven’t installed Vue then.

When running a simple npm/yarn install from your reproducable repository, I was able to reproduce the bug you mentioned in the issue description, but never ran into this problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve "window is not defined" errors in React and Next.js
1. First solution: typeof · 2. Second solution: the useEffect hook · 3. Third solution: dynamic loading.
Read more >
Window is not defined in Next.js React app - Stack Overflow
I am using it to dynamically import libaries that are not compatible with SSR in next.js, since its own dynamic import is only...
Read more >
Error with classic Vue Router: ReferenceError: window is not ...
I am a big fan of vue-ssg and vitesse, but unfortunately I can not get vue-ssg running just with vue 3 and vue-router...
Read more >
How to solve Next.js window is not defined
Solve a the common ReferenceError: next.js window is not defined error that you may see when doing SSR & pre-rendering.
Read more >
Migrating from Create React App - Next.js
Shallow Routing. API Routes ... With Create React App, you're likely using React Router. ... 'undefined') { // You now have access to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found