fix unordered include_files result
Now only concats adjacent internal to the application stuff, this also Fixes #1673
This commit is contained in:
+60
-31
@@ -473,45 +473,67 @@ class Response(Storage):
|
|||||||
response.cache_includes = (cache_method, time_expire).
|
response.cache_includes = (cache_method, time_expire).
|
||||||
Example: (cache.disk, 60) # caches to disk for 1 minute.
|
Example: (cache.disk, 60) # caches to disk for 1 minute.
|
||||||
"""
|
"""
|
||||||
|
app = current.request.application
|
||||||
|
|
||||||
|
# We start by building a files list in which adjacent files internal to
|
||||||
|
# the application are placed in a list inside the files list.
|
||||||
|
#
|
||||||
|
# We will only minify and concat adjacent internal files as there's
|
||||||
|
# no way to know if changing the order with which the files are apppended
|
||||||
|
# will break things since the order matters in both CSS and JS and
|
||||||
|
# internal files may be interleaved with external ones.
|
||||||
files = []
|
files = []
|
||||||
ext_files = []
|
# For the adjacent list we're going to use storage List to both distinguish
|
||||||
has_js = has_css = False
|
# from the regular list and so we can add attributes
|
||||||
|
internal = List()
|
||||||
|
internal.has_js = False
|
||||||
|
internal.has_css = False
|
||||||
|
done = set() # to remove duplicates
|
||||||
for item in self.files:
|
for item in self.files:
|
||||||
if isinstance(item, (list, tuple)):
|
if not isinstance(item, list):
|
||||||
ext_files.append(item)
|
if item in done:
|
||||||
|
continue
|
||||||
|
done.add(item)
|
||||||
|
if isinstance(item, (list, tuple)) or not item.startswith('/' + app): # also consider items in other web2py applications to be external
|
||||||
|
if internal:
|
||||||
|
files.append(internal)
|
||||||
|
internal = List()
|
||||||
|
internal.has_js = False
|
||||||
|
internal.has_css = False
|
||||||
|
files.append(item)
|
||||||
continue
|
continue
|
||||||
if extensions and not item.rpartition('.')[2] in extensions:
|
if extensions and not item.rpartition('.')[2] in extensions:
|
||||||
continue
|
continue
|
||||||
if item in files:
|
internal.append(item)
|
||||||
continue
|
|
||||||
if item.endswith('.js'):
|
if item.endswith('.js'):
|
||||||
has_js = True
|
internal.has_js = True
|
||||||
if item.endswith('.css'):
|
if item.endswith('.css'):
|
||||||
has_css = True
|
internal.has_css = True
|
||||||
files.append(item)
|
if internal:
|
||||||
|
files.append(internal)
|
||||||
|
|
||||||
if have_minify and ((self.optimize_css and has_css) or (self.optimize_js and has_js)):
|
# We're done we can now minify
|
||||||
# cache for 5 minutes by default
|
if have_minify:
|
||||||
key = hashlib_md5(repr(files)).hexdigest()
|
for i, f in enumerate(files):
|
||||||
cache = self.cache_includes or (current.cache.ram, 60 * 5)
|
if isinstance(f, List) and ((self.optimize_css and f.has_css) or (self.optimize_js and f.has_js)):
|
||||||
|
# cache for 5 minutes by default
|
||||||
def call_minify(files=files):
|
key = hashlib_md5(repr(f)).hexdigest()
|
||||||
return minify.minify(files,
|
cache = self.cache_includes or (current.cache.ram, 60 * 5)
|
||||||
URL('static', 'temp'),
|
def call_minify(files=f):
|
||||||
current.request.folder,
|
return List(minify.minify(files,
|
||||||
self.optimize_css,
|
URL('static', 'temp'),
|
||||||
self.optimize_js)
|
current.request.folder,
|
||||||
if cache:
|
self.optimize_css,
|
||||||
cache_model, time_expire = cache
|
self.optimize_js))
|
||||||
files = cache_model('response.files.minified/' + key,
|
if cache:
|
||||||
call_minify,
|
cache_model, time_expire = cache
|
||||||
time_expire)
|
files[i] = cache_model('response.files.minified/' + key,
|
||||||
else:
|
call_minify,
|
||||||
files = call_minify()
|
time_expire)
|
||||||
|
else:
|
||||||
files.extend(ext_files)
|
files[i] = call_minify()
|
||||||
s = []
|
|
||||||
for item in files:
|
def static_map(s, item):
|
||||||
if isinstance(item, str):
|
if isinstance(item, str):
|
||||||
f = item.lower().split('?')[0]
|
f = item.lower().split('?')[0]
|
||||||
ext = f.rpartition('.')[2]
|
ext = f.rpartition('.')[2]
|
||||||
@@ -531,6 +553,13 @@ class Response(Storage):
|
|||||||
if tmpl:
|
if tmpl:
|
||||||
s.append(tmpl % item[1])
|
s.append(tmpl % item[1])
|
||||||
|
|
||||||
|
s = []
|
||||||
|
for item in files:
|
||||||
|
if isinstance(item, List):
|
||||||
|
for f in item:
|
||||||
|
static_map(s, f)
|
||||||
|
else:
|
||||||
|
static_map(s, item)
|
||||||
self.write(''.join(s), escape=False)
|
self.write(''.join(s), escape=False)
|
||||||
|
|
||||||
def stream(self,
|
def stream(self,
|
||||||
|
|||||||
Reference in New Issue
Block a user