Skip to content

RescriptReactRouter crash on master (window vs. $$window) #6905

@cknitt

Description

@cknitt

On current master, RescriptReactRouter.useUrl() crashes.

The actual crash is in the first line of RescriptReactRouter.hash():

function hash() {
  let window = typeof window === "undefined" ? undefined : window;
  if (window === undefined) {
    return "";
  }
  let raw = window.location.hash;
  switch (raw) {
    case "" :
    case "#" :
        return "";
    default:
      return Js_string.sliceToEnd(1, raw);
  }
}

The error I get in the browser is

ReferenceError: Cannot access 'window2' before initialization

On the ReScript side we have

  switch %external(window) {
    ...

AFAIK %external is used to guard the access of window with typeof so that the code won't crash if run on a platform that doesn't have window.

Before #6831 the first line of the JS output was

  var $$window = typeof window === "undefined" ? undefined : window;

so the problem did not occur.

/cc @cometkim @cristianoc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions