Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit a75a7a9

Browse files
author
Steve Johnson
committed
MAGEDOC-1020. Ready for spell check and final review
1 parent 808a52b commit a75a7a9

File tree

4 files changed

+70
-49
lines changed

4 files changed

+70
-49
lines changed

guides/v2.0/config-guide/cache/cache-priv-cacheable.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,5 @@ To specify uncacheable pages, add `cacheable="false"` to the block definition in
5858

5959
[Example]({{ site.mage2000url }}app/code/Magento/Paypal/view/frontend/layout/paypal_payflow_returnurl.xml){:target="_blank"}
6060

61-
{% endhighlight %}
62-
6361
#### Next
6462
[Public and private content]({{ page.baseurl }}config-guide/cache/cache-priv-priv.html)

guides/v2.0/config-guide/cache/cache-priv-context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Context variables are used by the Magento application to render different conten
1919

2020
Context variables must not be specific to exactly one user, because variables are used in cache keys for public content. In other words, a context variable per user results in a separate copy of content for each user cached on the server.
2121

22-
Magento combines context variables in a string, generates the cache from that string, and sets it as the value of the `X-Magento-Vary` cookie. HTTP proxies can be configured to calculate a unique identifier for cache based on the cookie and URL. For example, [our sample Varnish 4 configuration]({{ site.mage2000url }}app/code/Magento/PageCache/etc/varnish4.vcl){:target="_blank"} uses the following:
22+
Magento combines context variables in a string, generates the cache from that string, and sets it as the value of the `X-Magento-Vary` cookie. HTTP proxies can be configured to calculate a unique identifier for cache based on the cookie and URL. For example, [our sample Varnish 4 configuration]({{ site.mage2000url }}app/code/Magento/PageCache/etc/varnish4.vcl#L63-L68){:target="_blank"} uses the following:
2323

2424
{% highlight xml %}
2525
sub vcl_hash {

guides/v2.0/config-guide/cache/cache-priv-over.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ For content to be cacheable, it must meet the following criteria:
5959
* Must use only the HTTP [GET or HEAD]({{ page.baseurl }}config-guide/cache/cache-priv-cacheable.html#config-page-cache) request method
6060
* Must render only cacheable blocks (which is the default behavior)
6161
* Contains no sensitive or private data (in other words, session and customer [Data Transfer Objects (DTO)](https://en.wikipedia.org/wiki/Data_transfer_object){:target="_blank"} objects are empty)
62-
* Current session (customer) and pages should be written using JavaScript. For example, a related product listing should exclude items that are already in the shopping cart.
63-
* All private content blocks must be marked as private using the [`_isScopePrivate` attribute]({{ page.baseurl }}config-guide/cache/cache-priv-priv.html#config-cache-priv-how)
62+
* Implement all customer-specific information using private blocks
6463
* Models and blocks should identify themselves for [invalidation support]({{ page.baseurl }}config-guide/cache/cache-priv-inval.html)
6564
* To show different public content on the same URL based on custom parameters, you should declare a custom [HTTP context variable]({{ page.baseurl }}config-guide/cache/cache-priv-context.html) if you plan to show different public content on the same URL
6665

guides/v2.0/config-guide/cache/cache-priv-priv.md

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,67 +28,91 @@ Many Magento pages contain personal or sensitive information that should be deli
2828
Examples of private content include the wishlist, shopping cart, customer name, and addresses. Private content should be limited to a small portion of the total content on a page.
2929

3030
## Specify private content {#config-cache-priv-how}
31-
To specify a block as private, use `$isScopePrivate` as follows:
32-
33-
{% highlight php startinline=true %}
34-
namespace Magento\Wishlist\Block;
35-
class Link
36-
{
37-
/**
38-
* One way to specify isScopePrivate
39-
*/
40-
protected $_isScopePrivate = true;
41-
42-
public function __construct()
43-
{
44-
/** Another way to do the same thing */
45-
$this->_isScopePrivate = true;
46-
}
47-
}
48-
{% endhighlight %}
31+
To specify a block as private and have the Magento application render it in browser, do the following:
32+
33+
* [Step 1: Create a section source](#config-cache-priv-how-source)
34+
* [Step 2: Create a block and template](#config-cache-priv-how-block)
35+
* [Step 3: Configure a UI component](#config-cache-priv-how-ui)
36+
* [Step 4: Invalidate private content](#config-cache-priv-how-inval)
37+
38+
### Step 1: Create a section source {#config-cache-priv-how-source}
39+
The section source class is responsible for retrieving data for the section. As a best practice, we recommend you implement your code using the `Vendor/ModuleName/CustomerData` namespace. Your classes must implement the [`Magento\Customer\Model\CustomerData\SectionSourceInterface`]({{ site.mage2000url }}app/code/Magento/Customer/CustomerData/SectionSourceInterface.php){:target="_blank"} interface.
40+
41+
The public method `getSectionData` must return an associative array with data for private block.
4942

50-
Magento uses JavaScript to work with private content as follows:
43+
[Example]({{ site.mage2000url }}app/code/Magento/Catalog/CustomerData/CompareProducts.php#L36-L45){:target="_blank"}
5144

52-
1. Magento substitutes private content for variables in the HTTP response
53-
2. JavaScript collects these variables in the browser and requests private content from the server
54-
3. The response with private content is cached in the browser until one of the following happens:
45+
Add the following to your component's dependency injection configuration (`di.xml`):
5546

56-
* The browser sends an HTTP POST request to Magento (for example, to add more items to the cart)
57-
* The user clears their browser's cache
58-
4. JavaScript replaces the variables in the HTML with content received from the preceding step
47+
{% highlight xml %}
48+
<type name="Magento\Customer\CustomerData\SectionPoolInterface">
49+
<arguments>
50+
<argument name="sectionSourceMap" xsi:type="array">
51+
<item name="compare-products" xsi:type="string">Magento\Catalog\CustomerData\CompareProducts</item>
52+
</argument>
53+
</arguments>
54+
</type>
55+
{% endhighlight %}
5956

60-
Using JavaScript has the following advantages over ESI, which uses an include URL for each piece of private content:
57+
### Step 2: Create a block and template {#config-cache-priv-how-block}
58+
To render private content, create a *template* to display user-agnostic information and *blocks* to contain private content.
6159

62-
* A single AJAX call can fetch all the private user content on a page instead of one request per part of a page to be replaced (as is done with ESI).
60+
The user-agnostic data will be replaced with user specific data by the UI component.
6361

64-
This reduces the number of HTTP requests.
65-
* The private content is cacheable by the web browser.
62+
<div class="bs-callout bs-callout-info" id="info">
63+
<p>Do <em>not</em> use the <code>$_isScopePrivate</code> property in your blocks. This property is obsolete and won't work properly.</p>
64+
</div>
6665

67-
A customer’s name is not likely to change for example, so why not keep it in the web browser cache and avoid future AJAX calls?
66+
Replace private data in blocks with placeholders (using [Knockout JavaScript](http://knockoutjs.com/documentation/introduction.html){:target="_blank"} syntax). The init scope on the root element is `data-bind="scope: 'compareProducts'"`, where you define the scope name (`compareProducts` in this example)in your layout.
6867

69-
Following is an example of JavaScript that displays the customer's full name or a default message if the customer's full name is not known:
68+
Initialize the component as follows:
7069

7170
{% highlight javascript %}
72-
73-
<!-- ko if: customer().fullname -->
74-
<span data-bind="text: new String('Welcome, %1!').replace('%1', customer().firstname)">
75-
</span>
76-
<!-- /ko -->
77-
<!-- ko ifnot: customer().fullname -->
78-
<span data-bind="html:'Default welcome msg!'"></span>
79-
<!-- /ko -->
71+
<script type="text/x-magento-init">
72+
{"<css-selector>": {"Magento_Ui/js/core/app": <?php echo $block->getJsLayout();?>}}
73+
</script>
74+
{% endhighlight %}
75+
76+
[Example]({{ site.mage2000url }}app/code/Magento/Catalog/view/frontend/templates/product/compare/sidebar.phtml){:target="_blank"}
77+
78+
### Step 3: Configure a UI component {#config-cache-priv-how-ui}
79+
The UI component renders block data on the Magento storefront. To initialize the UI component, you must call the initialization method `_super()`. [Example]({{ site.mage2000url }}app/code/Magento_Catalog/view/frontend/web/js/view/compare-products.js){:target="_blank"}
80+
81+
All properties are available in the template.
82+
83+
[Example of defining a UI component in a layout]({{ site.mage2000url }}app/code/Magento/Catalog/view/frontend/layout/default.xml#L11-L22){:target="_blank"}
84+
85+
### Step 4: Invalidate private content {#config-cache-priv-how-inval}
86+
Specify actions that trigger cache invalidation for private content blocks in a `sections.xml` configuration file in the `Vendor/ModuleName/etc/frontend` directory. Cache is invalidated on a POST or PUT request, such as a customer adding items to a cart.
87+
88+
The following example adds comments to [app/code/Magento/Catalog/etc/frontend/sections.xml]({{ site.mage2000url }}app/code/Magento/Catalog/etc/frontend/sections.xml){:target="_blank"} to show you what happens.
89+
90+
{% highlight xml %}
91+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Customer/etc/sections.xsd">
92+
93+
<!-- will invalidate "compare-products" section, when user perform "catalog/product_compare/add" POST request -->
94+
<action name="catalog/product_compare/add">
95+
<section name="compare-products"/>
96+
</action>
97+
98+
<!-- will invalidate all sections -->
99+
<action name="customer/account/logout"/>
100+
101+
<!-- will invalidate section for each POST request -->
102+
<action name="*">
103+
<section name="message"/>
104+
</action>
105+
106+
</config>
80107
{% endhighlight %}
81108

82109
<div class="bs-callout bs-callout-info" id="info">
83110
<ul><li>Use only HTTP <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5" target="_blank">POST</a> or <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6" target="_blank">PUT</a> methods to change state (for example, adding to a shopping cart, adding to a wishlist, and so on.) POST and PUT requests are <em>not</em> cached.</li>
84-
<li>Only HTTP <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3" target="_blank">GET</a> and <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4" target="_blank">HEAD</a> requests are cacheable.</li>
85-
<li>For more information about caching, see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html" target="_blank">RFC-2616 section 13</a>.</li>
111+
<li>Only HTTP <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3" target="_blank">GET</a> and <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4" target="_blank">HEAD</a> requests are cacheable.</li>
112+
<li>For more information about caching, see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html" target="_blank">RFC-2616 section 13</a>.</li>
86113

87114
</ul>
88115
</div>
89116

90-
## Considerations for public content {#config-cache-public}
91-
@@@
92-
93117
#### Next
94118
[HTTP context]({{ page.baseurl }}config-guide/cache/cache-priv-context.html)

0 commit comments

Comments
 (0)