Symfony, webpack, on click event executed double times only in prod enviroment

I use symfony/framework-bundle 5.0.5 and "symfony/webpack-encore-bundle": "dev-master"
When I develop code in dev env was everything correct, but when switched project to prod enviroment I faced with strange behaviour, my on lick event executed two times, maybe some double including js script I don’t understand, when I returned to dev env problem was gone. How to correct use web pack in symfony framework in prod env ?

webpack.config.js

var 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 sub-directory deploy
    //.setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     *
     * 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/js/app.js')
    .addEntry('index/js/index.min', './assets/js/index/index.js')
    .addEntry('index/js/awin.min', './assets/js/index/awin.js')
    .addEntry('index/js/adtraction.min', './assets/js/index/adtraction.js')
    .addEntry('index/js/adrecord.min', './assets/js/index/adrecord.js')
    .addEntry('index/js/tradeDoubler.min', './assets/js/index/tradeDoubler.js')
    .addEntry('index/js/hover_menu.min', './assets/js/index/hover_menu.js')
    .addEntry('index/js/brand_list.min', './assets/js/index/brand_list.js')
    .addEntry('index/js/admin_shop_rule_list.min', './assets/js/index/admin_shop_rule_list.js')
    .addEntry('index/js/resource_shop_list.min', './assets/js/index/resource_shop_list.js')
    .addEntry('index/js/statisticsMonitoring.min', './assets/js/index/statisticsMonitoring.js')

    //.addEntry('page1', './assets/js/page1.js')
    //.addEntry('page2', './assets/js/page2.js')

    // 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())

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

    // enables Sass/SCSS support
    .enableSassLoader()
    .addStyleEntry('index/css/index.min', './assets/css/app.scss')

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

    // 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
    .autoProvidejQuery()

    // uncomment if you use API Platform Admin (composer req api-admin)
    //.enableReactPreset()
    //.addEntry('admin', './assets/js/admin.js')
;

module.exports = Encore.getWebpackConfig();

config/packages/prod/webpack_encore.yaml

webpack_encore:
#     Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
#     Available in version 1.2
    cache: true

config/packages/webpack_encore.yaml

webpack_encore:
    # The path where Encore is building the assets - i.e. Encore.setOutputPath()
    output_path: '%kernel.project_dir%/public/build'

my base twig template has script only for fosjsrouting

{% block javascripts %}

    <script src="{{ asset('bundles/fosjsrouting/js/router.min.js') }}"></script>
    <script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>

{% endblock %}

and my child template whe I faced with double click

{% extends 'base.html.twig' %}
//...///
{% block javascripts %}
    {{ parent() }}
    {{ encore_entry_script_tags('index/js/resource_shop_list.min') }}

    <script>
        function getJSonObject(value) {
            return $.parseJSON(value.replace(/&quot;/ig, '"'));
        }

        let th_keys = getJSonObject("{{ dataTbaleKeys ? dataTbaleKeys|json_encode() : "{}" }}");
        let for_prepare_defs = getJSonObject("{{ dataTbaleKeys ? dataTbaleKeys|json_encode() : "{}" }}");

    </script>

{% endblock %}

and my assets/js/index/resource_shop_list.js

/*
 * Welcome to your app's main JavaScript file!
 *
 * We recommend including the built version of this JavaScript file
 * (and its CSS file) in your base layout (base.html.twig).
 */

// any CSS you import will output into a single css file (app.css in this case)
import '../../css/app.scss';
import * as dt_bs4 from 'datatables.net-bs4'
import * as fh_bs from 'datatables.net-fixedheader-bs4';
import * as r_bs from 'datatables.net-responsive-bs4';

require('@fortawesome/fontawesome-free/css/all.min.css');
require('datatables.net-dt/css/jquery.dataTables.min.css');
require('datatables.net-fixedheader-bs4/css/fixedHeader.bootstrap4.min.css');
require('datatables.net-responsive-bs4/css/responsive.bootstrap4.min.css');

require('@fortawesome/fontawesome-free/js/all.js');

const $ = require('jquery');
global.$ = global.jQuery = $;
// import 'popper.js';
require('bootstrap');


document.addEventListener("DOMContentLoaded", function () {
    console.log("resource shop list!");
    const body = $('body');
    const app_rest_admin_resourceshops_shopreloading = window.Routing.generate('app_rest_admin_resourceshops_shopreloading');
    var table;
    body.on('click', '.resource_reloading button', function () {
        let current = $(this);
        console.log(current);

        let shopName = current.data('shopName');

        reloadingShop(shopName);
    });

    // ...//

I double checked, I crete event only one time and in dev env it’s worked correct.
For prod env in server I executed

php bin/console c:c && yarn encore production && chmod 777 -R
 var/cache/ var/log/

with result

Done in 13.51s.

How to resolv it ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *