You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony项目Webpack Encore生产环境资源路径配置异常求助

Hey there! Let's work through this asset path issue you're hitting with Webpack Encore in your Symfony production environment. I’ve dealt with similar quirks before, so here’s a breakdown of the most likely fixes:

1. Fix Webpack Encore's Public Path Configuration

The most common culprit here is the setPublicPath setting in your webpack.config.js. This tells Encore where your built assets will live relative to your domain root.

Double-check that this is set correctly for production. If your assets are hosted at http://mydomain.fr/build/, your config should look like this:

// webpack.config.js
const Encore = require('@symfony/webpack-encore');

Encore
    // ... other existing config (output path, entry points, etc.)
    .setPublicPath('/build')
    // If you're using versioned assets (filename hashes), make sure this matches too
    .setManifestKeyPrefix('build/')
;

module.exports = Encore.getWebpackConfig();

If you're deploying to a subdirectory (e.g., http://mydomain.fr/my-app/build/), update setPublicPath to /my-app/build instead.

2. Verify Symfony's Asset Base Path Settings

Symfony's framework configuration might be overriding or supplementing the Encore path. Check your config/packages/framework.yaml (or framework_prod.yaml if you have environment-specific configs) for the assets section:

# config/packages/framework.yaml
framework:
    assets:
        # Uncomment and set this if your assets live under a base path (e.g., subdirectory)
        # base_path: '%env(ASSETS_BASE_PATH)%'

If you need a dynamic base path for production, add an entry to your .env.prod file:

# .env.prod
ASSETS_BASE_PATH=/build

This ensures Symfony resolves asset paths correctly alongside Encore's manifest.

3. Clear Production Cache (Critical!)

Symfony caches asset mappings in production, so even if you fix the config, old cached paths might still be used. Run this command to clear the cache:

php bin/console cache:clear --env=prod

Always do this after deploying asset changes to production.

4. Validate the Generated Manifest File

After running yarn run encore production, check the public/build/manifest.json file. This file maps your source asset paths to their built versions. Look for your footer/footer.css entry—it should look something like:

{
    "footer/footer.css": "/build/footer/footer.css"
}

If the value here is incorrect (e.g., missing a leading slash or using the wrong domain), your Encore config is still off. Double-check setPublicPath again.

5. Ensure You're Using Twig's asset() Function

Make sure in your Twig templates you’re using Symfony’s asset() function to reference assets, not hardcoding URLs:

{# Correct #}
<link rel="stylesheet" href="{{ asset('build/footer/footer.css') }}">

{# Wrong (hardcoded paths won't use Encore's manifest) #}
<link rel="stylesheet" href="/build/footer/footer.css">

The asset() function reads from the manifest file and outputs the correct production path automatically.


内容的提问来源于stack exchange,提问作者R Lopez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:58:27