Client Hints - The-Next-Web-Research-Lab/the-next-web-research-lab.github.io GitHub Wiki
Client Hints๋ก ํด๋ฆฐ ํ๊ฒ ๋ธ๋ผ์ฐ์ ๋ถ๊ธฐํ๊ธฐ
User-Agent์ ๋ฌธ์ ์
User-Agent๋
๋ธ๋ผ์ฐ์ ๋ฅผ ๊ตฌ๋ถํ ๋ ์ฌ์ฉํ๊ณค ํฉ๋๋ค. ํ์ง๋ง ์๋์ ๊ฐ์ด Chromium ๊ธฐ๋ฐ ๋ธ๋ผ์ฐ์ ์์ window.navigator.userAgent๋ก User-Agent๋ฅผ ์กฐํํ๋ฉด ๋์ผํ ๋ฌธ์์ด์ ๋ณผ ์ ์์ต๋๋ค.
// Chrome
Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/51.0.2704.103
Safari/537.36
// Opera
Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/51.0.2704.106
Safari/537.36
OPR/38.0.2220.41
// Edge
Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/91.0.4472.124
Safari/537.36
Edg/91.0.864.59
User-Agent์์ Chrome ๋ธ๋ผ์ฐ์ ๋ฅผ ๊ตฌ๋ถํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น์? OPR, Edg๊ฐ ํฌํจ๋์ง ์์ผ๋ฉด Chrome์ผ๋ก ๊ตฌ๋ถ ๊ฐ๋ฅํ ๊ฒ ๊ฐ์ง๋ง Chromium ๊ธฐ๋ฐ์ ๋ค๋ฅธ ๋ธ๋ผ์ฐ์ ์์ ์ ๊ทผํ๋ฉด ์ด ๋ฐฉ์์ ๋ฒ๊ทธ๋ฅผ ์ ๋ฐํ ์ ์์ต๋๋ค.
const userAgent = window.navigator.userAgent;
const isEdge = userAgent.includes("Edg");
const isOpera = userAgent.includes("OPR");
// Chrome์ด ์๋ Edge, Opara ์ด์ธ Chromium ๊ธฐ๋ฐ ๋ธ๋ผ์ฐ์ ์์ ํญ์ true
const isChrome = userAgent.includes("Chrome") && !isEdge && !isOpera;
Client Hints ์ฌ์ฉํ๊ธฐ
Chromium ๊ธฐ๋ฐ์ ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ช ํํ ๊ตฌ๋ถํ๊ธฐ ์ํด์๋ Client Hints์ ํ์ฉํด์ผ ํฉ๋๋ค. Client Hints๋ User-Agent์ ์ค์ฉ์ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ง๋ค์ด์ก๊ณ , ๋ธ๋ผ์ฐ์ ์์ ๋ฌธ์์ด์ ์ ๋ฌํฉ๋๋ค.
Client Hints๋ window.navigator.userAgentData.brands๋ฅผ ์ฌ์ฉํด์ ์กฐํํ ์ ์์ต๋๋ค.
Chromium ๊ธฐ๋ฐ ๋ธ๋ผ์ฐ์ ์์ Client Hints๋ฅผ ์กฐํํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ณ๋ก ๋ค๋ฅธ ๋ฌธ์์ด์ด ํฌํจ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
// Chrome
[
{
"brand": "Not/A)Brand",
"version": "99"
},
{
"brand": "Google Chrome",
"version": "115"
},
{
"brand": "Chromium",
"version": "115"
}
]
// Opera
[
{
"brand": "Not/A)Brand",
"version": "99"
},
{
"brand": "Opera",
"version": "101"
},
{
"brand": "Chromium",
"version": "115"
}
]
// Edge
[
{
"brand": "Not/A)Brand",
"version": "99"
},
{
"brand": "Microsoft Edge",
"version": "115"
},
{
"brand": "Chromium",
"version": "115"
}
]
์์ ๊ฐ์ด ๋ธ๋ผ์ฐ์ ์ ๋ฌธ์์ด์ ๋ช ํํ ๊ตฌ๋ถํ ์ ์์ต๋๋ค. ๋๋ฌธ์ User-Agent์ ๋ค๋ฅด๊ฒ ์๋์ ๊ฐ์ด ๋ฒ๊ทธ ์๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
const userAgentBrands = window.navigator
.userAgentData.brands.map(({brand}) => brand);
const isEdge = userAgentBrands.includes("Microsoft Edge");
const isOpera = userAgentBrands.includes("Opera");
const isChrome = userAgentBrands.includes("Google Chrome");