javascriptsymfonywebpackwebpack-encoreyarnpkg-v2

Webpack is taking a long time to load Js files


I installed Symfony 6.1.5 with PHP 8.1 on my Ubuntu 20.04 system. I am using nginx 1.18.0 and mysql 5.7. After, I ran a command composer require symfony/webpack-encore-bundle for using encore and ran yarn install. I added my css and js files to the assets folder. And I imported my css and js files in my app.js file. It works fine, but it taking a long time to load. When I open Chrome Network inspection tool, it shows me, that my js files are loading 23 sec and 19 sec. Why so long? How can I fix it? enter image description here

My webpack.config.js below:

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

// Manually configure the runtime environment if not already configured yet by the 
// "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore
// directory where compiled assets will be stored
 .setOutputPath('public/build/')
// public path used by the web server to access the output path
 .setPublicPath('/build')
// only needed for CDN's or subdirectory deploy
//.setManifestKeyPrefix('build/')

/*
 * ENTRY CONFIG
 *
 * Each entry will result in one JavaScript file (e.g. app.js)
 * and one CSS file (e.g. app.css) if your JavaScript imports CSS.
 */
.addEntry('app', './assets/app.js')

// enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)
 .enableStimulusBridge('./assets/controllers.json')

// When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
 .splitEntryChunks()

// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
.enableSingleRuntimeChunk()

/*
 * FEATURE CONFIG
 *
 * Enable & configure other features below. For a full
 * list of features, see:
 * https://symfony.com/doc/current/frontend.html#adding-more-features
 */
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())

// configure Babel
// .configureBabel((config) => {
//     config.plugins.push('@babel/a-babel-plugin');
// })

// enables and configure @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
    config.useBuiltIns = 'usage';
    config.corejs = '3.23';
})

// enables Sass/SCSS support
//.enableSassLoader()

// uncomment if you use TypeScript
//.enableTypeScriptLoader()

// uncomment if you use React
//.enableReactPreset()

// uncomment to get integrity="..." attributes on your script & link tags
// requires WebpackEncoreBundle 1.4 or higher
//.enableIntegrityHashes(Encore.isProduction())

// uncomment if you're having problems with a jQuery plugin
.autoProvideVariables({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery'
})
;

module.exports = Encore.getWebpackConfig();

My app.js file:

// any CSS you import will output into a single css file (app.css in this case)
import './css/bootstrap.min.css';
import './css/templatemo-style.css';
import './fontawesome/css/all.min.css';


import './js/plugins';

// start the Stimulus application
import './bootstrap';

Solution

  • I just unchecked checkbox with "Disable cache" and now after second loading it works fine. In other words I enabled browser cahce.

    enter image description here

    Also, my browser has network restrictions (3G on screen). So if you change this setting to 'No restrictions' (or smth), it will be fine.

    enter image description here