clubstudio / craft-asset-rev

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

rev() can't seem to find my manifest file #26

Closed alexandersix closed 5 months ago

alexandersix commented 4 years ago

I've been troubleshooting this one for a few hours now, and I'm at the point where I just need someone to take a look at this and make sure I'm doing what I think the README is telling me to do.

I'm running a Craft CMS 3 project, and my directory structure looks like this (it's the default structure):

project/
|--web
     |--resources
          |--app.<cachebusting-string>.css
          |--app.<cachebusting-string>.js
     |--mix-manifest.json
|--vendor
|--(etc.)

My mix-manifest.json file looks like this:

{
    "/resources/app.js": "/resources/app.<cachebusting-string>.js",
    "/resources/app.css": "/resources/app.<cachebusting-string>.css"
}

And my config/assetrev.php file looks like this:

 <?php
 return [
     '*' => [
         'strategies' => [
             'manifest' => \club\assetrev\utilities\strategies\ManifestFileStrategy::class,
             'querystring' => \club\assetrev\utilities\strategies\QueryStringStrategy::class,
             'passthrough' => function ($filename, $config) {
                 return $filename;
             },
         ],
         'pipeline' => 'manifest|querystring|passthrough',
         'manifestPath' => '/mix-manifest.json',
         'assetsBasePath' => 'resources',
         'assetUrlPrefix' => ''
     ],
     'dev' => [
         'pipeline' => 'passthrough'
     ]
 ];

And just in case there's something I'm missing here, this is what my rev() functions look like in my index.twig file:

<link rel="stylesheet" href="{{ rev('/resources/app.css') }}" media="screen">
<script src="{{ rev('/resources/app.js') }}"></script>

I'm absolutely at a loss. I think my eyes are just crossed from staring at the same piece of code for 4 hours straight, so if anyone has some insight as to where I have misunderstood what's supposed to be happening here, that would be great.

scottwakefield commented 4 years ago

@AlexanderSix, try setting manifestPath to web/mix-manifest.json and assetBasePath to web. If you start either of the paths with a / it will treat the path as being absolute on your filesystem, rather than relative to the Craft installation.

You can also try using the @webroot alias, too: 'manifestPath' => '@webroot/mix-manifest.json', and 'assetsBasePath' => '@webroot',

If that doesn't work, I'll set up a Craft installation locally and mimic your setup so I can help to debug further.

Let me know how you get on! 👍

alexandersix commented 4 years ago

@scottwakefield Hmm, I tried both of those setups, but I'm not getting anything. Thanks for the suggestions, though!

Is there anything else you need from me in order to mimic my setup efficiently? I'm super new to Craft CMS, so I'm still getting a grip on what's absolutely essential to know.

littlered commented 4 years ago

I was having same problem - Looking through closed tickets on the same error, check carefully the paths in your manifest, assetrev.php and twig templates.

My path structure is similar, but I have directories for css + js under my dist folder:

project/
|--web
    |--dist
        |--css
            |--app.<cachebusting-string>.css
        |--js
            |--app.<cachebusting-string>.js
        |--manifest.json

manifest.json contains paths like this (I'm using webpack):

{
  "app.css": "css/app.<cache-bursting-string>.css",
  "app.bundle.js": "js/app.bundle.<cache-bursting-string>.js"
}

assetrev:

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

Then in the twig template, I rev:

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

So for your setup, I would change manifestPath to 'web/resources/mix-manifest.json' and assetsBasePath to 'web/resources/' - doesn't look like you need assetUrlPrefix because your manifest has that path in it (my webpack doesn't output the /dist in the path).

alexandersix commented 4 years ago

@littlered Thanks for the tip; I set up my config file as you recommended (except instead of making the manifestPath "web/resources/mix-manifest.json", I used "web/mix-manifest.json" since my manifest file is on the same directory level as resources, not inside it), but I'm still getting no luck.

It seems like every combination that I try, the plugin is failing the other strategies for replacing the filename and going right to passthrough. I've changed the string that gets returned from the passthrough function and that will change what's output to the file every time, so I'm 100% certain that I'm passing through.

Is there another reason why the manifest pipeline strategy could be failing? I'm fairly certain that I have the correct filepaths in my assetrev.php file.

littlered commented 4 years ago

@AlexanderSix - I find this all very touchy, but once it's right, it works. Try your assetsBasePath to be '../web/resources/'

alexandersix commented 4 years ago

@littlered No luck with that either, so I'm starting to think that something else could be wrong.

Do I need to restart any services on my server or delete any cached files when I make a change to the assetrev config file? So far I've just been changing the file and re-running my webpack script (though I don't think that makes a difference--I'm just trying to knock out any potential problems as I go)

scottwakefield commented 4 years ago

Hi @AlexanderSix,

I've just recreated your Craft setup locally and the following configuration works for me:

<?php

return [

    '*' => [
         'strategies' => [
             'manifest' => \club\assetrev\utilities\strategies\ManifestFileStrategy::class,
             'querystring' => \club\assetrev\utilities\strategies\QueryStringStrategy::class,
             'passthrough' => function ($filename, $config) {
                 return $filename . '?testing-passthrough';
             },
         ],
         'pipeline' => 'manifest|querystring|passthrough',
         'manifestPath' => 'web/mix-manifest.json',
         'assetsBasePath' => 'web',
         'assetUrlPrefix' => ''
     ],

];

You shouldn't need to clear any caches, but it can't hurt to clear all caches via the Craft CP while you're debugging.

I've added ?testing-passthrough to the passthrough function to ensure that it was loading the configuration correctly. If that configuration doesn't work with the manifest file, does it at least append ?testing-passthrough to the URL as a fallback?

alexandersix commented 4 years ago

@scottwakefield I adjusted my configuration file to match yours (query string included), and I couldn't get it to work.

The query string is being appended, though, so the passthrough strategy is being applied. Since you got it to work, I'm sure there's something wrong with my setup (maybe another plugin or something?). I'm just not sure how to debug further what's going on to make the manifest strategy fail.

besimhu commented 4 years ago

@AlexanderSix what Craft 3 version are you on? Today I noticed I was having issues busting cache through query string. It used to work for me just fine in the past, but today I could not figure why it was causing issues. No matter what I did, it defaulted to the same query parameter.

I haven't had time to debug, but I will be looking at trying a manifest approach.

alexandersix commented 4 years ago

@besimhu I have been using the newest versions as they have been coming out. At this point, I’ve just disabled this code and will re-implement it when I can find out what’s going wrong.

itsmattsoria commented 4 years ago

I have been having the same issue! I'm running Craft 3.4.9 and Asset Rev 6.0.2.

Similar to @AlexanderSix I'm using laravel-mix to produce the manifest file (which in my case is also called mix-manifest.json). I tried running through all of the suggestions @scottwakefield made but I'm still left with what seems to be the issue of the manifest file not being found. Here's my structure:

project/
|--web
     |--assets
          |--dist
               |--styles
                    |--main.css
               |--scripts
                    |--main.js
               |--mix-manifest.json
|--vendor
|--(etc.)

And here is what I currently have set for my assetrev.php file:

<?php

return [

    '*' => [
         'strategies' => [
             'manifest' => \club\assetrev\utilities\strategies\ManifestFileStrategy::class,
             'querystring' => \club\assetrev\utilities\strategies\QueryStringStrategy::class,
             'passthrough' => function ($filename, $config) {
                 return $filename . '?testing-passthrough';
             },
         ],
         'pipeline' => 'manifest|querystring|passthrough',
         'manifestPath' => 'web/assets/dist/mix-manifest.json',
         'assetsBasePath' => 'web/assets/dist/',
         'assetUrlPrefix' => '/assets/dist/'
     ],

     'dev' => [
        // 'pipeline' => 'passthrough',
        // 'manifestPath' => null,
     ]

];

The querystring strategy is working correctly — I'm getting output like <script src="/assets/dist/scripts/main.js?1583362326"></script>, but if I remove |querystring|passthrough from the pipeline then I end up with the error message:

ErrorException
None of the configured strategies `manifest` returned a value.

Also for reference, my mix-manifest.json file looks like this:

{
    "/scripts/main.js": "/scripts/main.js?id=3ecaec70ee2d13de4caf",
    "/styles/main.css": "/styles/main.css?id=3763c9adf7d84e5ba5b9",
    "/scripts/manifest.js": "/scripts/manifest.js?id=3c768977c2574a34506e",
    "/scripts/vendor.js": "/scripts/vendor.js?id=4ee3b6989e3a715972df",
    "/spritemap.js": "/spritemap.js?id=26150e97ab0023e3c399",
    "/svgs-defs.svg": "/svgs-defs.svg?id=4badb287a627fb8a3d67",
    "/images/favicon.png": "/images/favicon.png?id=6002f49b5c5b1e62a97a"
}

I cannot figure out what I'm missing. @AlexanderSix — did you ever end up figuring it out for your setup?

itsmattsoria commented 4 years ago

@AlexanderSix + @scottwakefield — almost as soon as I posted that I resolved the issue, and it ended up actually being related to #25 — I removed the trailing slash from assetUrlPrefix, and added a leading slash to the rev() calls and that did the trick for me. So in the end the change was going from:

assetUrlPrefix => '/assets/dist/' + <link rel="stylesheet" href="{{ rev('styles/main.css') }}"> to assetUrlPrefix => '/assets/dist' + <link rel="stylesheet" href="{{ rev('/styles/main.css') }}">

Though it doesn't seem like @AlexanderSix was having that same issue.

besimhu commented 4 years ago

@poopsplat out of curiosity, why not use the larvel mix plugin that reads the mix-manifest files? I did so recently on a project where I used Larvel Mix.

itsmattsoria commented 4 years ago

@besimhu I wasn't aware of it, otherwise maybe I would, or still will. What plugin is it?

besimhu commented 4 years ago

@poopsplat I am using it on the latest 3.4.9 version and it works fine

https://github.com/mister-bk/craft-plugin-mix/blob/master/README.md

itsmattsoria commented 4 years ago

@besimhu oh awesome, thanks!