Skip to content

Commit 985cf53

Browse files
committed
feat(image): integrate CF Images srcset + responsive sizes
- Generate srcset only for Cloudflare Images URLs (/cdn-cgi/image/<options>/…). - Strip existing width/height from <options>, then rebuild srcset from a fixed width list (descending): 1920,1600,1440,1280,1024,960,800,720,640,512,480,400,320. - Set the fallback src to the 720px variant. - Inject sizes if absent, tuned to site breakpoints and in-post layout: sizes="(min-width: 1400px) 800px, (min-width: 850px) 720px, 92vw". - Do not touch non-CF URLs (backwards-compatible for local/other CDNs).
1 parent 21a8ce5 commit 985cf53

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

_includes/refactor-content.html

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,87 @@
183183
{% assign _left = _left | append: ' fetchpriority=' | append: _fetch_priority %}
184184
{% endif %}
185185

186+
{%- comment -%}
187+
Cloudflare Images (/cdn-cgi/image/(options)/...)용 srcset/sizes 지정:
188+
- (options)에서 width/height 제거 후, 고정 폭 목록으로 srcset 생성
189+
- sizes="100vw"
190+
- 기본 src는 720px 버전으로 지정
191+
- 반응형 이미지는 Cloudflare Images만 지원(/cdn-cgi/image/가 없는 경우엔 변경하지 않음)
192+
{%- endcomment -%}
193+
{%- assign _current_src_for_cf = _final_src | default: _src -%}
194+
{%- if _current_src_for_cf contains '/cdn-cgi/image/' -%}
195+
{%- assign _cf_split_1 = _current_src_for_cf | split: '/cdn-cgi/image/' -%}
196+
{%- assign _cf_image_zone = _cf_split_1[0] -%}
197+
{%- assign _cf_after = _cf_split_1[1] -%}
198+
199+
{%- assign _cf_segments = _cf_after | split: '/' -%}
200+
{%- assign _cf_options = _cf_segments[0] | strip -%}
201+
{%- assign _cf_rest = "" -%}
202+
{%- for _seg in _cf_segments offset:1 -%}
203+
{%- assign _cf_rest = _cf_rest | append: "/" | append: _seg -%}
204+
{%- endfor -%}
205+
206+
{%- comment -%} 기존 옵션에서 width/height 제거 {%- endcomment -%}
207+
{%- assign _cf_base_opts = "" -%}
208+
{%- if _cf_options != "" -%}
209+
{%- assign _cf_opts_list = _cf_options | split: "," -%}
210+
{%- for _opt in _cf_opts_list -%}
211+
{%- assign _kv = _opt | split: "=" -%}
212+
{%- assign _key = _kv[0] | strip -%}
213+
{%- if _key != "width" and _key != "height" and _key != "" -%}
214+
{%- if _cf_base_opts != "" -%}{%- assign _cf_base_opts = _cf_base_opts | append: "," -%}{%- endif -%}
215+
{%- assign _cf_base_opts = _cf_base_opts | append: _opt | strip -%}
216+
{%- endif -%}
217+
{%- endfor -%}
218+
{%- endif -%}
219+
220+
{%- comment -%}
221+
고정 width 목록 및 srcset 생성
222+
기본 src는 720px로 설정
223+
{%- endcomment -%}
224+
{%- assign _cf_widths_str = "1920,1600,1440,1280,1024,960,800,720,640,512,480,400,320" -%}
225+
{%- assign _cf_widths = _cf_widths_str | split: "," -%}
226+
227+
{%- assign _cf_srcset = "" -%}
228+
{%- assign _cf_src_720 = "" -%}
229+
230+
{%- for _w in _cf_widths -%}
231+
{%- assign _w_clean = _w | strip -%}
232+
{%- if _w_clean != "" -%}
233+
{%- assign _per_opts = _cf_base_opts -%}
234+
{%- if _per_opts != "" -%}{%- assign _per_opts = _per_opts | append: "," -%}{%- endif -%}
235+
{%- assign _per_opts = _per_opts | append: "width=" | append: _w_clean -%}
236+
237+
{%- assign _per_url = _cf_image_zone | append: "/cdn-cgi/image/" | append: _per_opts | append: _cf_rest -%}
238+
239+
{%- if _cf_srcset != "" -%}{%- assign _cf_srcset = _cf_srcset | append: ", " -%}{%- endif -%}
240+
{%- assign _cf_srcset = _cf_srcset | append: _per_url | append: " " | append: _w_clean | append: "w" -%}
241+
242+
{%- if _w_clean == "720" -%}
243+
{%- assign _cf_src_720 = _per_url -%}
244+
{%- endif -%}
245+
{%- endif -%}
246+
{%- endfor -%}
247+
248+
{%- if _cf_src_720 == "" -%}
249+
{%- comment -%} 혹시 목록에 720이 없다면 첫 항목으로 대체 {%- endcomment -%}
250+
{%- assign _cf_src_720 = _cf_before | append: "/cdn-cgi/image/" | append: _cf_base_opts | append: "," | append: "width=" | append: _cf_widths[0] | append: _cf_rest -%}
251+
{%- endif -%}
252+
253+
{%- comment -%}
254+
_left (img 속성 문자열)에서 src URL 교체 및 srcset/sizes 추가
255+
- 이미 srcset/sizes가 있으면 중복 추가 방지
256+
{%- endcomment -%}
257+
{%- assign _left = _left | replace_first: _current_src_for_cf, _cf_src_720 -%}
258+
259+
{%- unless _left contains ' srcset=' -%}
260+
{%- assign _left = _left | append: ' srcset="' | append: _cf_srcset | append: '"' -%}
261+
{%- endunless -%}
262+
{%- unless _left contains ' sizes=' -%}
263+
{%- assign _left = _left | append: ' sizes="(min-width: 1400px) 800px, (min-width: 850px) 720px, 92vw"' -%}
264+
{%- endunless -%}
265+
{%- endif -%}
266+
186267
{% if page.layout == 'home' %}
187268
<!-- create the image wrapper -->
188269
{% assign _wrapper_start = '<div class="preview-img ' | append: _class | append: '">' %}

0 commit comments

Comments
 (0)