Skip to content

Commit 2384cfd

Browse files
committed
feature #56 propose fix for CORS issues with missing crossorigin tag (PhilETaylor, weaverryan)
This PR was merged into the master branch. Discussion ---------- propose fix for CORS issues with missing crossorigin tag This fixes #55 and comes with the unit tests too ;-) updated: This PR also allows the value for crossorigin attribute to be defined in the configuration yaml Commits ------- ecad299 Merge branch 'master' into patch-1 04d8091 small docs tweak 95f7d27 making arg optional for BC fee8b91 add default attribute set to false in tests 7eeb941 revert the default test so that it tests with no attribute array a74dee0 tidy up service xml to remove keyed collection fadd432 Apply codestyle from php-cs-fixer fix --config=.php_cs.dist 0b607f7 refactor based on @stof feedback 5eac7b3 correct documentation of configuration 2fb1d3d Fix unit tests 22c971c Allow for no attribute by default, and allow it to be configurable and future proofed array d1866c5 <argument /><!-- crossorigin --> c304692 refine as per feedback 2f83a66 remove additional spaces 4a6325e update documentation 768a820 Allow value for crossorigin attribute to be configurable, defaults to anonymous 9f1e101 fix tests efdc276 fix tests for crossorigin="anonymous" 99e8bce propose fix for CORS issues with missing crossorigin tag
2 parents 6f28251 + ecad299 commit 2384cfd

File tree

7 files changed

+41
-21
lines changed

7 files changed

+41
-21
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ webpack_encore:
2424
output_path: '%kernel.project_dir%/public/build'
2525
# If multiple builds are defined (as shown below), you can disable the default build:
2626
# output_path: false
27-
27+
28+
# if using Encore.enableIntegrityHashes() and need the crossorigin attribute (default: false, or use 'anonymous' or 'use-credentials')
29+
# crossorigin: 'anonymous'
30+
2831
# if you have multiple builds:
2932
# builds:
3033
# pass "frontend" as the 3rg arg to the Twig functions

src/Asset/TagRenderer.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ final class TagRenderer
1818

1919
private $packages;
2020

21+
private $defaultAttributes;
22+
2123
public function __construct(
2224
$entrypointLookupCollection,
23-
Packages $packages
25+
Packages $packages,
26+
array $defaultAttributes = []
2427
) {
2528
if ($entrypointLookupCollection instanceof EntrypointLookupInterface) {
2629
@trigger_error(sprintf('The "$entrypointLookupCollection" argument in method "%s()" must be an instance of EntrypointLookupCollection.', __METHOD__), E_USER_DEPRECATED);
@@ -37,6 +40,7 @@ public function __construct(
3740
}
3841

3942
$this->packages = $packages;
43+
$this->defaultAttributes = $defaultAttributes;
4044
}
4145

4246
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
@@ -46,9 +50,8 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
4650
$integrityHashes = ($entryPointLookup instanceof IntegrityDataProviderInterface) ? $entryPointLookup->getIntegrityData() : [];
4751

4852
foreach ($entryPointLookup->getJavaScriptFiles($entryName) as $filename) {
49-
$attributes = [
50-
'src' => $this->getAssetPath($filename, $packageName),
51-
];
53+
$attributes = $this->defaultAttributes;
54+
$attributes['src'] = $this->getAssetPath($filename, $packageName);
5255

5356
if (isset($integrityHashes[$filename])) {
5457
$attributes['integrity'] = $integrityHashes[$filename];
@@ -70,10 +73,9 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
7073
$integrityHashes = ($entryPointLookup instanceof IntegrityDataProviderInterface) ? $entryPointLookup->getIntegrityData() : [];
7174

7275
foreach ($entryPointLookup->getCssFiles($entryName) as $filename) {
73-
$attributes = [
74-
'rel' => 'stylesheet',
75-
'href' => $this->getAssetPath($filename, $packageName),
76-
];
76+
$attributes = $this->defaultAttributes;
77+
$attributes['rel'] = 'stylesheet';
78+
$attributes['href'] = $this->getAssetPath($filename, $packageName);
7779

7880
if (isset($integrityHashes[$filename])) {
7981
$attributes['integrity'] = $integrityHashes[$filename];

src/DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function getConfigTreeBuilder()
3434
->isRequired()
3535
->info('The path where Encore is building the assets - i.e. Encore.setOutputPath()')
3636
->end()
37+
->enumNode('crossorigin')
38+
->defaultFalse()
39+
->values([false, 'anonymous', 'use-credentials'])
40+
->info('crossorigin value when Encore.enableIntegrityHashes() is used, can be false (default), anonymous or use-credentials')
41+
->end()
3742
->booleanNode('cache')
3843
->info('Enable caching of the entry point file(s)')
3944
->defaultFalse()

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,18 @@ public function load(array $configs, ContainerBuilder $container)
5050

5151
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
5252
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
53-
5453
if (false !== $config['output_path']) {
5554
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
5655
}
56+
57+
$defaultAttributes = [];
58+
59+
if (false !== $config['crossorigin']) {
60+
$defaultAttributes['crossorigin'] = $config['crossorigin'];
61+
}
62+
63+
$container->getDefinition('webpack_encore.tag_renderer')
64+
->replaceArgument(2, $defaultAttributes);
5765
}
5866

5967
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled): Reference

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<service id="webpack_encore.tag_renderer" class="Symfony\WebpackEncoreBundle\Asset\TagRenderer">
1717
<argument type="service" id="webpack_encore.entrypoint_lookup_collection" />
1818
<argument type="service" id="assets.packages" />
19+
<argument type="collection" />
1920
</service>
2021

2122
<service id="webpack_encore.twig_entry_files_extension" class="Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension">

tests/Asset/TagRendererTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class TagRendererTest extends TestCase
2020
{
21-
public function testRenderScriptTags()
21+
public function testRenderScriptTagsWithDefaultAttributes()
2222
{
2323
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
2424
$entrypointLookup->expects($this->once())
@@ -40,7 +40,7 @@ public function testRenderScriptTags()
4040
->willReturnCallback(function ($path) {
4141
return 'http://localhost:8080'.$path;
4242
});
43-
$renderer = new TagRenderer($entrypointCollection, $packages);
43+
$renderer = new TagRenderer($entrypointCollection, $packages, []);
4444

4545
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
4646
$this->assertContains(
@@ -71,11 +71,11 @@ public function testRenderScriptTagsWithBadFilename()
7171
->willReturnCallback(function ($path) {
7272
return 'http://localhost:8080'.$path;
7373
});
74-
$renderer = new TagRenderer($entrypointCollection, $packages);
74+
$renderer = new TagRenderer($entrypointCollection, $packages, ['crossorigin'=>'anonymous']);
7575

7676
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
7777
$this->assertContains(
78-
'<script src="http://localhost:8080/build/file&lt;&quot;bad_chars.js"></script>',
78+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file&lt;&quot;bad_chars.js"></script>',
7979
$output
8080
);
8181
}
@@ -117,21 +117,21 @@ public function testRenderScriptTagsWithinAnEntryPointCollection()
117117
->willReturnCallback(function ($path) {
118118
return 'http://localhost:8080'.$path;
119119
});
120-
$renderer = new TagRenderer($entrypointCollection, $packages);
120+
$renderer = new TagRenderer($entrypointCollection, $packages, ['crossorigin'=>'anonymous']);
121121

122122
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
123123
$this->assertContains(
124-
'<script src="http://localhost:8080/build/file1.js"></script>',
124+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file1.js"></script>',
125125
$output
126126
);
127127
$output = $renderer->renderWebpackScriptTags('my_entry', null, 'second');
128128
$this->assertContains(
129-
'<script src="http://localhost:8080/build/file2.js"></script>',
129+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file2.js"></script>',
130130
$output
131131
);
132132
$output = $renderer->renderWebpackScriptTags('my_entry', 'specific_package', 'third');
133133
$this->assertContains(
134-
'<script src="http://localhost:8080/build/file3.js"></script>',
134+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file3.js"></script>',
135135
$output
136136
);
137137
}
@@ -167,15 +167,15 @@ public function testRenderScriptTagsWithHashes()
167167
->willReturnCallback(function ($path) {
168168
return 'http://localhost:8080'.$path;
169169
});
170-
$renderer = new TagRenderer($entrypointCollection, $packages, true);
170+
$renderer = new TagRenderer($entrypointCollection, $packages, ['crossorigin'=>'anonymous']);
171171

172172
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
173173
$this->assertContains(
174-
'<script src="http://localhost:8080/build/file1.js" integrity="sha384-Q86c+opr0lBUPWN28BLJFqmLhho+9ZcJpXHorQvX6mYDWJ24RQcdDarXFQYN8HLc"></script>',
174+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file1.js" integrity="sha384-Q86c+opr0lBUPWN28BLJFqmLhho+9ZcJpXHorQvX6mYDWJ24RQcdDarXFQYN8HLc"></script>',
175175
$output
176176
);
177177
$this->assertContains(
178-
'<script src="http://localhost:8080/build/file2.js" integrity="sha384-ymG7OyjISWrOpH9jsGvajKMDEOP/mKJq8bHC0XdjQA6P8sg2nu+2RLQxcNNwE/3J"></script>',
178+
'<script crossorigin="anonymous" src="http://localhost:8080/build/file2.js" integrity="sha384-ymG7OyjISWrOpH9jsGvajKMDEOP/mKJq8bHC0XdjQA6P8sg2nu+2RLQxcNNwE/3J"></script>',
179179
$output
180180
);
181181
}

tests/IntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
154154
$container->loadFromExtension('webpack_encore', [
155155
'output_path' => __DIR__.'/fixtures/build',
156156
'cache' => true,
157+
'crossorigin' => false,
157158
'builds' => [
158159
'different_build' => __DIR__.'/fixtures/different_build',
159160
],

0 commit comments

Comments
 (0)