diff --git a/core/core.services.yml b/core/core.services.yml
index 5ba0116..474f0c8 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1434,7 +1434,7 @@ services:
     class: Drupal\Core\Extension\InfoParser
   twig:
     class: Drupal\Core\Template\TwigEnvironment
-    arguments: ['@app.root', '@cache.default', '@twig.loader', '%twig.config%']
+    arguments: ['@app.root', '@cache.default', '%twig_extension_hash%', '@twig.loader', '%twig.config%']
     tags:
       - { name: service_collector, tag: 'twig.extension', call: addExtension }
   twig.extension:
diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php
index 7e02c80..7b83fc2 100644
--- a/core/lib/Drupal/Core/CoreServiceProvider.php
+++ b/core/lib/Drupal/Core/CoreServiceProvider.php
@@ -19,6 +19,7 @@
 use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
 use Drupal\Core\DependencyInjection\Compiler\StackedSessionHandlerPass;
 use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
+use Drupal\Core\DependencyInjection\Compiler\TwigExtensionPass;
 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
 use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
@@ -78,6 +79,8 @@ public function register(ContainerBuilder $container) {
     $container->addCompilerPass(new RegisterStreamWrappersPass());
     $container->addCompilerPass(new GuzzleMiddlewarePass());
 
+    $container->addCompilerPass(new TwigExtensionPass());
+
     // Add a compiler pass for registering event subscribers.
     $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
 
diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/TwigExtensionPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/TwigExtensionPass.php
new file mode 100644
index 0000000..35dd690
--- /dev/null
+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/TwigExtensionPass.php
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\DependencyInjection\Compiler\TwigExtensionPass.
+ */
+
+namespace Drupal\Core\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * Adds twig_extension_hash parameter to the container.
+ *
+ * A hash of all extensions for Twig template invalidation.
+ */
+class TwigExtensionPass implements CompilerPassInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function process(ContainerBuilder $container) {
+    $twig_extension_hash = '';
+    foreach (array_keys($container->findTaggedServiceIds('twig.extension')) as $service_id) {
+      $twig_extension_hash .= $container->getDefinition($service_id)->getClass();
+    }
+
+    $container->setParameter('twig_extension_hash', $twig_extension_hash);
+  }
+
+}
diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php
index dc7de16..b86a40a 100644
--- a/core/lib/Drupal/Core/Template/TwigEnvironment.php
+++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
@@ -11,6 +11,7 @@
 use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\PhpStorage\PhpStorageFactory;
 use Drupal\Core\Render\SafeString;
+use Drupal\Core\Site\Settings;
 
 /**
  * A class that defines a Twig environment for Drupal.
@@ -21,10 +22,29 @@
  * @see core\vendor\twig\twig\lib\Twig\Environment.php
  */
 class TwigEnvironment extends \Twig_Environment {
+
+  /**
+   * The cache object used for auto-refresh via mtime.
+   *
+   * @var \Drupal\Core\Cache\CacheBackendInterface
+   */
   protected $cache_object = NULL;
+
+  /**
+   * The PhpStorage object used for storing the templates.
+   *
+   * @var \Drupal\Core\PhpStorage\PhpStorageFactory
+   */
   protected $storage = NULL;
 
   /**
+   * The template cache filename prefix.
+   *
+   * @var string
+   */
+  protected $templateCacheFilenamePrefix;
+
+  /**
    * Static cache of template classes.
    *
    * @var array
@@ -32,6 +52,13 @@ class TwigEnvironment extends \Twig_Environment {
   protected $templateClasses;
 
   /**
+   * The Twig extension hash, which is used to invalidate Twig templates.
+   *
+   * @var string
+   */
+  protected $twigExtensionHash;
+
+  /**
    * Constructs a TwigEnvironment object and stores cache and storage
    * internally.
    *
@@ -39,13 +66,16 @@ class TwigEnvironment extends \Twig_Environment {
    *   The app root.
    * @param \Drupal\Core\Cache\CacheBackendInterface $cache
    *   The cache bin.
+   * @param string $twig_extension_hash
+   *   The Twig extension hash.
    * @param \Twig_LoaderInterface $loader
    *   The Twig loader or loader chain.
    * @param array $options
    *   The options for the Twig environment.
    */
-  public function __construct($root, CacheBackendInterface $cache, \Twig_LoaderInterface $loader = NULL, $options = array()) {
+  public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, \Twig_LoaderInterface $loader = NULL, $options = array()) {
     $this->cache_object = $cache;
+    $this->twigExtensionHash = $twig_extension_hash;
 
     // Ensure that twig.engine is loaded, given that it is needed to render a
     // template because functions like TwigExtension::escapeFilter() are called.
@@ -89,6 +119,30 @@ public function updateCompiledTemplate($cache_filename, $name) {
   }
 
   /**
+   * {@inheritdoc}
+   */
+  public function getCacheFilename($name) {
+    // We override the cache filename in order to avoid issues with not using
+    // shared filesystems. The Twig templates for example rely on available Twig
+    // extensions, so we need to take into account the \Drupal::VERSION
+    // deployment identifier, much like for the container itself.
+
+    if (!$this->cache) {
+      return FALSE;
+    }
+
+    $class = substr($this->getTemplateClass($name), strlen($this->templateClassPrefix));
+
+    // Just check the list of loaded extensions once.
+    if (!isset($this->templateCacheFilenamePrefix)) {
+      $this->templateCacheFilenamePrefix = hash('crc32b', $this->twigExtensionHash . \Drupal::VERSION . Settings::get('deployment_identifier', \Drupal::VERSION));
+    }
+
+    // The first part is what is invalidated.
+    return $this->templateCacheFilenamePrefix . '_' . basename($name) . '_' . $class;
+  }
+
+  /**
    * Implements Twig_Environment::loadTemplate().
    *
    * We need to overwrite this function to integrate with drupal_php_storage().
