[ script vs script defer vs script async ]

young_palleteยท2021๋…„ 6์›” 1์ผ
1

HTML

๋ชฉ๋ก ๋ณด๊ธฐ
2/3
post-thumbnail
post-custom-banner

๐Ÿ’ก ์„œ๋ก 

์šฐ๋ฆฌ๊ฐ€ HTML์„ ์ ‘ํ•˜๊ณ , JS์™€ CSS๋ฅผ ๋‹ค๋ฃฐ ๋•Œ, ํ”ํžˆ๋“ค ๊ฒฝํ—˜ํ–ˆ๋˜ ์‹ค์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๋ฐ”๋กœ ํŒŒ์ผ ๋ฐฐ์น˜๋กœ ์ธํ•ด ์›ํ•˜๋Š” ์ ์šฉ์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค!

์™œ ์šฐ๋ฆฌ์˜ HTML์€ ๋„๋Œ€์ฒด, ๋ถ„๋ช… script์™€ linkํƒœ๊ทธ๋ฅผ ์•„๋ฆ„๋‹ต๊ฒŒ ๊ฑธ์–ด๋†จ๋Š”๋ฐ, ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ธ๊ฐ€!

๋Œ€์ถฉ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์ข€ ๋” ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๊ณ  ๊ธฐ๋ณธ์„ ์ตํžˆ๊ธฐ ์œ„ํ•ด ์ด ์ฃผ์ œ๋ฅผ ๋‹ค๋ฃจ๊ฒŒ ๋๋‹ค.

๐Ÿ’ฌ ๋ณธ๋ก 

HTML ํŒŒ์‹ฑ์€ ๋™๊ธฐ์ 

HTML์ด ์ด ๊ธ€์„ ๋ณธ๋‹ค๋ฉด ์•„๋งˆ ์ด๋ ‡๊ฒŒ ์ค‘์–ผ๊ฑฐ๋ฆด ๊ฒƒ์ด๋‹ค.

์•„๋‹ˆ... ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ์š”... (๊ธ์ ๊ธ์ )

์šฐ๋ฆฌ๋Š” HTML๋ฅผ ์„œ์นญ์„ ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด ๋ถ„๋ช… HTML์˜ ๋ Œ๋”๋ง ๊ณผ์ •์˜ ์ˆจ๊ฒจ์ง„ ๋น„๋ฐ€์„ ์•Œ๊ฒŒ ๋˜๋Š”๋ฐ, ๊ทธ๊ฒƒ์€ ๋ฐ”๋กœ,

HTML์€ synchronousํ•˜๊ฒŒ css์™€ jsํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!

์—ฌ๊ธฐ์„œ ์ด์ œ script async ๊ณผ script defer์˜ ์ฐจ์ด์ ์ด ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ๋œ๋‹ค.

script fetch ์›๋ฆฌ

๊ฐ€๋ น ๋‹ค์Œ๊ณผ ๊ฐ™์ด HTML์„ ํŒŒ์‹ฑํ•˜๋Š” ๊ณผ์ •์„ ์„ ์œผ๋กœ ๊ทธ๋ ค๋ณธ๋‹ค๊ณ  ํ•œ๋‹ค.
์ด๋•Œ ์Šคํฌ๋ฆฝํŠธ์˜ ์œ„์น˜๋Š” ๊ฐ€์šด๋ฐ๋‹ค.
HTML ํŒŒ์‹ฑ

์ด๋•Œ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ๋งŒ๋‚˜๋ฉด, HTML์€ ํŒŒ์‹ฑ์„ ์ค‘๋‹จํ•˜๊ณ , ์„œ๋‘˜๋Ÿฌ script๋ฅผ ๊ฐ€์ ธ์™€์„œ, ์‹คํ–‰ํ•œ๋‹ค. ์ดํ›„ ์žฌ๊ฐœํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
SCRIPT fetch

ํ•œ๊ณ„์ 

๋ณธ ์˜ˆ์‹œ๋Š” ๋งค์šฐ ๋‹จํŽธ์ ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ script ํŒŒ์ผ์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž. ๊ณผ์—ฐ ์‚ฌ์šฉ์ž๋Š” ๊ทธ ๋Œ€๊ธฐ์‹œ๊ฐ„์„ ๊ด€์šฉํ• ๊นŒ?

๊ฒฐ๊ตญ ์ด๋Ÿฌํ•œ ๋Œ€๊ธฐ์‹œ๊ฐ„์„ ์ค„์ด๋ฉด์„œ, ๊ธฐ์กด์˜ HTML ํŒŒ์‹ฑ ๋ฐ script fetch๋Š” ์›ํ™œํžˆ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ–ˆ๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ํƒ„์ƒํ•œ ๊ฒƒ์ด script async๊ณผ script defer์ด๋‹ค.

script async

async์ด๋ผ๋Š” ๋ง ๋‹ต๊ฒŒ ๋น„๋™๊ธฐ์ ์œผ๋กœ script๋ฅผ fetchํ•˜๊ณ , ์™„๋ฃŒํ•˜๋ฉด ๊ทธ๋•Œ HTML ํŒŒ์‹ฑ์„ ์ค‘๋‹จ์‹œํ‚ค๊ณ  ์‹คํ–‰์— ์ง‘์ค‘ํ•œ๋‹ค.
๊ฒฐ๊ณผ์ ์œผ๋กœ ํŒŒ์‹ฑ ์‹œ๊ฐ„์€ ๋‹จ์ถ•์‹œ์ผœ (script fetch์™€ ๋ณ‘ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ) ์‹œ๊ฐ„์„ ์•ž๋‹น๊ธธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.
script async

async ํ•œ๊ณ„์ 

์–ผ๋งˆ๋‚˜ ์ด์ƒ์ ์ธ๊ฐ€! ํ•˜์ง€๋งŒ ์™„๋ฒฝํ•œ ์ด์ƒ์€ ์—†๋‹ค.
๊ฐ€๋ น ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.
์—ฌ๊ธฐ์„œ script1์ด ๋ฌด์กฐ๊ฑด script2๋ณด๋‹ค ์šฐ์„  ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฐ€์ •์„ ๊น”๊ฒ ๋‹ค.
async shortcoming
script2๊ฐ€ ๋” ์ž‘์•„์„œ ๋น ๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋˜๊ณ , ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋จผ์ € ์‹คํ–‰๋˜์–ด๋ฒ„๋ฆฌ๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•œ๋‹ค! ํ•œํŽธ, ์šฐ๋ฆฌ๋Š” ๊ฒฐ๊ตญ JS์—์„œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‚˜ ์‚ดํŽด ๋ณด๋ฉฐ ์‚ฝ์งˆ์„ ํ•  ๊ฒƒ์ด๋‹ค!

๊ทธ๋ ‡๊ธฐ์—, script fetch์— async์„ ์ ์šฉํ•  ๊ฑฐ๋ผ๋ฉด, ๋…๋ฆฝ์ ์ด์–ด์•ผ ํ•œ๋‹ค.

script defer

๋ง ๊ทธ๋Œ€๋กœ ์—ฐ๊ธฐํ•œ๋‹ค๋Š” ๊ฑฐ๋‹ค!

  • HTML์ด ๊ตฌ๋ฌธ ๋ถ„์„์„ ์™„๋ฃŒํ•  ๋•Œ๊นŒ์ง€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.
  • ๋˜ํ•œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋œ๋‹ค.
  • ๋Œ€๊ฐœ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งจ ์•„๋ž˜์— ๋ฐฐ์น˜ํ•˜์ง€๋งŒ ์–ด๋–ค ์ด์œ ์ƒ์—์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ฐ˜๋“œ์‹œ ์œ„์ชฝ์— ๋ฐฐ์น˜ํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ ์Šคํฌ๋ฆฝํŠธ fetch ์‹œ๊ฐ„๋งŒํผ ์ด ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œํ‚ค๋ฉด์„œ, ์Šคํฌ๋ฆฝํŠธ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    script defer

ํ•œ๊ณ„์ 

์•„์ง๊นŒ์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์†๋„๋ฅผ ์ค„์ด๋ฉด์„œ ๋ฌธ์ œ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘์—์„  ๊ฐ€์žฅ ๋‚˜์€ ๋ฐฉ๋ฒ•์ธ ๋“ฏ ์‹ถ๋‹ค. (ํ˜น์—ฌ๋‚˜ ํ•œ๊ณ„์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋ง์”€ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.)

์•„, script async / defer์€ src ์†์„ฑ์ด ํ•„์ˆ˜์ด๋ฉฐ, ์ผ๋ถ€ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ง€์›๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•˜๋‹ˆ(๋งค์šฐ ์ ์ง€๋งŒ), ์ฐธ๊ณ ํ•˜์ž.

๐Ÿ“ƒ ์ฐธ๊ณ ์ž๋ฃŒ

์ฐธ๊ณ  1 - Five minutes to understand async and defer
์ฐธ๊ณ  2 - script์˜ async์™€ defer ์†์„ฑ(๋ธ”๋กœ๊ทธ)

profile
People are scared of falling to the bottom but born from there. What they've lost is nth. ๐Ÿ˜‰
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€