Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: relative asset paths #482

Closed

Conversation

jackd248
Copy link

@jackd248 jackd248 commented Feb 13, 2024

Subject

We're using symfony instances on our staging system in subdirectories. Unfortunately we ran into the problem, that the public assets of the sonataform bundle points to absolute paths.

I think the paths need an relative "./" to be more flexible. See entrypoints.json and manifest.json.

I'm not so familiar with the webpack config, but I think changing the public path may deals with this fact, see webpack.config.js.

Closes #480 .

Changelog

### Changed
- Added relative dot to public path within webpack config.

@VincentLanglet
Copy link
Member

Hi @jackd248

when looking at the symfony documentation
https://symfony.com/doc/current/frontend/encore/installation.html
the call is made with a /, see

.setPublicPath('/build')

so our usage seem similar to what the documentation ask, no ?

@jordisala1991 are you familiar with such change ?

@Hanmac
Copy link
Contributor

Hanmac commented Apr 25, 2024

@VincentLanglet it depends on how the Web-Server is configured.

If the Project is the Web-Root or maybe the public folder.
Or in the case if you are a Developer, you might have the parent folder as root.

If webpack force to hard-code this, then it is bad design in my opinion.

@Hanmac
Copy link
Contributor

Hanmac commented Apr 26, 2024

@jackd248

Your change doesn't work for me either

Before:
It tries to access https://localhost.local/bundles/sonataform/379.js
but it should try to access https://localhost.local/<project>/public/bundles/sonataform/379.js

Now it tries to access https://localhost.local/<project>/public/<route>/bundles/sonataform/379.js

@jackd248
Copy link
Author

You're right, I wasn't able to fix this problem so far.

Do you have an idea how to solve this kind of issue?

@Hanmac
Copy link
Contributor

Hanmac commented Apr 26, 2024

My current hotfix:

using your Branch, and then applied my own Redirect Controller

class BundleFixController extends AbstractController
{
    #[Route('/admin/{something}/bundles/{path}', requirements: ['something' => '.+', 'path' => '.+'], methods: ['GET'])]
    public function bundleAction(RouterInterface $router, string $path): Response
    {
        $baseUrl = $router->getContext()->getBaseUrl();
        return $this->redirect($baseUrl . "/bundles/" . $path);
    }
}

This redirects https://localhost.local/<project>/public/admin/<route>/bundles/<path>
to https://localhost.local/<project>/public/bundles/<path>

For Future, I would like to see this webpack getting replaced with AssetMapper in the Future when Symfony 5.4 gets dropped

@VincentLanglet
Copy link
Member

So does this PR solve something and should be merged or not ?

BTW, it need to be rebased.

@Hanmac
Copy link
Contributor

Hanmac commented Apr 26, 2024

So does this PR solve something and should be merged or not ?

BTW, it need to be rebased.

No, right now it just shifts the problem from one wrong path (outside the project), to another (inside the project)
The only good thing, now that the wrong path is inside the project, you might capture it with a Redirect Controller

For the Base Path Problem, I found this:
https://stackoverflow.com/questions/51613055/encore-webpack-or-assets-generating-wrong-path-to-image-in-symfony-4

WARNING The value passed to setPublicPath() should usually start with "/" or be a full URL (http://...). If you're not sure, then you should probably change your public path and make this message disappear..

@VincentLanglet when is Sonata going to drop Symfony 5.4 Support?
The AssetMapper might look more interesting than webpack

@VincentLanglet
Copy link
Member

@VincentLanglet when is Sonata going to drop Symfony 5.4 Support? The AssetMapper might look more interesting than webpack

We're following https://symfony.com/releases#maintained-symfony-branches
so it will be end of the year.

But you can start a PR with AssetMapper if you want.

@Hanmac
Copy link
Contributor

Hanmac commented Apr 26, 2024

@VincentLanglet when is Sonata going to drop Symfony 5.4 Support? The AssetMapper might look more interesting than webpack

We're following https://symfony.com/releases#maintained-symfony-branches so it will be end of the year.

But you can start a PR with AssetMapper if you want.

I don't have exp on using asset mapper yet
especially on how to use it for a bundle like this instead of the main project

@jackd248
Copy link
Author

Right, this PR is currently not the right solution. I close them for now.

@jackd248 jackd248 closed this Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Relative Asset Paths
3 participants