Back To Home

Name: Ngày 13: Environment Configuration (CM/CD)

🎯 Mục tiêu Ngày 13

Bạn sẽ học:

  1. CM và CD khác nhau như thế nào

  2. Cách cấu hình App_Config cho từng Environment

  3. Cách tạo các config file CM-only và CD-only

  4. Cách dùng patch để bật/tắt chức năng theo environment

  5. Best practices cho môi trường thực tế


🧩 1. CM vs CD là gì?

Environment Mục đích Đặc điểm
CM – Content Management Tác giả tạo nội dung Bật đầy đủ admin, SPE, indexing, media upload, workflows
CD – Content Delivery Trả nội dung cho end-user Tắt admin, Tắt indexing, cache tối ưu, cho performance

🏗 2. Cách Sitecore phân chia CM/CD bằng cấu hình

✔ CM và CD dùng cùng 1 codebase

→ Cách tách môi trường dựa trên thư mục config riêng:

 
App_Config/Include /CM /CD /Foundation /Feature /Project

📁 3. Tạo Config CM-only

Tạo file:

 
App_Config/Include/CM/MyProject.CM.config

Ví dụ bật Experience EditorContent Testing chỉ cho CM:

 
<?xml version="1.0"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <settings> <!-- Chỉ bật Analytics/Testing trên CM --> <setting name="Analytics.Enabled"> <patch:attribute name="value">true</patch:attribute> </setting> <setting name="ContentTesting.Enabled"> <patch:attribute name="value">true</patch:attribute> </setting> </settings> </sitecore> </configuration>

📁 4. Tạo Config CD-only

Tạo file:

 
App_Config/Include/CD/MyProject.CD.config

Ví dụ tắt Experience Editor, Indexing, và Content Testing (chỉ delivery):

 
<?xml version="1.0"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <settings> <!-- CD không cần phân tích --> <setting name="Analytics.Enabled"> <patch:attribute name="value">false</patch:attribute> </setting> <!-- CD không cần Experience Editor --> <setting name="ExperienceEditor.Enabled"> <patch:attribute name="value">false</patch:attribute> </setting> <!-- CD không index documents --> <setting name="Indexing.Enabled"> <patch:attribute name="value">false</patch:attribute> </setting> </settings> </sitecore> </configuration>

🛠 5. Dùng biến EnvironmentName (Sitecore 9+)

Bạn có thể đặt tên môi trường trong Web.config:

 
<sc.variable name="EnvironmentName">CM</sc.variable>

Hoặc cho CD:

 
<sc.variable name="EnvironmentName">CD</sc.variable>

Sau đó file patch có thể dùng:

 
<setting name="Environment.IsCM"> <patch:attribute name="value">$(EnvironmentName)=='CM'</patch:attribute> </setting>

🔥 6. Patch theo role (CM/CD)

Sitecore 9.3+ hỗ trợ:

  • role:define

  • role:require

  • role:deny

VD: Chỉ chạy pipeline processor trên CM

 
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/"> <sitecore role:require="ContentManagement"> <pipelines> <initialize> <processor type="MyProject.InitProcessor, MyProject"/> </initialize> </pipelines> </sitecore> </configuration>

🧪 7. Ví dụ thực tế: Bật Media Upload chỉ trên CM

CM

 
<setting name="Media.Upload.Enabled"> <patch:attribute name="value">true</patch:attribute> </setting>

CD

 
<setting name="Media.Upload.Enabled"> <patch:attribute name="value">false</patch:attribute> </setting>

📘 8. Best Practice cho CM/CD

✔ Tạo 2 folder riêng

 
App_Config/Include/CM App_Config/Include/CD

✔ Không viết trực tiếp vào Web.config

→ chỉ patch

✔ Tắt những thứ này trên CD:

  • Experience Editor

  • Sitecore Admin

  • Indexing

  • SPE (PowerShell)

  • Content Testing

  • Analytics

✔ Enable cache tối đa trên CD

Donald Trump

Để trở thành người chiến thắng, bạn cần biết khi nào là đủ. Đôi khi trong cuộc sống, bạn phải từ bỏ cuộc chiến và chuyển sang mục tiêu mới mang lại hiệu quả hơn

Related Post