RobinHerbots / Inputmask

Input Mask plugin
https://robinherbots.github.io/Inputmask/
MIT License
6.4k stars 2.17k forks source link

"window is not defined" when using serverside rendering (Angular Universal) #2628

Open fabienne-m opened 2 years ago

fabienne-m commented 2 years ago

Summary

When using Inputmask with Angular Universal in ServerSideRendering mode, get a "ReferenceError: window is not defined" upon startup.

Setup

Using the ngneat/input-mask wrapper around Inputmask.

Relevant libraries and versions:

Deployed to AWS Elasticbeanstalk running Node.js 16.

Reproducible locally using the Angular Universal test SSR.

Expected

When using in a client side rendering mode, no errors are shown. It would be expected that the same would happen in a SSR environment (Node.js) however the server fails to start.

Actual

Server side code fails to start, throwing a ReferenceError: window is not defined. I suspect this is because of the outputted webpack code (see below).

Other

Relevant code from dist/inputmask.js:

/*!
 * dist/inputmask
 * https://github.com/RobinHerbots/Inputmask
 * Copyright (c) 2010 - 2020 Robin Herbots
 * Licensed under the MIT license
 * Version: 5.0.5-beta.0
 */
!function webpackUniversalModuleDefinition(root, factory) {
    if (true) module.exports = factory(); else { var i, a; }
}(window, function() {
  ^ 
ReferenceError: window is not defined
felipein commented 1 year ago

@fabienne-m any progress on this?

fabienne-m commented 1 year ago

Unfortunately, not that I know of.

felipein commented 1 year ago

my observation is that inputmask is being built and placed in the main.js bundle. Because its code referes self/window it doesnt pass over the :SSR compiler. Neither route i'm using inputmask needs to be served over SSR so i'm trying to find a way by pass this code check that stops when self/window happens. Something like configuring the routes that will be served as SSR on the server.ts