Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window is not defined for preact pre-render #485

Closed
GarliqBread opened this issue Aug 31, 2020 · 2 comments
Closed

Window is not defined for preact pre-render #485

GarliqBread opened this issue Aug 31, 2020 · 2 comments

Comments

@GarliqBread
Copy link

Preact pre-render throws an error because window doesn't exist on the render environment :
`
/node_modules/form-data/lib/browser.js

ReferenceError: window is not defined`

`
This is most likely caused by using DOM or Web APIs.
Pre-render runs in node and has no access to globals available in browsers.

Consider wrapping code producing error in: 'if (typeof window !== "undefined") { ... }'
`
I could use the --no-prerender tag on the build command but I really didn't want to use it, is there a work-a-round for this? or should I create a pull-request to add "if (typeof window !== "undefined")..." ?

aemaem added a commit to leftshiftone/gaia-sdk that referenced this issue Oct 5, 2020
oliverwoe added a commit to leftshiftone/gaia-sdk that referenced this issue Oct 5, 2020
…orm-data/form-data#485) (#54)

Co-authored-by: aemaem <michael.mair@leftshift.one>
(cherry picked from commit 8ed0326)
@enkelmedia
Copy link

enkelmedia commented Jan 26, 2021

Getting the same error on version 3.0.0 when performing SSR.

Updating browser.js to this solves the problem but it would be good is this was shipped.

/* eslint-env browser */
module.exports = typeof self == 'object' ? self.FormData : typeof window !='undefined' ? window.FormData : undefined;

Since the SSR does not have a window-object we need to check that it exists, no requests are performed on the server but the rendering will complain about this use of the window-variable.

PR Created: #496

enkelmedia added a commit to Obviuse/form-data that referenced this issue Jan 26, 2021
This updates that browser.js does not throw when rendering on the server and window is not defined.

Solves issue form-data#485
@chrisjingram
Copy link

For me, this was caused by the self object not being present globally in the server rendering context.

browser.js in form-data is already set up to check for self in the global context – presence of self means we are in SSR. In that case it provides self.FormData instead of window.FormData. This is where window is undefined comes from when self is undefined.

I fixed this by defining var self = self || this in the global SSR context.

I'm using react_on_rails for server rendering so for me the fix was to monkey patch ReactOnRails::ServerRenderingPool::RubyEmbeddedJavaScript like so:

ReactOnRails::ServerRenderingPool::RubyEmbeddedJavaScript.class_eval do
  def self.console_polyfill
    <<~JS
      var debugConsole = console;
      var console = { history: [] };
      ['error', 'log', 'info', 'warn'].forEach(function (level) {
        console[level] = function () {
          var argArray = Array.prototype.slice.call(arguments);
          if (argArray.length > 0) {
            argArray[0] = '[SERVER] ' + argArray[0];
          }
          console.history.push({level: level, arguments: argArray});
        };
      });
      // patched self and global definitions ------------------
      var global = global || this
      var self = self || this
      // ----------------------------
    JS
  end
end

This won't be relevant for everyone but hopefully provides a clue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants