Skip to content

Instantly share code, notes, and snippets.

@CaptainJojo
Created January 16, 2018 14:13
Show Gist options
  • Save CaptainJojo/80b684aebd550a3a491e640a6618a204 to your computer and use it in GitHub Desktop.
Save CaptainJojo/80b684aebd550a3a491e640a6618a204 to your computer and use it in GitHub Desktop.
framework:
#esi: ~
#translator: { fallbacks: ['%locale%'] }
secret: '%secret%'
router:
resource: '%kernel.project_dir%/app/config/routing.yml'
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: '%locale%'
trusted_hosts: ~
session:
# https://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
fragments: ~
http_method_override: true
assets: ~
php_errors:
log: true
assets:
packages:
js:
base_urls: 'http://infinite.dev/scripts/js'
version_strategy: 'app.assets.js.version_strategy'
{% extends 'base.html.twig' %}
{% block body %}
<div id="react-latest-news-home" >
<div>
{% endblock %}
{% block javascripts %}
{% endblock %}
{% block stylesheets %}
{% endblock %}
{% block javascripts %}
{% set initial_state = {latestNews: {}} %}
<script>window.__INITIAL_STATE__ = {{ initial_state|json_encode|raw }};</script>
<script src="{{ asset('inlined.js', 'js') }}" defer></script>
<script src="{{ asset('vendor.js', 'js') }}" defer></script>
<script src="{{ asset('home.js', 'js') }}" defer></script>
{% endblock %}
<script src="http://infinite.dev/scripts/js/inlined.2d4b19e4578c3af04a03.js" defer></script>
<script src="http://infinite.dev/scripts/js/vendor.2d4b19e4578c3af04a03.js" defer></script>
<script src="http://infinite.dev/scripts/js/home.e428223280fc3028d63f.js" defer></script>
<?php
namespace AppBundle\Service\VersionStrategy;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
class JavascriptBusterVersionStrategy implements VersionStrategyInterface
{
/**
* The manifest object containing for each entry
* the filename containing the version.
*
* @var array
*/
private $hashes = [];
/**
* The manifest file path.
*
* @var string
*/
private $path;
/**
* @param string $path The manifest file path
*/
public function __construct(string $path)
{
$this->path = $path;
}
/**
* {@inheritdoc}
*/
public function getVersion($asset): string
{
$this->ensureHashesLoaded();
preg_match(
// Matches pattern like 'vendor.67e29947398bcbf9b383.js'
'/(?:.*)\.([[:alnum:]]*)\.js/',
$this->hashes[$this->getEntryName($asset)] ?? '',
$matches
);
return $matches[1] ?? '';
}
/**
* {@inheritdoc}
*/
public function applyVersion($asset): string
{
$this->ensureHashesLoaded();
return $this->hashes[$this->getEntryName($asset)] ?? '';
}
/**
* Return the manifest entry name for the given asset.
*
* @return string
*/
private function getEntryName($path): string
{
// Replace pattern like 'vendor.js' into 'vendor'
return preg_replace(
'/(.*)\.js/',
'$1',
$path
);
}
/**
* Load hashes from manifest if needed.
*/
private function ensureHashesLoaded()
{
if (empty($this->hashes)) {
$this->hashes = json_decode(file_get_contents($this->path), true);
}
}
}
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import LatestNewsHome from 'containers/LatestNewsHome.jsx';
import configureStore from 'store';
const elements = {
latest_news: document.getElementById('react-latest-news-home'),
};
// eslint-disable-next-line no-underscore-dangle
const store = configureStore(window.__INITIAL_STATE__);
const component = (
<Provider store={store}>
<LatestNewsHome />
</Provider>
);
render(component, elements.latest_news);
services:
app.assets.js.version_strategy:
class: AppBundle\Service\VersionStrategy\JavascriptBusterVersionStrategy
arguments:
- '%kernel.root_dir%/../var/webpack/manifest.json'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment