MithrilJS / mithril.js

A JavaScript Framework for Building Brilliant Applications
https://mithril.js.org
MIT License
14.02k stars 925 forks source link

Mithril 1.0.1 request is no longer loading file:/// URLs #1624

Closed simov closed 7 years ago

simov commented 7 years ago

I'm using Mithril Rewrite in one of my apps, and it seems that the last time I updated Mithril there was on 16th October 2016. I think it was right after the run method was changed to then.

Today I decided to migrate to Mithril 1.0.1 from cdnjs and now loading file:/// URLs using the request method is no longer working. There is no error whatsoever. Let me know if I have to provide a code example to reproduce the issue.

pygy commented 7 years ago

It is a problem with cross-origin requests. IIRC Chrome won't allow XHR to local files at all. Other browsers (Firefox, at least) are more tolerant for pages loaded from file:///. But you can't XHR to file:/// from a page loaded from HTTP, for security reasons. There may be options to override that behavior, but I've never looked into them.

You can set up a local server to work around that limitation.

simov commented 7 years ago

It's a Chrome extension that injects a javascript file into a page loaded on file:/// URL, then the script uses the m.request method to get that same page. I know it sounds weird but it was a perfectly valid solution for my problem and prior version 1.0.1 it was working.

I'll try to put together a simple extension that you can test with.

simov commented 7 years ago

I figured it will be much easier to show you what I'm seeing in a screencast.

As you can see with mithril-rewirte the deserialize function is being called and even though the final result is returned in the catch error handler, I can use that response body.

With Mithril 1.0.1 nothing happens.

manifest.json

{
  "manifest_version": 2,
  "name"            : "Mithril",
  "version"         : "1.0",
  "description"     : "",

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": [

        // Test with either one of the following:

        // Nov 17, 2016
        // https://raw.githubusercontent.com/lhorie/mithril.js/d02a238bf0d513887cf455cd60b3ae32d3d0ced7/mithril.min.js
        "mithril-rewrite.min.js",

        // https://cdnjs.cloudflare.com/ajax/libs/mithril/1.0.1/mithril.min.js
        // "mithril-1.0.1.min.js",

        "content.js"
      ],
      "run_at": "document_start"
    }
  ]
}

content.js

m.request({
  method: 'GET',
  url: 'file:///home/s/something',
  deserialize: (body) => {
    console.log(body)
    return body
  }
})
.then((body) => {
  console.log(body)
})
.catch((err) => {
  console.log(err)
})

You need to download both versions of Mithril locally and set up some test file on your local file system.

pygy commented 7 years ago

Ok, so it could be a regression... m.request is a bit of a blind spot for me, I haven't tracked its implementation and therefore I can't tell you what changed off the top of my head... I'll take a look tonight (unless someone beats me to it).

simov commented 7 years ago

Thanks @pygy in case you plan to test it using the Chrome extensions approach, once you have all of the extension files in a folder you'll have to 'Load unpacked extension' from chrome://extensions.

simov commented 7 years ago

@pygy the regression is caused by this line https://github.com/lhorie/mithril.js/blob/v1.0.1/request/request.js#L74-L76

xhr.status is 0 and xhr.readyState is 2 when loading the file:/// URL

tivac commented 7 years ago

Sounds like it's related to https://github.com/lhorie/mithril.js/pull/1612 then.

bruce-one commented 7 years ago

Hmm, is there a chance that xhr.readyState becomes 4 eventually? Because otherwise it would never have worked (would it? from what I can see there's always been a readyState === 4 check, even in 0.2.)

(The status 0 thing makes sense wrt #1612 (ie, it's probably the same issue))

simov commented 7 years ago

Closing this because the fix https://github.com/lhorie/mithril.js/pull/1612 was merged in master. What is the release schedule, if any?