aio-libs / aio-libs/aiohttp

Handling static URLs

Đang mở
#7,940 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
Python
Star
16.5k
Fork
2.4k
Merge trung bình
17 giờ 22 phút
Pull request đã merge (30 ngày)
212

Mô tả

Handling static URLs in aiohttp seems to be a little messy, and I'm wondering if we can do something better. I'd like to discuss potential solutions here.

So, we can use `add_static()` to create a static resource in aiohttp, but then if we're in aiohttp-jinja2 we can't use that.
Instead, in aiohttp-jinja2 we have a `static_root_key` on the app, which the user can set (e.g. to `/static/`) and then aiohttp-jinja2 uses that as a prefix for static resources.
aiohttp-devtools sets this same key on the app in order for aiohttp-jinja2 to work. It sets it to a mutable value which is updated after the app is created to point to the static server (e.g. `http://localhost:8001/static/`).

One problem here is that we don't get all the usual arguments/features in the static resource (e.g. for cache busting) when we use static files in aiohttp-jinja2. (e.g. https://github.com/aio-libs/aiohttp-jinja2/issues/673) So, it would be nice if we can somehow merge these together, so that aiohttp-jinja2 can work with the static resources better. However, a resource is meant to route locally to the app, but we need something which can be set to a remote location (for aiohttp-devtools, or using a CDN).

Another problem is that if you need to specify the URL for a resource while creating the app (e.g. https://github.com/aio-libs/aiohttp-admin/blob/master/examples/demo/app.py#L62), then it won't work correctly with aiohttp-devtools. This is because it will set `static_root_key` _after_ the app is created. Maybe a solution could involve url_for() returning some kind of dynamic object which recalculates the URL when used?

-----

After writing this all down, I'm wondering if the solution might be to have some kind of new resource object which is not added to the router (or only when explicitly requested?), so it can be used to create URLs. And it can return some kind of object which is tied to the resource, so it will always be up-to-date with the correct prefix. And it should be accessible on a preset key that exists in aiohttp, so all libraries can easily find it.

Maybe that looks something like:
```
app[static].set_root("/static") # Local
my_js_file = app[static] / "js" / "my.js"
print(my_js_file) # /static/js/my.js

app[static].set_root("http://localhost:8001") # aiohttp-devtools
print(my_js_file) # http://localhost:8001/js/my.js
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.