Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

JX.Resource: normalize paths #38

Closed
wants to merge 1 commit into from

2 participants

@jwatzman

Summary:
Be consistent between browser munging on bootstrap and munging when
given a path via JX.Resource.load. See comment in code and T2234 for details.

Test Plan:
Load a page with CSS and JS resources specified with URIs that did
not include hostnames. See that JX.Resource._loaded does include hostnames.
Re-load one of those resources via JX.Resource.load(path-without-hostname,
JX.bag)
. See that the resource is no longer re-loaded; its name is properly
normalized. Load a resource that is not already on the page, see it get loaded.
See normalization work in both Firefox and Chrome.

Reviewers: epriestley, chad, cpojer

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T2234

Differential Revision: https://secure.phabricator.com/D4279

@jwatzman jwatzman JX.Resource: normalize paths
Summary:
Be consistent between browser munging on bootstrap and munging when
given a path via JX.Resource.load. See comment in code and T2234 for details.

Test Plan:
Load a page with CSS and JS resources specified with URIs that did
not include hostnames. See that `JX.Resource._loaded` does include hostnames.
Re-load one of those resources via `JX.Resource.load(path-without-hostname,
JX.bag)`. See that the resource is no longer re-loaded; its name is properly
normalized. Load a resource that is not already on the page, see it get loaded.
See normalization work in both Firefox and Chrome.

Reviewers: epriestley, chad, cpojer

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T2234

Differential Revision: https://secure.phabricator.com/D4279
4e8394c
@epriestley
Owner

Cherry-picked as 07c9b3f. Thanks!

@epriestley epriestley closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 23, 2012
  1. @jwatzman

    JX.Resource: normalize paths

    jwatzman authored
    Summary:
    Be consistent between browser munging on bootstrap and munging when
    given a path via JX.Resource.load. See comment in code and T2234 for details.
    
    Test Plan:
    Load a page with CSS and JS resources specified with URIs that did
    not include hostnames. See that `JX.Resource._loaded` does include hostnames.
    Re-load one of those resources via `JX.Resource.load(path-without-hostname,
    JX.bag)`. See that the resource is no longer re-loaded; its name is properly
    normalized. Load a resource that is not already on the page, see it get loaded.
    See normalization work in both Firefox and Chrome.
    
    Reviewers: epriestley, chad, cpojer
    
    Reviewed By: epriestley
    
    CC: aran, Korvin
    
    Maniphest Tasks: T2234
    
    Differential Revision: https://secure.phabricator.com/D4279
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 2 deletions.
  1. +26 −2 src/lib/Resource.js
View
28 src/lib/Resource.js
@@ -26,7 +26,7 @@ JX.install('Resource', {
*/
load: function(list, callback) {
var resources = {},
- uri, resource, path, type;
+ uri, resource, path, is_css;
list = JX.$AX(list);
@@ -42,13 +42,37 @@ JX.install('Resource', {
uri = new JX.URI(list[ii]);
resource = uri.toString();
path = uri.getPath();
+
+ is_css = false;
+ if (path.indexOf('.css') == path.length - 4) {
+ is_css = true;
+ }
+
+ // During initialization, resource paths are pulled directly out of the
+ // elements in the <head>. The browser can munge these from what the
+ // literal URIs are in the HTML; in particular, it tends to include the
+ // domain. So that we can allow clients to specify URIs consistently,
+ // not worrying about this behavior, run the URI through the same
+ // browser munging path that it would have gone through had it come out
+ // of the <head> in the first place, so everything matches and we don't
+ // double-load resources.
+ if (is_css) {
+ var normalizing_elem = document.createElement('link');
+ normalizing_elem.href = resource;
+ resource = normalizing_elem.href;
+ } else {
+ var normalizing_elem = document.createElement('script');
+ normalizing_elem.src = resource;
+ resource = normalizing_elem.src;
+ }
+
resources[resource] = true;
if (JX.Resource._loaded[resource]) {
setTimeout(JX.bind(JX.Resource, JX.Resource._complete, resource), 0);
} else if (!JX.Resource._loading[resource]) {
JX.Resource._loading[resource] = true;
- if (path.indexOf('.css') == path.length - 4) {
+ if (is_css) {
JX.Resource._loadCSS(resource);
} else {
JX.Resource._loadJS(resource);
Something went wrong with that request. Please try again.