/
optimizing-responses.md
101 lines (63 loc) · 2.72 KB
/
optimizing-responses.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Optimizing responses
====================
Servers may inspect [request headers](/up.protocol) to customize or shorten responses,
e.g. by [omitting content that isn't targeted](#omitting-content-that-isnt-targeted)
or by [rendering different content for overlays](#rendering-different-content-for-overlays).
## Omitting content that isn't targeted
Unpoly transmits the [targeted selector](/targeting-fragments) in an `X-Up-Target` request header.
Server-side code may shorten its response by only rendering HTML
that matches the target selector. For example, you might prefer to not render an
expensive sidebar if the sidebar is not targeted.
Doing this is **fully optional**. The server is free to send redundant elements or full HTML documents.
Only the [targeted fragment](/targeting-fragments) will be updated on the page.
Other elements from the response will be discarded.
### Example
The user makes a request to `/sitemap` in order to updates a fragment `.menu`.
Unpoly makes a request like this:
```http
GET /sitemap HTTP/1.1
X-Up-Target: .menu
```
The server may choose to [optimize its response](/optimizing-responses) by only render only the HTML for
the `.menu` fragment. It responds with the following HTTP:
```http
Vary: X-Up-Target
<div class="menu">...</div>
```
@include vary-header-note
## Rendering different content for overlays
This request header contains the targeted layer's [mode](/up.layer.mode) in an `X-Up-Mode` request header.
Server-side code is free to render different HTML for different modes.
For example, you might prefer to not render a site navigation for overlays.
```http
X-Up-Mode: drawer
X-Up-Target: main
```
The server chooses to render only the HTML required for the overlay.
It responds with the following HTTP:
```http
Vary: X-Up-Mode
<main>...</main>
```
## Rendering different content for Unpoly requests
When Unpoly updates a fragment, it always includes an `X-Up-Version` header.
Server-side code may check for the presence of an `X-Up-Version` header to
distinguish [fragment updates](/up.link) from full page loads.
### Example
The user updates a fragment. Unpoly automatically includes the following request header:
```http
X-Up-Version: 1.0.0
```
The server chooses to render different HTML to Unpoly requests, e.g. by excluding the document `<head>`
and only rendering the `<body>`. The server responds with the folowing HTTP:
```http
Vary: X-Up-Version
<body>
...
</body>
```
## Rendering content that depends on layer context
[Layer context](/context) is an object that exists for the lifetime of a layer.
You may use this to [re-use existing interactions in an overlay](/context#reuse-interaction-with-variation),
bit with a variation like a different page title.
@page optimizing-responses