[英]Automatically adding SourceMap headers for JavaScript requests
I've started playing around with SourceMaps and am wondering if there's a way to automatically set the appropriate header automatically if a .map
file exists for a JavaScript request using .htaccess instead of having to modify the original JavaScript file. 我开始玩SourceMaps,我想知道如果使用.htaccess存在JavaScript请求的
.map
文件而不必修改原始JavaScript文件,是否有自动设置相应标头的方法。
I already do something similar to serve minified JavaScript if it exists and I'm not debugging: 我已经做过类似的事情来提供缩小的JavaScript,如果它存在并且我没有调试:
RewriteCond %{REQUEST_FILENAME} \.(php|js)
RewriteRule . - [E=DEBUG:true]
RewriteCond %{ENV:DEBUG} !^true$
RewriteCond %{REQUEST_URI} ^(.+)\.js$
RewriteCond %{DOCUMENT_ROOT}%1-min.js -f
RewriteRule . %1-min.js [L]
I guess it basically boils down to: how can I add an extra HTTP header if a certain file exists for a certain type of request? 我想它基本归结为:如果特定类型的请求存在某个文件,我如何添加额外的HTTP标头?
UPDATE UPDATE
The solution is as follows: 解决方案如下:
# If serving minified js and a SourceMap file exists, send appropriate header
RewriteCond %{REQUEST_URI} ^(.+-min\.js)$
RewriteCond %{DOCUMENT_ROOT}%1.map -f
RewriteRule . - [L,E=SOURCE_MAP:%{REQUEST_URI}.map]
Header set X-SourceMap "%{SOURCE_MAP}e" env=SOURCE_MAP
See edit made by Nev Stokes above for the final solution 请参阅以上Nev Stokes的编辑,以获得最终解决方案
RewriteCond %{REQUEST_FILENAME} \.(php|js)
RewriteRule . - [E=DEBUG:true]
RewriteCond %{ENV:DEBUG} !^true$
RewriteCond %{REQUEST_URI} ^(.+)\.js$
RewriteCond %{DOCUMENT_ROOT}%1-min.js -f
RewriteRule [^/]+\.js$ %1-min.js [L,E=SOURCE_MAP:%{REQUEST_URI}]
Header set X-SourceMap "%{SOURCE_MAP}e" env=SOURCE_MAP
I could not test the Header part as my test server doesn't have mod_headers installed. 我无法测试Header部分,因为我的测试服务器没有安装mod_headers。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.