lhapaipai / vite-bundle

Integration with your Symfony app & Vite

Home Page:https://symfony-vite.pentatrion.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Asset versioning with other bundles not working

crtl opened this issue · comments

commented

When using the ViteAssetVersionStrategy it will resolve the "wrong" host url in dev environments when using other bundles in dev environment.
I was trying to install Nelmio/ApiDocBundle which loads static assets from public/bundles/nelmioapidoc but the ViteAssetVersionStrategy resolves the path to 127.0.1:3000/build/bundles/nelmioapidoc ($origin . $host . $path).

The problem comes from ViteAssetVersionStrategy::getassetsPath#L79-85

if (false !== $this->manifestData) {
            if (isset($this->manifestData[$path])) {
                return $this->build['base'].$this->manifestData[$path]['file'];
            }
        } else {
            return $this->entrypointsData['viteServer']['origin'].$this->entrypointsData['viteServer']['base'].$path;
        }

As a workaround I had to implement my own version strategy using the following class:

<?php

namespace App\Asset;

use Pentatrion\ViteBundle\Asset\ViteAssetVersionStrategy;

class CustomAssetVersionStrategy extends ViteAssetVersionStrategy
{

    public function getVersion(string $path): string
    {
        if (str_starts_with($path, "bundles")) return $path;

        return parent::getVersion($path);
    }

    public function applyVersion(string $path): string
    {
        if (str_starts_with($path, "bundles")) return $path;
        return parent::applyVersion($path);
    }

}

Hi @crtl,
ViteAssetVersionStrategy is directly associated to files built with vite, so you can't use ViteAssetVersionStrategy for another bundle assets resolution.

As a workaround I think you can configure multiple assets resolution strategies in your config/packages/framework.yaml

framework:
    assets:
        packages:
            vite:
                version_strategy: 'Pentatrion\ViteBundle\Asset\ViteAssetVersionStrategy'
            nelmio:
                version_strategy: null
<body>
    <img src="{{ asset('assets/images/avatar.jpg', 'vite') }}" />

    <img src="{{ asset('path-to-your-nelmio-api-asset/file.jpg', 'nelmio') }}" />

</body>

Symfony documentation

commented

@lhapaipai Thank you very much.
I have moved vite strategy into packages.vite because otherwise I would have to override the nelmio bundle resources.
Now all assets use the default (no) version strategy and to include vite assets I call asset("assets/images/avatar.jpg", "vite")

# packages/assets.yaml
framework:
    assets:
        packages:
            vite:
                version_strategy: 'Pentatrion\ViteBundle\Asset\ViteAssetVersionStrategy'