Skip to content

Use Fetch API in browsers that support it - #7371

Merged
kkaefer merged 3 commits into
masterfrom
fetch
Oct 9, 2018
Merged

kkaefer merged 3 commits into
masterfrom
fetch

Conversation

@kkaefer

@kkaefer kkaefer commented Oct 5, 2018

Copy link
Copy Markdown
Member

We're currently using the XmlHttpRequest to retrieve data like tiles from the backend server.

This PR switches to the Fetch API where it is supported to enable sending correct referrers from the worker bundle. XHR requests don't send a correct referrer for code from a blob URL worker bundle in Safari, Chrome and Edge (and the Referer header is disallowed in this API), while the Fetch API allows us to correctly set the page's referrer URL, as demonstrated by this small snippet:

<script>
let worker = new Worker(window.URL.createObjectURL(new Blob([`
const referrer = "${document.location.href}";
const url = 'https://example.com'

const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();

fetch(new Request(url, { referrer }))
`], { type: 'text/javascript' })));
</script>

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • document any changes to public APIs
  • post benchmark scores
  • manually test the debug page

@kkaefer
kkaefer requested review from mollymerp and mourner October 5, 2018 16:42
@kkaefer
kkaefer force-pushed the fetch branch 3 times, most recently from 14c65f9 to 729ea91 Compare October 5, 2018 17:06
Comment thread src/style/style.js

this._resetUpdates();

this.dispatcher.broadcast('setReferrer', getReferrer());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are dispatching the referrer to the workers when we set up the Style object. On single page applications, the pathname may change, but we'll continue to use the initial pathname. Should we rebroadcast a new pathname?

t.end();
});
window.server.respond();
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the special casing for empty array buffers. There's nothing inherently wrong with empty 200 responses. The reason we originally added this special case is for images that had an empty response, but that is already handled directly in the getImage call.

config.ACCESS_TOKEN = null;

event.postTurnstileEvent([' a.tiles.mapxbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're passing full tile URLs from the TileJSON file here, not just host names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants