ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
File path ဆိုတာ လမ်းညွှန်ချက် တစ်ခုလိုပါပဲ။ "https://example.com/photo.jpg" က အိမ်လိပ်စာ အပြည့်အစုံ ပေးလိုက်တာ ဖြစ်ပြီး ဘယ်နေရာကနေ ဖတ်ဖတ် တူညီတဲ့ နေရာကိုပဲ ညွှန်ပါတယ်။ "images/photo.jpg" ကတော့ "ငါ အခု ရပ်နေတဲ့ နေရာကနေ images ဆိုတဲ့ အခန်းထဲ ဝင်ပါ" လို့ ပြောတာမျိုးဖြစ်လို့ ဘယ်ကနေ စထွက်လဲပေါ် မူတည်ပြီး ရောက်မယ့်နေရာ ကွာသွားပါတယ်။
HTML page တစ်ခုမှာ စထွက်တဲ့ နေရာက အဲဒီ page ကိုယ်တိုင်ရဲ့ URL ပါ။ Browser က src ဒါမှမဟုတ် href ကို တွေ့တိုင်း path ကို page ရဲ့ URL နဲ့ ပေါင်းပြီး full URL တစ်ခု တွက်ထုတ်ကာ အဲဒီ address ကို server ဆီ request ပို့ပါတယ်။ Image, CSS, JavaScript, link အားလုံး ဒီ စည်းမျဉ်း တစ်ခုတည်းကို လိုက်နာပါတယ်။
Problem → Why → Solution
ပြဿနာ: ကိုယ့်စက်မှာ ပေါ်ပြီး deploy လုပ်တော့ ပုံတွေ ပျောက်
Aye က portfolio site တစ်ခုကို Windows laptop ပေါ်မှာ ရေးပြီး index.html ကို double-click နှိပ်ဖွင့်ကြည့်တော့ အားလုံး ပြည့်ပြည့်စုံစုံ ပေါ်ပါတယ်။ ဒါပေမယ့် hosting ပေါ် upload လုပ်လိုက်တော့ logo မပေါ်၊ CSS မဝင်၊ profile ပုံနေရာမှာ broken-image icon ပဲ ကျန်ပါတယ်။ Code ထဲမှာ အမှား သုံးခု ပုန်းနေပါတယ်၊ သုံးခုလုံးက laptop ပေါ်မှာ အလုပ်လုပ်နေလို့ မမြင်ခဲ့တာပါ။
<!-- Deployed at https://aye.github.io/portfolio/ -->
<link rel="stylesheet" href="/css/style.css">
<img src="C:\Users\Aye\Desktop\portfolio\images\logo.png" alt="Aye's logo">
<!-- the real file on disk is images/profile.jpg -->
<img src="Images/Profile.JPG" alt="Aye smiling at a desk">ဘာကြောင့် laptop မှာတော့ အလုပ်လုပ်ခဲ့တာလဲ
C:\ နဲ့ စတဲ့ path က Aye ရဲ့ hard disk ကိုပဲ ညွှန်ပါတယ်၊ website ကို ကြည့်တဲ့ တခြားသူတွေရဲ့ စက်မှာ အဲဒီ folder မရှိပါဘူး။ Windows ရဲ့ file system က စာလုံးအကြီးအသေး မခွဲလို့ Images/Profile.JPG က images/profile.jpg ကို ရှာတွေ့ပေမယ့် hosting အများစု သုံးတဲ့ Linux server တွေက နှစ်ခုကို မတူတဲ့ file အဖြစ် သတ်မှတ်ပါတယ်။ /css/style.css ကတော့ site root ကနေ စလို့ /portfolio/ subfolder ကို ကျော်သွားပြီး https://aye.github.io/css/style.css ကို request လုပ်မိပါတယ်။
Core concept: path တိုင်းကို page ရဲ့ URL ကနေ resolve လုပ်တယ်
Browser က page တစ်ခုကို load လုပ်တဲ့အခါ အဲဒီ page ရဲ့ URL ကို base URL အဖြစ် မှတ်ထားပါတယ်။ src="..." ဒါမှမဟုတ် href="..." ထဲက value တိုင်းကို ဒီ base URL နဲ့ ပေါင်းပြီး full URL တစ်ခု ဖြစ်အောင် resolve လုပ်ပါတယ်။ ဒီလုပ်ငန်းစဉ်က HTML, CSS, JavaScript ဘယ် file အမျိုးအစားအတွက်မဆို အတူတူပါပဲ — browser ဘက်ကနေ ကြည့်ရင် အရာအားလုံးက URL တွေပဲ ဖြစ်ပြီး ကိုယ့် computer ပေါ်က folder တွေ မဟုတ်ပါဘူး။
| Path အမျိုးအစား | ဥပမာ | ဘယ်ကနေ စလဲ |
|---|---|---|
| Absolute URL | https://cdn.example.net/a.png | ဘယ်ကမှ မစ၊ address အပြည့်အစုံ ပါပြီးသား |
| Root-relative | /images/logo.png | လက်ရှိ site ရဲ့ domain root |
| Document-relative | images/a.png, ../css/site.css | လက်ရှိ page ရှိနေတဲ့ folder |
Document-relative path ကို resolve လုပ်ပုံက ဒီလိုပါ — page URL ရဲ့ နောက်ဆုံး slash နောက်က file name ကို ဖြုတ်လိုက်ရင် page ရှိတဲ့ folder ကျန်ပါတယ်။ https://example.com/blog/post.html ဆိုရင် folder က /blog/ ပါ။ images/a.png ဆိုရင် /blog/images/a.png ဖြစ်ပြီး ./ က "ဒီ folder" ကို ဆိုလိုကာ ../ ကတော့ folder တစ်ဆင့် အပေါ် တက်တာပါ၊ ဒါကြောင့် ../css/site.css က /css/site.css ဖြစ်ပါတယ်။ Root ထက် ပိုမတက်နိုင်လို့ ../ အများကြီး ထည့်လည်း domain root မှာပဲ ရပ်ပါတယ်။
Trailing slash တစ်ခုက ရလဒ်ကို ပြောင်းစေနိုင်ပါတယ်။ https://example.com/blog/ က blog folder ထဲမှာ ရှိနေတာပါ၊ https://example.com/blog (slash မပါ) ကတော့ root folder ထဲက "blog" ဆိုတဲ့ file တစ်ခုလို ဆက်ဆံခံရလို့ images/a.png က /images/a.png ဖြစ်သွားပါတယ်။
URL တွေက file system path တွေ မဟုတ်ပါဘူး။ Separator က အမြဲ forward slash (/) ဖြစ်ရပါတယ် — browser တွေက http(s) URL ထဲက backslash ကို / အဖြစ် တိတ်တိတ်လေး ပြောင်းပေးပေမယ့် spec အရ မှားတာဖြစ်ပြီး တခြား tool တွေက နားမလည်နိုင်ပါဘူး။ Server အများစုမှာ path တွေက case-sensitive ဖြစ်ပြီး space လို စာလုံးတွေကို %20 အဖြစ် percent-encode လုပ်ရပါတယ်။ ဒါကြောင့် file name တွေကို စာလုံးအသေး၊ hyphen နဲ့ space မပါအောင် ပေးတာ အကောင်းဆုံးပါ။
Element တစ်ခုက page တစ်ခုလုံးရဲ့ စထွက်မယ့်နေရာကို ပြောင်းနိုင်ပါတယ် — head ထဲက <base href="..."> က relative path တိုင်းအတွက် base ကို page URL အစား အစားထိုးပါတယ်။ သိပ်မလိုအပ်သလို လူတွေကို အံ့သြစေတတ်ပါတယ်၊ ထည့်လိုက်တာနဲ့ page ပေါ်က relative link နဲ့ ပုံ အားလုံးရဲ့ ပန်းတိုင်ကို တိတ်တိတ်လေး ပြောင်းပစ်လို့ပါ။
- Page ရဲ့ URL ကနေ စပါ
- File name ကိုဖြုတ်ပြီး folder ယူပါ
- Path ကိုလိုက်ပါ: ../ ဆို တစ်ဆင့်တက်
- ရလာတဲ့ full URL ကို request လုပ်
/ နဲ့ စရင် folder ကို ကျော်ပြီး domain root ကနေ စပါတယ်၊ https:// နဲ့ စရင် လမ်းလျှောက်စရာ မလိုပါဘူး။
အထွေထွေ ပုံစံ
<!-- absolute: scheme + host + path -->
<script src="https://host.example/path/file.js"></script>
<!-- root-relative: from the domain root -->
<link rel="stylesheet" href="/folder/file.css">
<!-- document-relative: from this page's folder -->
<img src="folder/file.ext" alt="Description">
<a href="../other-page.html">Up one folder</a>Path ရဲ့ ရှေ့ဆုံး စာလုံးတွေက ဘယ်နေရာကနေ စရမလဲ ဆုံးဖြတ်ပါတယ် — https:// ဆို address အပြည့်၊ / ဆို domain root၊ ကျန်တာဆို page ရဲ့ folder ပါ။ ဒီစည်းမျဉ်းက src, href နဲ့ URL လက်ခံတဲ့ attribute တိုင်းမှာ အတူတူပါပဲ။
Example: path သုံးမျိုးနဲ့ ပုံ သုံးပုံ
ဒီ code မှာ img element သုံးခုရှိပြီး တစ်ခုစီက path အမျိုးအစား တစ်မျိုးစီ သုံးထားပါတယ်။ ဒီ page ကို https://example.com/blog/post.html မှာ တင်ထားတယ်လို့ ယူဆပြီး ပုံတစ်ပုံချင်းစီအတွက် browser က ဘယ် URL ကို request လုပ်မလဲ ခန့်မှန်းကြည့်ပါ။
<img src="https://example.com/photo.jpg" alt="From another website">
<img src="/images/logo.png" alt="Logo from root images folder">
<img src="images/profile.jpg" alt="Profile from current folder">Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Absolute URL
<img src="https://example.com/photo.jpg" alt="From another website">Scheme (https:) နဲ့ host ပါပြီးသားမို့ page ဘယ်မှာ ရှိရှိ https://example.com/photo.jpg ကိုပဲ request လုပ်ပါတယ်။ တခြား domain က file တွေ၊ CDN တွေအတွက် ဒီပုံစံ သုံးပါတယ်။
- Root-relative
<img src="/images/logo.png" alt="Logo from root images folder">/ နဲ့ စလို့ /blog/ folder ကို လျစ်လျူရှုပြီး domain root ကနေ စပါတယ် — ရလဒ်က https://example.com/images/logo.png ပါ။ Page ကို ဘယ် folder ထဲ ရွှေ့ရွှေ့ ဒီ URL မပြောင်းပါဘူး။
- Document-relative
<img src="images/profile.jpg" alt="Profile from current folder">Page ရဲ့ folder ဖြစ်တဲ့ /blog/ ကနေ စလို့ https://example.com/blog/images/profile.jpg ကို request လုပ်ပါတယ်။ Page ကို တခြား folder ထဲ ရွှေ့ရင် ဒီ path က ညွှန်တဲ့ နေရာလည်း လိုက်ရွှေ့သွားပါတယ်။
- alt
alt="Profile from current folder"Path မှားပြီး file ရှာမတွေ့ရင် browser က broken-image icon နဲ့ alt text ကို ပြပါတယ်။ ဒါကြောင့် path ပျက်နေတာကို စစ်တဲ့အခါ alt text ပေါ်လာတာက ပထမဆုံး သဲလွန်စ ဖြစ်ပါတယ်။
Page က https://example.com/blog/post.html မှာ ရှိရင် browser က ဒီ URL သုံးခုကို request လုပ်ပါတယ်:
1. https://example.com/photo.jpg
2. https://example.com/images/logo.png
3. https://example.com/blog/images/profile.jpg
File ရှိတဲ့ ပုံကို ပြပြီး မရှိတဲ့ ပုံနေရာမှာ broken-image icon နဲ့ alt text ပေါ်ပါတယ်။Folder tree တစ်ခုပေါ်မှာ path တွေကို လိုက်ကြည့်ရအောင်
Site တစ်ခုမှာ page တွေ folder အဆင့်ဆင့် ရှိလာတဲ့အခါ relative path တွေကို ခေါင်းထဲမှာ ပုံဆွဲကြည့်ရတာ အကူအညီ ဖြစ်ပါတယ်။ အောက်က tree မှာ blog/post.html က အပေါ်က folder ထဲက file တွေကို ../ နဲ့ ညွှန်ရပါတယ်။
my-site served at https://example.com/
--------------------------------------
my-site/ -> https://example.com/
|-- index.html
|-- css/
| `-- site.css
|-- images/
| `-- logo.png
`-- blog/
|-- post.html (page we are writing)
`-- images/
`-- profile.jpg| blog/post.html ထဲမှာ ရေးတာ | Browser request လုပ်တဲ့ URL |
|---|---|
| images/profile.jpg | https://example.com/blog/images/profile.jpg |
| ../images/logo.png | https://example.com/images/logo.png |
| ../css/site.css | https://example.com/css/site.css |
| ../index.html | https://example.com/index.html |
CSS ထဲက url() က stylesheet ကနေ စတယ်
css/site.css ထဲမှာ background-image: url("../images/logo.png") လို့ ရေးရင် relative path ကို HTML page ကနေ မဟုတ်ဘဲ stylesheet ရဲ့ URL (/css/site.css) ကနေ resolve လုပ်လို့ /images/logo.png ရပါတယ်။ ဒါကြောင့် ဘယ် page ကနေ ဒီ CSS ကို load လုပ်လုပ် ရလဒ်က အတူတူပါပဲ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Framework အများစုမှာ static file တွေကို folder တစ်ခုထဲ ထားပြီး site root ကနေ serve လုပ်ပါတယ်၊ ဥပမာ Next.js ရဲ့ public/ folder ထဲက public/logo.png ကို /logo.png လို့ root-relative နဲ့ ညွှန်ရပါတယ်။ Page တွေ folder အဆင့်ဆင့် ရှိတဲ့ site ကြီးတွေမှာ root-relative path တွေက ../../ တွေ ရေတွက်စရာ မလိုလို့ ပိုတည်ငြိမ်ပါတယ်။ ဒါပေမယ့် site ကို https://user.github.io/repo/ လို subpath အောက်မှာ deploy လုပ်ရင် / က repo ကို ကျော်သွားလို့ build tool ရဲ့ base path setting (ဥပမာ Vite ရဲ့ base option) ကို သတ်မှတ်ရပါတယ်။
Absolute URL တွေကိုတော့ CDN ပေါ်က font, library, ပုံတွေနဲ့ email template လို ဘယ်နေရာကနေ ဖွင့်ဖွင့် အလုပ်လုပ်ရမယ့် link တွေအတွက် သုံးပါတယ်။ ကိုယ့် site ထဲက page တွေကို https://mysite.com/... absolute URL နဲ့ link ချိတ်ထားရင် staging server မှာ test လုပ်တဲ့အခါ production ဆီ ခုန်သွားတတ်လို့ internal link တွေအတွက် relative ဒါမှမဟုတ် root-relative ကို ရွေးပါ။ Team တွေက file name တွေကို စာလုံးအသေးနဲ့ hyphen ပဲ သုံးဖို့ စည်းကမ်းချပြီး CI ထဲမှာ link checker run ကာ broken link နဲ့ ပျောက်နေတဲ့ asset တွေကို deploy မလုပ်ခင် ဖမ်းပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Local drive path ကို src ထဲ ထည့်တာ
Editor ကနေ copy ယူလာတဲ့ C:\Users\Aye\site\logo.png ဒါမှမဟုတ် file:///C:/... ကို src ထဲ ထည့်မိတာပါ။ Browser က C: ကို URL scheme တစ်ခုလို ဖတ်ပြီး load မလုပ်နိုင်ပါဘူး၊ ကိုယ့်စက်မှာ ပေါ်ခဲ့ရင်တောင် အခြားသူတွေမှာ မပေါ်ပါဘူး။ Project folder ထဲက relative path (images/logo.png) ကို သုံးပါ။
2. File name ရဲ့ စာလုံးအကြီးအသေး မကိုက်တာ
Disk ပေါ်မှာ hero.jpg ဖြစ်ပြီး HTML ထဲမှာ Hero.JPG လို့ ရေးမိတာပါ။ Windows နဲ့ macOS ရဲ့ default setting မှာ ပေါ်ပေမယ့် Linux server ပေါ်မှာ 404 ဖြစ်ပါတယ်။ File name အားလုံးကို စာလုံးအသေးနဲ့ ပေးပြီး HTML ထဲမှာလည်း တစ်ပုံစံတည်း ရေးပါ။
3. Subpath အောက်မှာ root-relative path သုံးတာ
Site ကို /portfolio/ လို subfolder အောက်မှာ host လုပ်ထားပြီး /css/style.css လို့ ရေးရင် /portfolio/ ကို ကျော်သွားလို့ 404 ဖြစ်ပါတယ်။ index.html ကို double-click နဲ့ ဖွင့်ရင်လည်း / က drive root (file:///C:/) ကို ညွှန်ပါတယ်။ ဒီလို အခြေအနေမှာ css/style.css လို relative path ဒါမှမဟုတ် build tool ရဲ့ base setting ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- File နဲ့ folder name တွေကို စာလုံးအသေး၊ hyphen နဲ့ space မပါဘဲ ပေးပါ (my-photo.jpg)။
- Project တစ်ခုလုံးမှာ internal link တွေအတွက် relative ဒါမှမဟုတ် root-relative တစ်မျိုးတည်းကို ရွေးပြီး ရောမသုံးပါနဲ့။
- File:// နဲ့ ဖွင့်မယ့်အစား local web server (ဥပမာ VS Code Live Server) နဲ့ test လုပ်ပါ၊ ဒါမှ path တွေ production မှာလို အလုပ်လုပ်ပါမယ်။
Mini challenge
Mini challenge: blog post page ရဲ့ path တွေ
အပေါ်က folder tree ကို သုံးပြီး blog/post.html ကို ရေးပါ။ Page ကို https://example.com/blog/post.html မှာ တင်ထားတယ်လို့ ယူဆပြီး file တိုင်းကို မှန်ကန်တဲ့ path နဲ့ ချိတ်ပါ။
- css/site.css ကို relative path နဲ့ link လုပ်ရမယ်
- images/logo.png (root folder ထဲက) ကို relative path နဲ့ ပြရမယ်
- blog/images/profile.jpg ကို ပြရမယ်၊ img တိုင်းမှာ alt ပါရမယ်
- Home page (index.html) ဆီ ပြန်သွားတဲ့ link တစ်ခုနဲ့ https://developer.mozilla.org/ ဆီ absolute link တစ်ခု ထည့်ရမယ်
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first post</title>
<!-- TODO: link ../ up to css/site.css -->
</head>
<body>
<!-- TODO: logo from the root images folder -->
<!-- TODO: profile photo from blog/images -->
<!-- TODO: nav links: home page (relative) and MDN (absolute) -->
</body>
</html>ရလဒ်: Browser က https://example.com/css/site.css, https://example.com/images/logo.png, https://example.com/blog/images/profile.jpg ကို request လုပ်ပါတယ်။ "Home" link က https://example.com/index.html ကို ညွှန်ပြီး "MDN Web Docs" link က https://developer.mozilla.org/ ကို ညွှန်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
post.html က blog/ folder ထဲမှာ ရှိလို့ root folder ထဲက file တိုင်းကို ../ နဲ့ စရပါမယ်။ blog/images/ ကတော့ post.html နဲ့ folder တူလို့ images/ နဲ့ တိုက်ရိုက် စလို့ ရပါတယ်။
အဖြေ ကြည့်ရန်
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first post</title>
<link rel="stylesheet" href="../css/site.css">
</head>
<body>
<header>
<img src="../images/logo.png" alt="Example Blog logo" width="120" height="40">
<nav>
<a href="../index.html">Home</a>
<a href="https://developer.mozilla.org/">MDN Web Docs</a>
</nav>
</header>
<main>
<h1>My first post</h1>
<img src="images/profile.jpg" alt="The author holding a coffee cup"
width="200" height="200">
<p>Every path on this page starts from the blog/ folder.</p>
</main>
</body>
</html>Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Browser က src/href ထဲက path တိုင်းကို page ရဲ့ URL နဲ့ ပေါင်းပြီး full URL တစ်ခုအဖြစ် resolve လုပ်ပါတယ်။
- https:// က address အပြည့်၊ / က domain root၊ ကျန်တာက page ရဲ့ folder ကနေ စပြီး ../ က တစ်ဆင့် တက်ပါတယ်။
- C:\ လို local path၊ case မကိုက်တဲ့ file name နဲ့ subpath အောက်က / path တွေက deploy လုပ်ပြီးမှ ပျက်တတ်ပါတယ်။
- CSS file ထဲက url() ကတော့ HTML page ကနေ မဟုတ်ဘဲ stylesheet ရဲ့ နေရာကနေ resolve ဖြစ်ပါတယ်။
HTML Living Standard — Links — WHATWG