Sage 10 - Problem with BrowserSync Performance

Hi. I’ve been testing Sage 10 for several months now and I have one problem all the time. When I have some (15) images in resources/images browsersync is very slow. I often have to manually refresh the page to see the changes. I’m doing build:production after each added file, but that doesn’t help. When I move the images to a different location, browsersync works fine again. The problem appears on Windows and Ubuntu. Tested with Laragon, Docker and Valet.
I’m currently using tailwind 2.2.4 in jit mode.

webpack.mix.js

const mix = require('laravel-mix');
require('@tinypixelco/laravel-mix-wp-blocks');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Sage application. By default, we are compiling the Sass file
 | for your application, as well as bundling up your JS files.
 |
 */

mix
    .setPublicPath('./public')
    .browserSync({
        proxy: 'lightmarlin.test',
        port: 32816,
        injectChanges: true,
        notify: false,
    });

mix
    .sass('resources/styles/app.scss', 'styles')
    .sass('resources/styles/editor.scss', 'styles')
    .sass('resources/styles/admin.scss', 'styles')
    .options({
        processCssUrls: false,
        postCss: [require('tailwindcss')],
    });

mix
    .js('resources/scripts/app.js', 'scripts')
    .js('resources/scripts/customizer.js', 'scripts')
    .blocks('resources/scripts/editor.js', 'scripts')
    // .autoload({ jquery: ['$', 'window.jQuery'] })
    .extract();

mix
    .copyDirectory('resources/images', 'public/images');
    // .copyDirectory('resources/fonts', 'public/fonts');

mix
    // .sourceMaps()
    .version();

I had a similar problem.

My solution was removing images from being moved to ‘public/images’ since I don’t run any optimizations on them.

To achieve that, I added another asset manifest called images and made it the default. That allows the use of @asset to use the resources/images directory instead of public/images.

Links to styles and scripts were broken because of that, so I had to add theme as the second parameter for style and script assets.

If you kept the theme asset manifest as the default, you can use @asset('image.png', 'images').

Here’s an example with the latest sage 10 theme

I wanted to test your solution, but unfortunately it doesn’t work for me. I left theme as the default value in assets.php. Now the image links are broken.

    'manifests' => [
        'theme' => [
            'path' => get_theme_file_path('public'),
            'url' => get_theme_file_uri('public'),
            'assets' => public_path('mix-manifest.json'),
        ],
        'images' => [
            'path' => get_theme_file_path('resources/images'),
            'url' => get_theme_file_uri('resources/images'),
            'assets' => '',
        ]
    ]

<img class="w-auto block h-16 mx-auto mb-4" src="@asset('firefox.png','images')" alt="Mozilla Firefox">

return in html:

<img class="w-auto block h-16 mx-auto mb-4" src="http://domain.test/wp-content/themes/test-theme/public/chrome.png" alt="Google Chrome">

I used build:production, still the same.

I don’t think you have the same version of Sage I added in the example. If you downloaded my example theme, it should work.

Can you paste the contents of config/assets.php before you change it? might require older config options to work properly.

Yes it’s true. I didn’t download the entire theme. I changed my theme according to this commit https://github.com/theMosaad/sage/commit/c953a30a3918ec7888b4ff7dd7057a9d42532f84

before change:

<?php

use function Roots\public_path;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Assets Manifest
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default asset manifest that should be used.
    | The "theme" manifest is recommended as the default as it cedes ultimate
    | authority of your application's assets to the theme.
    |
    */

    'default' => 'theme',

    /*
    |--------------------------------------------------------------------------
    | Assets Manifests
    |--------------------------------------------------------------------------
    |
    | Manifests contain lists of assets that are referenced by static keys that
    | point to dynamic locations, such as a cache-busted location. We currently
    | support two types of manifest:
    |
    | assets: key-value pairs to match assets to their revved counterparts
    |
    | bundles: a series of entrypoints for loading bundles
    |
    */

    'manifests' => [
        'theme' => [
            'path' => get_theme_file_path('public'),
            'url' => get_theme_file_uri('public'),
            'assets' => public_path('mix-manifest.json'),
        ]
    ]
];

Not a Sage version problem. Maybe you missed something in the changes.
Can you share your theme even in a DM to debug it with you?

@theMosaad I’ll try again with a clean theme. My theme has a lot of modifications and I will probably find the reason myself. Your solution is fine for most projects, but I’d like to finally find the cause of my browsersync problems. the worst part is that periodically some themes work fine with pictures. another one worked as I deleted and added images again. It’s hard to find a clue.

@theMosaad i downloaded sage from your link, i dont change antything and its doesnt work.
404 for assets, example url:
http://sage.test/wp-content/themes/sage/resources/images/styles/app.css

I remember not checking the styles. Will fix and let you know.

My approach was working before they updated acorn to 2.0.0-alpha.0

Spent some time exploring the update but wasn’t able to identify the alternative for asset('scripts/vendor.js', 'theme')->uri() still.

@QWp6t any help on how to use another assets manifest?

The config file only provides a place to put config values; they aren’t necessary used anywhere unless they’re…used anywhere.

I haven’t tried this out, but I’d guess that if you want to use an additional manifest you’d need to add the manifest to the manifests tracked by the app. I’d start by mimicking the way it’s done by Sage core in its ServiceProvider: https://github.com/roots/acorn/blob/eb238de53431d300b567a295962170831581b43c/src/Roots/Acorn/Assets/AssetsServiceProvider.php#L22

1 Like

Thanks for putting me on the right path @alwaysblank!

@grzesiek1owline for my initial approach to work with the latest version of Sage, I had to create another blade directive @resource which gets resources from the theme/resources directory instead of the theme/public directory.

An example of usage would be @resource('images/firefox.png')

Here’s the commit for you to replicate

Great, now everything works fine.
I will optimize the images in a different way.
I must live with it :wink:

This topic was automatically closed after 42 days. New replies are no longer allowed.