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

Added a new asset versioning strategy that reads a manifest JSON file #7659

Closed
javiereguiluz opened this issue Mar 20, 2017 · 6 comments
Closed
Assignees
Labels
Asset hasPR A Pull Request has already been submitted for this issue.
Milestone

Comments

@javiereguiluz
Copy link
Member

javiereguiluz commented Mar 20, 2017

See symfony/symfony#22046 and https://symfony.com/blog/new-in-symfony-3-3-manifest-based-asset-versioning

fabpot added a commit to symfony/symfony that referenced this issue Mar 25, 2017
…a manifest JSON file (weaverryan)

This PR was squashed before being merged into the 3.3-dev branch (closes #22046).

Discussion
----------

[Asset] Adding a new version strategy that reads from a manifest JSON file

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | symfony/symfony-docs#7659

Hi guys!

Often, when using a frontend task manager or bundler (e.g. webpack of gulp), the final assets are dumped with a version or content hash in the filename itself (e.g. main.123abc.css). To know what the correct, current hashed filename is, you'll dump a `manifest.json` file - e.g.

```json
{
  "main.js": "main.123abc.js",
  "css/styles.css": "css/styles.555def.css"
}
```

Examples: [gulp-rev](https://github.com/sindresorhus/gulp-rev) and [webpack-manifest-plugin](https://www.npmjs.com/package/webpack-manifest-plugin).

This PR adds a new version strategy that will look up the asset path (e.g. `main.css`) in that file and return the final, versioned path. Some people may dump manifest files in other formats, but I think this catches the most common use-case (and you can always still create your own version strategy). I've written this to be "forgiving" - if a path doesn't exist in the manifest, the path is simply returned, unaltered.

Another implementation *could* have been to add a new Twig filter (e.g. `{{ asset('main.css|manifest_path) }}`) - but I thought I'd try first using the existing versioning system.

## Usage

```yml
# app/config/config.yml
framework:
    # ...
    assets:
       # added validation prevents you from setting json_manifest_path AND version, for example
        json_manifest_path: '%kernel.root_dir%/../web/manifest.json'
```

```twig
{# someTemplate.html.twig #}

{# use asset() just like normal #}
<script src="{{ asset('js/main.js') }}"></script>
```

## TODO
* fabbot hates my invalid json syntax file... even though I tried to be clever and not give it a `.json` suffix :)

Commits
-------

07fec2b [Asset] Adding a new version strategy that reads from a manifest JSON file
@javiereguiluz javiereguiluz changed the title [WCM] Added a new asset versioning strategy that reads a manifest JSON file Added a new asset versioning strategy that reads a manifest JSON file Mar 26, 2017
@HeahDude HeahDude added this to the 3.3 milestone Mar 28, 2017
@ghost
Copy link

ghost commented Mar 30, 2017

I am picking up this documentation piece.

@javiereguiluz
Copy link
Member Author

@Ricknox my guess is that we need to:

@javiereguiluz
Copy link
Member Author

@ricknox just asking: will you have time to contribute this doc before the end of next week? I'd like to have this merged soon because it's very important for the new JavaScript tool that we're going to introduce to manage assets in Symfony apps. If you don't have time, please tell me and I'll write this doc. Thanks!

@ghost
Copy link

ghost commented Jun 4, 2017

@javiereguiluz I totally forgot about this piece of documentation. Cannot pick this up at the moment, I am sorry. Will pick up another soon! Thanks.

@javiereguiluz javiereguiluz self-assigned this Jun 4, 2017
@robfrawley
Copy link
Contributor

robfrawley commented Jun 4, 2017

@javiereguiluz If you're tight on time with all the other documentation you are writing/reviewing, I'm happy to pick this up and can have something for tomorrow or the next few days.

@javiereguiluz
Copy link
Member Author

Thanks a lot! I'll wait then for your PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Asset hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

4 participants