ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ NestJS Lazy Loading Modules – ๋ชจ๋ ์ง์ฐ ๋ก๋ฉ ์์ ๊ฐ์ด๋
octo54 2025. 5. 13. 12:08๐ NestJS Lazy Loading Modules – ๋ชจ๋ ์ง์ฐ ๋ก๋ฉ ์์ ๊ฐ์ด๋
NestJS์์ Lazy Loading(์ง์ฐ ๋ก๋ฉ) ๋ชจ๋์ ์ ํ๋ฆฌ์ผ์ด์
์ด ์์๋ ๋ ๋ชจ๋ ๋ชจ๋์ ๋ก๋ฉํ์ง ์๊ณ ,
ํ์ํ ๋ ๋ชจ๋์ ๋ก๋ํ์ฌ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๊ณผ ์ด๊ธฐํ ์๊ฐ์ ์ต์ ํํ๋ ๊ธฐ์ ์
๋๋ค.
์ด๋ฒ ๊ธ์ NestJS ๊ณต์ ๋ฌธ์ Lazy Loading Modules๋ฅผ ๊ธฐ๋ฐ์ผ๋ก,
์ค๋ฌด์์ Lazy Loading์ ํจ๊ณผ์ ์ผ๋ก ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช
ํฉ๋๋ค.
๐ก "๋๊ท๋ชจ ์ ํ๋ฆฌ์ผ์ด์ ์์๋ ๋ชจ๋ ๋ชจ๋์ ํ ๋ฒ์ ๋ก๋ํ๋ฉด ๋ฉ๋ชจ๋ฆฌ์ ์ฑ๋ฅ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค."
Lazy Loading์ ํตํด ์ฑ๋ฅ ์ต์ ํ์ ์ด๊ธฐํ ์๊ฐ์ ์ค์ผ ์ ์์ต๋๋ค.
โ Lazy Loading์ด๋?
NestJS์์๋ ๋ชจ๋์ ์ ์ ์ผ๋ก ์ ์ํ๊ณ , ์ ํ๋ฆฌ์ผ์ด์
์์ ์ ๋ชจ๋ ๋ชจ๋์ ๋ก๋ํฉ๋๋ค.
ํ์ง๋ง ๋๊ท๋ชจ ํ๋ก์ ํธ์์๋ ํ์ํ ๋ ๋ก๋ฉํ๋ **์ง์ฐ ๋ก๋ฉ(Lazy Loading)**์ด ํ์์
๋๋ค.
๐ก ์ Lazy Loading์ด ํ์ํ๊ฐ?
- ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ: ํ์ํ ๋ชจ๋๋ง ๋ก๋ํ์ฌ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์ต์ํ
- ์ฑ๋ฅ ํฅ์: ์ด๊ธฐํ ์๊ฐ์ ์ค์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์๋ ๊ฐ์
- ํจ์จ์ ์ธ ๋ชจ๋ ๊ด๋ฆฌ: ํน์ ์์ฒญ์ด ๋ฐ์ํ ๋๋ง ๋ชจ๋์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋
๐ฆ ๊ธฐ๋ณธ ๊ตฌ์กฐ – Lazy Loading ์ค์
โ ๋์ ๋ชจ๋ ๋ก๋ฉ ํจ์
// lazy.module.ts
import { Module, DynamicModule } from '@nestjs/common';
@Module({})
export class LazyModule {
static forRoot(): DynamicModule {
return {
module: LazyModule,
providers: [
{
provide: 'LAZY_SERVICE',
useValue: { message: 'Lazy loaded!' },
},
],
exports: ['LAZY_SERVICE'],
};
}
}
โ ๋ฉ์ธ ๋ชจ๋์์ ๋์ ๋ก๋ฉ
// app.module.ts
import { Module, DynamicModule } from '@nestjs/common';
@Module({
imports: [
LazyModule.forRoot(), // ์ง์ ํธ์ถ
],
})
export class AppModule {}
โ Lazy Service ์ฌ์ฉ
@Injectable()
export class AppService {
constructor(@Inject('LAZY_SERVICE') private lazyService: any) {}
getLazyMessage() {
return this.lazyService.message;
}
}
๐บ ๊ด๊ณ
์ด ๊ตฌ๊ฐ์ Lazy Loading์ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ์๊ฐํ๋ ํต์ฌ ๊ตฌ๊ฐ์ ๋๋ค.
์ ๋์ผ์ค ๊ด๊ณ ๋ฅผ ์ฝ์ ํ์ฌ CTR์ ๊ทน๋ํํ ์ ์๋ ์์น์ ๋๋ค.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXX"
data-ad-slot="YYYYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
๐ ๏ธ ์ค์ ํ์ฉ – ์์ฒญ ์ ๋ชจ๋ ๋์ ๋ก๋ฉ
โ AppService์์ Lazy Module ๋์ ๋ก๋ฉ
@Injectable()
export class AppService {
constructor(private moduleRef: ModuleRef) {}
async getLazyData() {
const lazyModule = await this.moduleRef.create(LazyModule.forRoot());
const lazyService = lazyModule.get('LAZY_SERVICE');
return lazyService.message;
}
}
โ ๋ผ์ฐํธ์์ ํธ์ถ
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('lazy')
async getLazy() {
return this.appService.getLazyData();
}
}
โ๏ธ ์ค๋ฌด์์ ์ ์ฉํ Lazy Loading ํจํด
ํจํด ์ค๋ช
๋๊ท๋ชจ ๋ชจ๋ ๋ถ๋ฆฌ | ๋ํ ๋ชจ๋์ ์์ฒญ ์ ๋ก๋ฉํ์ฌ ์ด๊ธฐํ ์๊ฐ ์ ๊ฐ |
๋น์ ๊ธฐ์ ์ฌ์ฉ ๋ชจ๋ | ํน์ ์ด๋ฒคํธ๋ ์ํฉ์์๋ง ๋ก๋ฉ |
์ธ๋ถ API ์ฐ๋ ๋ชจ๋ | API ์์ฒญ ์์๋ง ํด๋ผ์ด์ธํธ ๋ชจ๋ ๋ก๋ |
๋ํ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ชจ๋ | ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ต์ํํ๊ธฐ ์ํด ํ์ํ ๋ ๋ก๋ |
๐ก ๊ณ ๊ธ ๊ธฐ๋ฒ – HTTP ์์ฒญ์ ๋ฐ๋ผ ๋ชจ๋ ๋ก๋
โ ์กฐ๊ฑด๋ถ ๋ก๋ฉ ์๋น์ค
@Injectable()
export class ConditionalService {
constructor(private moduleRef: ModuleRef) {}
async getDynamicService(type: string) {
if (type === 'A') {
const module = await this.moduleRef.create(LazyModule.forRoot());
return module.get('LAZY_SERVICE');
}
return { message: 'Default service' };
}
}
โ ์ปจํธ๋กค๋ฌ์์ ์กฐ๊ฑด๋ถ ํธ์ถ
@Controller()
export class AppController {
constructor(private readonly conditionalService: ConditionalService) {}
@Get('dynamic/:type')
async getDynamic(@Param('type') type: string) {
const service = await this.conditionalService.getDynamicService(type);
return service.message;
}
}
๐ง Lazy Loading ์ ์ฉ ์ ์ฃผ์์ฌํญ
- ๋ชจ๋๊ฐ ์์กด์ฑ ์ฃผ์:
- Lazy ๋ก๋ฉ ๋ชจ๋์ด ์ข ์์ฑ์ ๊ฐ์ง๋ฉด ๋ก๋ฉ์ด ๋ณต์กํด์ง ์ ์์ต๋๋ค.
- ์ฑ๋ฅ๊ณผ ๋ฉ๋ชจ๋ฆฌ ๊ท ํ:
- ๋ชจ๋ ๋ชจ๋์ Lazy๋ก ํ๋ฉด ์คํ๋ ค ์ฑ๋ฅ์ด ๋จ์ด์ง ์ ์์ผ๋ฏ๋ก ์์ฃผ ์ฌ์ฉํ๋ ๋ชจ๋์ ์ ์ ๋ก๋ฉ์ผ๋ก ๊ด๋ฆฌํฉ๋๋ค.
- ๋ก๋ ์๋ ํ์ธ:
- Lazy ๋ก๋ฉ์ผ๋ก ์ธํด ์์ฒญ ์๋๊ฐ ๋๋ ค์ง ๊ฐ๋ฅ์ฑ์ด ์์ต๋๋ค. ์ฑ๋ฅ ํ ์คํธ๋ฅผ ๋ณํํ์ธ์.
โ Lazy Loading ๋ชจ๋ ํ์ฉ ์ ๋ต
๋ชจ๋ ์ ํ ๊ถ์ฅ ๋ก๋ฉ ๋ฐฉ์
์ธ์ฆ ๋ชจ๋ | ์ ์ ๋ก๋ฉ |
์ธ๋ถ API ๋ชจ๋ | Lazy ๋ก๋ฉ |
๋์ฉ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ชจ๋ | Lazy ๋ก๋ฉ |
๊ด๋ฆฌ์ ๋์๋ณด๋ ๋ชจ๋ | Lazy ๋ก๋ฉ |
์ผ๋ฐ ์๋น์ค ๋ชจ๋ | ์ ์ ๋ก๋ฉ |
๐ ๋ง๋ฌด๋ฆฌ ์์ฝ
ํน์ง ์ค๋ช
๋ฉ๋ชจ๋ฆฌ ์ต์ ํ | ์ฌ์ฉ ์์๋ง ๋ก๋ฉํ์ฌ ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น ๋ฐฉ์ง |
๋์ ๋ก๋ฉ | ํน์ ์ํฉ์์๋ง ๋ชจ๋์ ๋ก๋ฉํ์ฌ ์ฑ๋ฅ ์ต์ ํ |
์ค์๊ฐ ์์ฒญ ์ฒ๋ฆฌ | ์์ฒญ์ ๋ฐ๋ผ ๋ชจ๋์ ๋ก๋ฉํ์ฌ ๋ถํ์ํ ๋ฆฌ์์ค ์ฌ์ฉ ๋ฐฉ์ง |
์ ์ฐํ ๋ชจ๋ ๊ด๋ฆฌ | ๋๊ท๋ชจ ํ๋ก์ ํธ์์ ๊ด๋ฆฌ ํจ์จ์ฑ ์ฆ๋ |
NestJS Lazy Loading,NestJS ๋์ ๋ชจ๋ ๋ก๋ฉ,NestJS ModuleRef,NestJS ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ,NestJS ๋๊ท๋ชจ ํ๋ก์ ํธ ์ค๊ณ,NestJS ๋ชจ๋ ๊ด๋ฆฌ,NestJS Lazy Module ์ค์ ,NestJS ๊ณ ๊ธ ๋ชจ๋ ์ค์ ,NestJS ์ฑ๋ฅ ํฅ์,NestJS ๋ชจ๋ ์ง์ฐ ๋ก๋ฉ
'framework > NestJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- Ktor
- ์น๊ฐ๋ฐ
- gatsbyjs
- ๋ฐฑ์๋๊ฐ๋ฐ
- llm
- PostgreSQL
- ํ๋ก ํธ์๋๋ฉด์
- kotlin
- NestJS
- REACT
- Prisma
- ํ๋ก ํธ์๋
- seo ์ต์ ํ 10๊ฐ
- Docker
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
- flax
- ๋ฅ๋ฌ๋
- SEO์ต์ ํ
- App Router
- fastapi
- AI์ฑ๋ด
- CI/CD
- SEO ์ต์ ํ
- Python
- rag
- ํ์ด์ฌ์๊ณ ๋ฆฌ์ฆ
- nodejs
- nextJS
- Next.js
- JAX
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |