Skip to content

Commit cc5af63

Browse files
committed
Attempting to enable allowing CORS requests on AMP pages as well, to close Issue splashlab#10. You can enable the AMP-Access-Control-Allow-Source-Origin header in the config now.
1 parent ab4f2db commit cc5af63

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Plugin/CorsHeadersPlugin.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ protected function getAllowCredentials()
6161
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
6262
}
6363

64+
/**
65+
* Get the origin domain the requests are going to come from
66+
* @return string
67+
*/
68+
protected function getEnableAmp()
69+
{
70+
return (bool) $this->scopeConfig->getValue('web/corsRequests/enable_amp',
71+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
72+
}
73+
6474
/**
6575
* Triggers before original dispatch
6676
* This method triggers before original \Magento\Webapi\Controller\Rest::dispatch and set version
@@ -79,6 +89,9 @@ public function beforeDispatch(
7989
if ($this->getAllowCredentials()) {
8090
$this->response->setHeader('Access-Control-Allow-Credentials', 'true', true);
8191
}
92+
if ($this->getEnableAmp()) {
93+
$this->response->setHeader('AMP-Access-Control-Allow-Source-Origin', rtrim($originUrl,"/"), true);
94+
}
8295
}
8396
}
8497

etc/adminhtml/system.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
2020
<comment>Enables Access-Control-Allow-Credentials response header to pass cookies</comment>
2121
</field>
22+
<field id="enable_amp" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1"
23+
showInStore="1">
24+
<label>CORS Requests for AMP</label>
25+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
26+
<comment>Enables AMP-Access-Control-Allow-Source-Origin response header for AMP CORS requests</comment>
27+
</field>
2228
</group>
2329
</section>
2430
</system>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<corsRequests>
77
<origin_url>*</origin_url>
88
<allow_credentials>0</allow_credentials>
9+
<enable_amp>0</enable_amp>
910
</corsRequests>
1011
</web>
1112
</default>

0 commit comments

Comments
 (0)