使用 Catalyst 从 CDN 提供静态文件
我正在尝试从 CDN 而不是 /static 文件夹提供所有静态文件(js、css、图像)。我开始编写自己的子程序来重写图像 URL:
sub uri_for_image {
my ( $c, $path, @args ) = @_;
my $uri = $c->uri_for($path, @args);
if (MyApp->config->{use_cdn}) {
my $cdn = MyApp->config->{cdn_location};
$uri = $path;
$uri =~ s/^/$cdn/si;
}
return $uri;
}
但后来我意识到需要做更多的工作才能自动重新路由所有静态文件。我应该如何为静态文件设置 CDN url?
我正在考虑配置 Static::Simple 但它似乎只有本地文件位置的设置。
I'm trying to serve all static files (js, css, images) from a CDN instead of the /static folder. I started writing my own sub to rewrite image URLs:
sub uri_for_image {
my ( $c, $path, @args ) = @_;
my $uri = $c->uri_for($path, @args);
if (MyApp->config->{use_cdn}) {
my $cdn = MyApp->config->{cdn_location};
$uri = $path;
$uri =~ s/^/$cdn/si;
}
return $uri;
}
But then I realized its gonna take a lot more work to get all the static files rerouted automatically. How should I go about setting a CDN url for static files?
I was looking into configuring Static::Simple but it seems to only have settings for location of files locally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您认为 Catalyst 实际上可以并且应该为您做什么?
恐怕如果您想在应用程序之外生成自己的链接,那么您几乎只能靠自己了——Catalyst 无法真正做或提供任何东西来帮助您,不是吗?
或者您只是希望
uri_for
粉碎以/static
开头的所有路径以指向您的 CDN?你可以只包装uri_for
并让它做到这一点吗?我认为你的版本(有一个单独的方法)更干净整洁,但如果你所有的静态内容都进入 CDN,那么只提供你想要的行为 uri_for 是合理的。
What do you think Catalyst actually can and should do for you here?
I'm afraid if you want to generate your own links which are outside the app, then you're pretty much on your own—there isn't anything Catalyst can really do or provide to help you here, is there?
Or do you just want
uri_for
to smash all paths starting in/static
to point your CDN? As you can just wrapuri_for
and make it do that?I think your version (having a separate method) is cleaner and neater, but if all your static content is going onto the CDN, then just giving
uri_for
the behavior you want is reasonable.2013 年晚些时候,有人编写了
Catalyst ::Plugin::ExternalURI
用于此目的。从概要部分:
Later in 2013 someone wrote the
Catalyst::Plugin::ExternalURI
for this purpose.From the synopsis section: