clubstudio / craft-asset-rev

A Craft CMS plugin to help with cache busting
MIT License
109 stars 7 forks source link

Query String Strategy Ignores AssetsBasePath Variable #21

Closed kmgdevelopment closed 5 years ago

kmgdevelopment commented 5 years ago

I'm trying to use the query string strategy but the plugin appears to be ignoring the assetsBasePath config variable when looking for files.

My config:

'assetsBasePath' => '../content/assets/',
'assetUrlPrefix' => '{baseUrl}/assets/',

My template tag:

<link rel="stylesheet" href="{{ rev('css/main.css') }}">

I get this message in the log:

Cannot append query string - the file css/main.css does not exist.

The plugin should be looking for the file in ../content/assets/css/main.css but is ignoring the assetsBasePath variable.

If I do this in the template the query string is appended:

{{ rev('../content/assets/css/main.css') }}

But the output path is wrong:

<link rel="stylesheet" href="../content/assets/css/main.css?1546728657">
scottwakefield commented 5 years ago

Hi @kgrote,

The assetBasePath is relative to Craft base directory (or project root), so changing ../content/assets/ to content/assets/ should do the trick, assuming that your content directory is in the same directory as your vendor folder.

Can you give that a try and let me know how you get on?

kmgdevelopment commented 5 years ago

Hi Scott,

My folder structure is:

craft/
content/
--assets/
----css/
------main.css

So I'm pretty sure I've got it right already.

scottwakefield commented 5 years ago

Hi Kristen,

Ah, are you using Craft 2? I had recreated your setup on a fresh Craft 3 install.

Let me know and I'll take a look into it for you.

Cheers! 👍

kmgdevelopment commented 5 years ago

Nope, Craft 3.0.36 / Asset Rev 6.0.2

kmgdevelopment commented 5 years ago

So were you able to recreate the problem?

scottwakefield commented 5 years ago

Hi Kristen,

Unfortunately not. I've installed fresh copies of Craft 3.0.36 and Asset Rev 6.0.2 and changed the default directory structure to:

screenshot 2019-01-09 at 21 34 56

I added

    'assetsBasePath' => '../content/assets',
    'assetUrlPrefix' => '{baseUrl}/assets/',

to craft/config/assetrev.php.

Calling {{ rev('css/main.css') }} in a template appends the query string as you would expect.

Does that configuration and setup match how you have set up your project?

kmgdevelopment commented 5 years ago

OK I was editing the config.php file in the plugin's vendor folder rather than creating an assetrev.php file in craft's config folder. So that fixed the issue with the paths, but I'm still having a problem where my config variables aren't parsing.

So in my main craft/config/general.php I've got:

return [
    * => [
    // global config settings
    ],
    'local' => [
    'siteUrl' => '//mysite.local'
    ],
];

And my craft/config/assetrev.php:

<?php
return array(
    '*' => array(
        'strategies' => [
            'manifest' => \club\assetrev\utilities\strategies\ManifestFileStrategy::class,
            'querystring' => \club\assetrev\utilities\strategies\QueryStringStrategy::class,
            'passthrough' => function ($filename, $config) {
                return $filename;
            },
        ],
        'pipeline' => 'querystring|passthrough',
        'manifestPath' => '',
        'assetsBasePath' => '../content/assets/'
    ),
    'local' => array(
        'assetUrlPrefix' => '{siteUrl}/assets/',
    )
);

But I get this as the output:

<link rel="stylesheet" href="{siteUrl}/assets/css/main.css?1547085278">

kmgdevelopment commented 5 years ago

Got it!

environmentVariables were updated to aliases for Craft 3, and the variable syntax changed from {myVariable} to @myVariable.

So in my general.php config file I need:

return [
    * => [
        // global config settings
    ],
    'local' => [
        'aliases' = array(
            'siteUrl' => '//mysite.local'
        )
    ],
];

And in assetrev.php I need:

<?php
return array(
    '*' => array(
        'strategies' => [
            'manifest' => \club\assetrev\utilities\strategies\ManifestFileStrategy::class,
            'querystring' => \club\assetrev\utilities\strategies\QueryStringStrategy::class,
            'passthrough' => function ($filename, $config) {
                return $filename;
            },
        ],
        'pipeline' => 'querystring|passthrough',
        'manifestPath' => '',
        'assetsBasePath' => '../content/assets/'
    ),
    'local' => array(
        'assetUrlPrefix' => '@siteUrl/assets/',
    )
);

So it was just a documentation issue. Swap out "Environment Variables" for "Aliases" and "{baseUrl}" for "@baseUrl" and you should be good.

scottwakefield commented 5 years ago

Aha! Sorry about the outdated readme. I'll be sure to make sure it is kept up to date in future 👍

Pleased to hear that you got everything sorted out!