Cloud 6.03

Generate Static Content On Demand

Understanding SCD_ON_DEMAND behavior, four applied patches, time measurements, security implications, and when to use it.

Exam Critical: Understanding SCD_ON_DEMAND behavior, patches, and limitations is important for the exam!

Static Content On Demand

mindmap root((SCD_ON_DEMAND)) Default Production No static generation Faster page load Read only files Why Added Development speed Skip build and deploy Generate as needed Patches Applied configure_scd_on_demand respect_minification_override unlock_locale_editing trim_static_content When to Use Development phase Pre release only NOT for production Time Impact First load slow 9 4s Subsequent fast 300ms

Default Magento Behavior in Production Mode

Production Mode Static Content Handling

Default Production Behavior

By default, production mode disables generating any static content files.

Why?
  • Speed: Uploading content from the website is faster
  • Efficiency: Magento doesn't have to look for new content
  • Security: Static files are read-only (pre-generated)

The Problem

This default behavior prevents on-demand deployment of static content. You cannot generate static files dynamically in production mode without special configuration.

Why SCD_ON_DEMAND Mode Was Added

The "Magento Way" Solution

Purpose of SCD_ON_DEMAND

As with everything "Magento," there's a way around the production mode restriction!

  • Development speed: Skip SCD during build and deploy
  • Flexibility: Generate static content as needed on first request
  • Rapid deployment: Faster deployment cycles during development
  • Dynamic generation: Support new themes/locales without redeployment

How It Works

When SCD_ON_DEMAND is enabled, Magento generates static content files dynamically on the first request for each file, then caches them for subsequent requests.

How Magento Behaves with SCD_ON_DEMAND

Configuration and Setup

Step 1: Enable in .magento.env.yaml

stage:
  deploy:
    SCD_ON_DEMAND: true

Step 2: Deploy Scripts Update env.php

When deploying in on-demand mode, the deploy scripts add this to app/etc/env.php:

'static_content_on_demand_in_production' => 1

Critical Detail

Even more important: patches are applied to Magento so it understands this flag. The functionality is somewhat present in core, but patches ensure full support.

Patches Applied for SCD_ON_DEMAND

Four Critical Patches

1. configure_scd_on_demand

Purpose

If in production mode AND static_content_on_demand_in_production is configured, then the asset can be created.

Additional Functionality
  • Allows newly generated files to be minified
  • Enables dynamic asset creation in production
  • Core logic for on-demand generation

2. respect_minification_override

Purpose

Adds OR logic for minification decisions:

  • IF production SCD is enabled OR
  • IF force_html_minification is enabled
  • THEN the file is minified

This ensures minification works correctly with on-demand generation.

3. unlock_locale_editing

The Problem

When in production mode, the Locale is locked in Store → Configuration → General → Locale Options to the locales for which themes have been generated.

The Solution

This patch unlocks the locale setting so you can generate static content for any locale, not just pre-generated ones.

4. trim_static_content

Purpose

Removes /static/version[NUMBER] from the $_GET['resource'].

Why?

The version number in static URLs can interfere with on-demand generation. This patch normalizes the resource path for proper dynamic generation.

When to Use SCD_ON_DEMAND

Appropriate Use Cases

Good Use Cases

This should only be done in the development (pre-release) phase of a project.

  • Development environment: Rapid iteration and testing
  • Integration environment: Frequent deployments
  • Pre-release testing: Testing new themes or locales
  • Demo environments: Quick setup without full SCD

NOT for Production

SCD_ON_DEMAND should NOT be used in production!

Reasons Why:
  1. Inconsistencies: No guarantee files will be identical to intended output
  2. Performance: First requests are very slow (9+ seconds)
  3. Security risk: Static files are no longer read-only, opening attack vectors
  4. Unpredictable behavior: Different users might trigger different generation patterns
  5. Resource usage: Generates files on production servers, consuming resources

Security Considerations

Read-Only vs Writable Static Files

Pre-Generated SCD (Secure)
  • Files are read-only
  • Cannot be modified after generation
  • Protects against injection attacks
  • Consistent across all requests
SCD_ON_DEMAND (Less Secure)
  • Files are writable
  • Can be modified dynamically
  • Potential attack vector
  • Risk of inconsistent generation

Security Risk

While the risk is small, there is some possibility of attack as static files are no longer read-only with SCD_ON_DEMAND. Attackers could potentially inject malicious code into dynamically generated static files.

Time Measurements

Performance Impact

File First Load (Generation) Subsequent Loads (Cached)
styles-m.css 9.4 seconds < 300 ms
styles-l.css 4.54 seconds < 300 ms

Key Insights

  • First load: VERY slow (4-9+ seconds per file)
  • Subsequent loads: Fast (< 300ms, served from cache)
  • User impact: First visitors experience very slow page loads
  • Production impact: Unacceptable for production environments

Why This Matters

These generation times occur on the first request for each static file. In production, this means the first users to visit your site (or any new page) will experience extremely slow load times while static content is generated.

How It Works: Request Flow

On-Demand Generation Process

Step-by-Step Flow

  1. Request arrives: Browser requests static file (e.g., styles-m.css)
  2. File check: Magento checks if file exists in pub/static
  3. Not found: File doesn't exist yet
  4. Generate: Magento generates the file dynamically (SLOW - 4-9+ seconds)
  5. Cache: Generated file is saved to pub/static
  6. Serve: File is served to browser
  7. Subsequent requests: File exists, served quickly (< 300ms)

Practical Experience Exercise

Testing SCD_ON_DEMAND

Step 1: Enable SCD_ON_DEMAND

Add to .magento.env.yaml:

stage:
  deploy:
    SCD_ON_DEMAND: true

Step 2: Deploy Your Development Project

git add .magento.env.yaml
git commit -m "Enable SCD_ON_DEMAND for testing"
git push origin development

Step 3: Switch Locale for Frontend

  1. Navigate to Store → Configuration → General → Locale Options
  2. Change locale (e.g., from en_US to fr_FR)
  3. Save configuration
  4. Clear cache

Step 4: Observe Behavior

  • First page load: Notice very slow load time (9+ seconds)
  • Check browser network tab: See static files taking 4-9 seconds each
  • Refresh page: Notice much faster load (< 300ms per file)
  • Check pub/static: See dynamically generated files

Expected Observations

  • Locale can be changed without redeployment
  • Static content generates automatically on first request
  • Subsequent requests are fast (files cached)
  • No build or deploy time spent on SCD

SCD_ON_DEMAND Configuration

Complete Configuration Example

Optimal Development Setup

# .magento.env.yaml
stage:
  global:
    MIN_LOGGING_LEVEL: debug
  build:
    SKIP_SCD: true  # Skip in build
  deploy:
    SCD_ON_DEMAND: true  # Generate on demand
    SKIP_SCD: true  # Skip in deploy
  post_deploy:
    WARM_UP_PAGES:
      - "/"
      - "/customer/account/login"

Configuration Notes

  • SKIP_SCD: true in build and deploy prevents traditional SCD
  • SCD_ON_DEMAND: true enables on-demand generation
  • WARM_UP_PAGES: Optional, pre-generates content for specified pages

Comparison: Traditional SCD vs SCD_ON_DEMAND

Aspect Traditional SCD SCD_ON_DEMAND
When Generated During build or deploy On first request
Build Time Long (generates all content) Fast (nothing generated)
Deploy Time Long if SCD in deploy Fast (nothing generated)
First Page Load Fast (content pre-generated) Very slow (4-9+ seconds per file)
Subsequent Loads Fast Fast (< 300ms)
File Security Read-only (secure) Writable (security risk)
Consistency Guaranteed identical Potential inconsistencies
Locale Flexibility Locked to pre-generated Any locale on demand
Production Use Recommended NOT recommended
Development Use Optional Ideal for rapid iteration

Exam Tips

Key Points to Remember

  • Default production: Disables static content generation
  • Why added: Development speed, skip SCD, generate as needed
  • Configuration: SCD_ON_DEMAND: true in .magento.env.yaml
  • env.php flag: static_content_on_demand_in_production
  • Four patches: configure_scd_on_demand, respect_minification_override, unlock_locale_editing, trim_static_content
  • configure_scd_on_demand: Enables asset creation in production, allows minification
  • unlock_locale_editing: Unlocks locale selection in Store Configuration
  • trim_static_content: Removes /static/version[NUMBER] from URL
  • When to use: Development/pre-release ONLY
  • NOT for production: Inconsistencies, slow first load, security risk
  • Time impact: 4-9+ seconds first load, < 300ms cached
  • Security: Files writable (attack vector), not read-only
  • Locale flexibility: Can generate for any locale on demand