Changeset 6570


Ignore:
Timestamp:
Oct 22, 2014, 7:55:46 AM (8 years ago)
Author:
Nicklas Nordborg
Message:

References #1867: Prevent browsers from caching scripts and style sheets between releases

Now also checks if an absolute path is pointing to a file from an extensions and uses the version from the extension if so.

Added the same functionality to taglibs used by extensions to add extra scripts and style sheets.

Location:
branches/3.3-stable/src/clients/web/net/sf/basedb/clients/web/taglib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.3-stable/src/clients/web/net/sf/basedb/clients/web/taglib/Head.java

    r6562 r6570  
    103103  private static final Map<String, String> xtVersions = new HashMap<String, String>();
    104104
    105   // Match files under /extensions path. group 2 is the JAR file name
     105  // Match files under /base-root/extensions path. group 1 is the JAR file name
    106106  private static final Pattern PATH_MATCH =
    107       Pattern.compile("(" + ExtensionsControl.RESOURCES_URL + "/([^/]+)/(.*))");
    108 
    109   /**
    110     Register the version for an exteniosn JAR file
     107      Pattern.compile("/?[^/]*" + ExtensionsControl.RESOURCES_URL + "/([^/]+)/(.*)");
     108
     109  /**
     110    Register the version for an exteniosn JAR file.
     111    Used for appending a query parameter to all scripts and style sheets
     112    to prevent that browsers cache the old version.
     113    @see #getVersionParameter(String)
    111114    @since 3.3.2
    112115  */
     
    119122  }
    120123
     124  /**
     125    Get query parameter to the given absolute path (including root directory).
     126    If the path points to a file in the /base-root/extensions/ directory the version
     127    of the extension is used, otherwise the BASE version is used. The
     128    returned parameter is of the form v=<version>.
     129    @param absolutePath An absolute path with or without the base-root directory
     130    @since 3.3.2
     131  */
     132  public static final String getVersionParameter(String absolutePath)
     133  {
     134    Matcher m = PATH_MATCH.matcher(absolutePath);
     135    String version = globalVersion;
     136    if (m.matches())
     137    {
     138      String jarFile = m.group(1);
     139      if (xtVersions.containsKey(jarFile))
     140      {
     141        version = xtVersions.get(jarFile);
     142      }
     143    }
     144    return version;
     145  }
     146
     147 
    121148  /**
    122149    The parent &lt;base:page&gt; tag.
     
    179206      {
    180207        // Full absolute path
    181         sb.append(style).append(pp).append(globalVersion);
     208        sb.append(style).append(pp).append(getVersionParameter(style));
    182209      }
    183210      else if (style.startsWith("/"))
    184211      {
    185         // Absolute path is relative the page root (eg. /foo/bar.css --> /base/foo/bar.css)
    186         sb.append(appRoot).append(style.substring(1)).append(pp).append(globalVersion);
     212        sb.append(appRoot).append(style.substring(1)).append(pp).append(getVersionParameter(style));
    187213      }
    188214      else if (style.startsWith("~"))
     
    229255      {
    230256        // Full absolute path
    231         sb.append(script).append(pp).append(globalVersion);
     257        sb.append(script).append(pp).append(getVersionParameter(script));
    232258      }
    233259      else if (script.startsWith("/"))
    234260      {
    235261        // Absolute path that is relative the BASE root (eg. /foo/bar.js --> /base/foo/bar.js)
    236         sb.append(appRoot).append(script.substring(1)).append(pp).append(globalVersion);
     262        sb.append(appRoot).append(script.substring(1)).append(pp).append(getVersionParameter(script));
    237263      }
    238264      else if (script.startsWith("~"))
     
    254280  {
    255281    super.setPageContext(pageContext);
    256 
     282    HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
    257283    // If we are on /extension subpage, the localVersion is the
    258284    // version for the extension, otherwise we use the global version
    259     localVersion = globalVersion;
    260     HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
    261     Matcher m = PATH_MATCH.matcher(req.getServletPath());
    262     if (m.matches())
    263     {
    264       String jarFile = m.group(2);
    265       if (xtVersions.containsKey(jarFile))
    266       {
    267         localVersion = xtVersions.get(jarFile);
    268       }
    269     }
     285    localVersion = getVersionParameter(req.getServletPath());
    270286  }
    271287 
  • branches/3.3-stable/src/clients/web/net/sf/basedb/clients/web/taglib/extensions/Scripts.java

    r6134 r6570  
    3434
    3535import net.sf.basedb.clients.web.extensions.JspContext;
     36import net.sf.basedb.clients.web.taglib.Head;
    3637
    3738/**
     
    9293    for (String script : scripts)
    9394    {
     95      char pp = script.indexOf('?') == -1 ? '?' : '&';
    9496      sb.append("<script type=\"text/javascript\" src=\"");
    95       sb.append(script);
     97      sb.append(script).append(pp).append(Head.getVersionParameter(script));
    9698      sb.append("\" charset=\"UTF-8\"></script>\n");
    9799    }
  • branches/3.3-stable/src/clients/web/net/sf/basedb/clients/web/taglib/extensions/Stylesheets.java

    r5384 r6570  
    3434
    3535import net.sf.basedb.clients.web.extensions.JspContext;
     36import net.sf.basedb.clients.web.taglib.Head;
    3637
    3738/**
     
    9394    for (String stylesheet : stylesheets)
    9495    {
     96      char pp = stylesheet.indexOf('?') == -1 ? '?' : '&';
    9597      sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
    96       sb.append(stylesheet);
     98      sb.append(stylesheet).append(pp).append(Head.getVersionParameter(stylesheet));
    9799      sb.append("\">\n");
    98100    }
Note: See TracChangeset for help on using the changeset viewer.