From 2c61395ed7f943e9bdaf554e6f452fff8d23b593 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 10 Jan 2015 16:25:22 -0800 Subject: [PATCH] Add X-Request-URL --- README.md | 17 +++++++++++++++++ fetch.js | 2 +- test/server.js | 7 +++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5d7835e..e1b6f25 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,23 @@ fetch('/users') }) ``` +### Response URL caveat + +The `Response` object has a URL attribute for the final responded resource. +Usually this is the same as the `Request` url, but in the case of a redirect, +its all transparent. Newer versions of XHR include a `responseURL` attribute +that returns this value. But not every browser supports this. The compromise +requires setting a special server side header to tell the browser what URL it +just requested (yeah, I know browsers). + +``` ruby +response.headers['X-Request-URL'] = request.url +``` + +If you want `response.url` to be reliable, you'll want to set this header. The +day that you ditch this polyfill and use native fetch only, you can remove the +header hack. + ## Browser Support ![Chrome](https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png) diff --git a/fetch.js b/fetch.js index 65065ea..f285a23 100644 --- a/fetch.js +++ b/fetch.js @@ -181,7 +181,7 @@ status: status, statusText: xhr.statusText, headers: headers(xhr), - url: xhr.responseURL + url: xhr.responseURL || xhr.getResponseHeader('X-Request-URL') } resolve(new Response(xhr.responseText, options)) } diff --git a/test/server.js b/test/server.js index a13af04..cf691c1 100755 --- a/test/server.js +++ b/test/server.js @@ -20,8 +20,11 @@ var routes = { })); }) }, - '/hello': function(res) { - res.writeHead(200, {'Content-Type': 'text/plain'}); + '/hello': function(res, req) { + res.writeHead(200, { + 'Content-Type': 'text/plain', + 'X-Request-URL': 'http://' + req.headers.host + req.url + }); res.end('hi'); }, '/redirect/301': function(res) { -- GitLab