hearsayit / HearsayRequireJSBundle

RequireJS integration for Symfony2.
130 stars 55 forks source link

baseUrl is not absolute #1

Closed ismell closed 12 years ago

ismell commented 12 years ago

My template has the following

{% block javascripts %}
    {{ parent() }}
    <script type='text/javascript'>require(['wedcore/edit'], function() {})</script>
{% endblock %}

My Config looks like so


hearsay_require_js:
    base_directory: %kernel.root_dir%/scripts
    paths: 
        jquery:
            location: //ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min
            external: true
        wedcore: '@WedCoreBundle/Resources/scripts'
    optimizer:
        path: /path/to/r.js
        excludes: [ excluded/module ] # Modules to exclude from the build (optional)
        options: { skipModuleInsertion: true } # Additional options to pass to the optimizer (optional)

When I hit the following url: http://rangelwed/app_dev.php/reception/edit

My require block generates the following

<script type='text/javascript'>var require={"baseUrl":"js","locale":null,"paths":{"jquery":"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min"}}</script><script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/require.js/1.0.1/require.min.js'></script>

<script type='text/javascript'>require(['wedcore/edit'], function() {})</script>

if you notice the baseUrl it's relative. This causes the request to wedcore/edit to go to http://rangelwed/app_dev.php/reception/js/wedcore/edit.js. This is incorrect and instead should go to http://rangelwed/app_dev.php/js/wedcore/edit.js.

So the base URL should be rendered as /app_dev.php/js/.

Thanks, Raul

bigethan commented 12 years ago

in your config.yml you can specify

hearsay_require_js:
    ...
    base_url: /js 

in your main config, and then in your config_dev.yml do:

hearsay_require_js:
    base_url: /app_dev.php/js

and that should get you want you want.

kmontag commented 12 years ago

Fixed; note that @bigethan 's workaround breaks as a result, though. The base_url specified in the config will now be automatically appended to the request root, regardless of whether it contains a leading slash.

ismell commented 12 years ago

Awesome thanks!

AsaAyers commented 12 years ago

Your commit works as long as your .htaccess is working to hide the /app.php part of the URL. Maybe it should check if kernel.debug is set? Or maybe see how assetic figures out the URL for production?

kmontag commented 12 years ago

Base URL is now chosen based on the value of %assetic.use_controller%. If a controller is being used for assets, the root path (e.g. app.php or app_dev.php) will be included in the base URL (unless it's being hidden by .htaccess). Otherwise, the base URL is just the one specified in your config.

This feels a bit tightly tied to the Assetic Bundle implementation; if anyone has thoughts on a better way to do it, suggestions or pull requests are appreciated :)