[英]Grunt URL rewrite in Angular application on page reload
Here's what the livereload block looks like in Grunt file:下面是 Grunt 文件中 livereload 块的样子:
livereload: {
options: {
open: true,
middleware: function(connect, options, middleware) {
var optBase = typeof options.base === "string"
? [options.base]
: options.base;
return [
[
require("connect-modrewrite")(["!(\\..+)$ / [L]"])
].concat(
optBase.map(function(path) { return connect.static(path); })
),
connect.static(".tmp"),
connect().use("/bower_components", connect.static("./bower_components")),
connect().use("/app/styles", connect.static("./app/styles")),
connect.static(appConfig.app)
];
}
}
}
However, if my URL has a '.'但是,如果我的 URL 有一个 '.' (period) in it, Grunt fails to reload the page.
(句号) 在其中,Grunt 无法重新加载页面。 I am using HTML5 Mode in my Angular app and that works fine.
我在我的 Angular 应用程序中使用 HTML5 模式并且工作正常。
Could I please know what part of我能知道是哪一部分吗
[
require("connect-modrewrite")(["!(\\..+)$ / [L]"])
].concat(
optBase.map(function (path) { return connect.static(path); })
)
is causing it to fail and how do I fix this?导致它失败,我该如何解决?
Note: It fails only on page reload.注意:它仅在页面重新加载时失败。 The first time I visit the route it works, then if I hit refresh it fails.
我第一次访问它工作的路线时,如果我点击刷新它就会失败。
Could I please know what part of [this snippet] is causing it to fail and how do I fix this?
我能否知道 [此代码段] 的哪一部分导致它失败,我该如何解决?
The connect-modrewrite
rule here appears to be intended for rewriting non-static-asset URLs only.此处的
connect-modrewrite
规则似乎仅用于重写非静态资产 URL。
[
require("connect-modrewrite")(["!(\\..+)$ / [L]"])
].concat(
optBase.map(function (path) { return connect.static(path); })
)
The rule passed in here, "!(\\\\..+)$ / [L]"
, is an inverted URL matching rule to rewrite all URLs that don't contain a period and one or more characters back to the base URL ( /
).这里传入的规则
"!(\\\\..+)$ / [L]"
是一个反向 URL 匹配规则,用于将所有不包含句点和一个或多个字符的 URL 重写回基本 URL ( /
)。 So, if your routes include a period, they will not be rewritten... this is why it fails when live reload runs.因此,如果您的路由包含句点,它们将不会被重写……这就是运行实时重新加载时失败的原因。
One suggestion to remedy this is to more explicitly avoid rewrites for requests to static assets:解决此问题的一个建议是更明确地避免重写对静态资产的请求:
require("connect-modrewrite")([
'!\\.html|\\.js|\\.svg|\\.css|'
+ '\\.scss.*|\\.woff.*|\\.gif.*|\\.png$ '
// etc...
+ '/ [L]'
])
Notice that this rule also begins with an exclamation point ( !
).请注意,此规则也以感叹号 (
!
) 开头。 Instead of relying solely on the presence of a period, it more explicitly targets URLs that don't include certain file extensions-- along with the period!它不是仅仅依赖句点的存在,而是更明确地针对不包含某些文件扩展名的 URL——以及句点! (:
(:
Note: code for the suggestion was found on searchcode.com here注意:建议的代码可以在 searchcode.com 上找到
I hope this helps!我希望这有帮助! HMU with any questions, corrections, or well actually-s.
HMU 有任何问题、更正或实际上-s。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.