Skip to content

'this' is undefined on the server side #176

Closed
@SoonDead

Description

@SoonDead

Upon upgrading to 2.0 the server side rendering broke:

To render numbers and dates in an i18n compatible way I'm using numeral.js and moment.js. Both of them are written in a way that uses this as the global object, like

(function () {
    // here, `this` means `window` in the browser, or `global` on the server
    // add `numeral` as a global object via a string identifier,
    // for Closure Compiler 'advanced' mode
    this['numeral'] = numeral;
}).call(this);

Or

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    global.moment = factory()
}(this, function () {
    var _moment;
    ......
    return _moment;
});

They are both valid in a browser or a node.js environment, but the following fails:

ReactConfig.cs:

ReactSiteConfiguration.Configuration
    .AddScript("~/Scripts/numeral.js")
    .AddScript("~/Scripts/my-component.js");

Index.cshtml:

@Html.React("MyComponent", new { prop1 = "prop" })

Since it cannot set the property 'numeral' of undefined.

This pretty much blocks me from upgrading to React.Core 2.0 and therefore React 0.14.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions