You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> _This is still in beta stages, use it with care and expect some changes to be shipped before official release. Tests are still being worked on and will be pushed at v1, sit tight._
1
+
> _This is still in beta stages, use it with care and expect some changes to be shipped before official release. Tests are still being worked on and will be pushed at official v1, sit tight._
2
2
3
3
## @brixtol/pjax
4
4
5
-
A blazing fast, lightweight (8kb gzipped), feature full drop-in next generation pjax solution for SSR web applications. Supports multiple fragment replacements, appends and prepends. Pre-fetching capabilities via mouse, pointer, touch and intersection events and snapshot caching which prevent subsequent requests for occurring that results in instantaneous navigation.
5
+
A blazing fast, lightweight (8kb gzipped), feature full drop-in new generation pjax solution for SSR web applications. This Pjax variation supports multiple fragment replacements, it ships with advanced pre-fetching capabilities executing via mouse/pointer/touch or intersection events and provides a snapshot caching feature which prevents subsequent requests for occurring resulting in instantaneous page navigation.
6
6
7
-
##### Example
7
+
##### Demo
8
8
9
9
We are using this module live on our [webshop](https://brixtoltextiles.com).
10
10
11
+
### Why?
12
+
13
+
The landscape of pjax based solution has become rather scarce. The current bread winners either offer the same thing or for our use cases were vastly over engineered. We wanted to couple together various techniques we found to be the most effective in enhancing the performance of SSR rendered web application.
14
+
11
15
## Install
12
16
13
17
```cli
@@ -18,14 +22,16 @@ pnpm i @brixtol/pjax
18
22
19
23
## Usage
20
24
21
-
To initialize, call `Pjax.connect()` in your bundle and optionally pass preset configuration. By default Pjax will replace the entire `<body>` fragment upon each navigation. You should define a set of `targets[]` whose inner contents change on a per-page basis.
25
+
To initialize, call `Pjax.connect()` in your bundle and optionally pass preset configuration. By default, Pjax will replace the entire `<body>` fragment upon each navigation, so you should define a set of `targets[]` whose inner contents change on a per-page basis.
26
+
27
+
> The typings provided in this package will describe each option in good detail
22
28
23
29
<!-- prettier-ignore -->
24
30
```js
25
31
import*asPjaxfrom"@brixtol/pjax";
26
32
27
33
Pjax.connect({
28
-
targets: ["body"],
34
+
targets: ["body"],// Define fragments to be replaced here!
29
35
cache: {
30
36
enable:true,
31
37
limit:25,
@@ -38,7 +44,6 @@ Pjax.connect({
38
44
mouseover: {
39
45
enable:true,
40
46
threshold:100,
41
-
proximity:0,
42
47
},
43
48
intersect: {
44
49
enable:true,
@@ -64,6 +69,238 @@ Pjax.connect({
64
69
65
70
```
66
71
72
+
#### Real World
73
+
74
+
Below is a real world example you can use to better understand how this module works so you can apply it into your web application. We are working on providing a live demonstration for more advanced use cases, but the below example should give you a good understanding and help you in understanding how to leverage the module.
75
+
76
+
<details>
77
+
<summary>
78
+
Example
79
+
</summary>
80
+
81
+
The first thing we want to do is make a connection with Pjax. In your JavaScript bundle, we need to initialize. Our example web application has 3 pages, the home page, about page and contact page. We are going to instruct pjax to replace the `<nav>` and `<main>` fragments on every visit and then we are going to leverage `data-pjax` attributes to replace an additional fragment when we navigate to the contact page.
82
+
83
+
<br>
84
+
**JavaScript Bundle**
85
+
<br>
86
+
87
+
<!-- prettier-ignore -->
88
+
```javascript
89
+
import*asPjaxfrom"@brixtol/pjax";
90
+
91
+
exportdefault () => {
92
+
93
+
Pjax.connect({
94
+
targets: [
95
+
"nav",
96
+
"main"
97
+
],
98
+
})
99
+
100
+
}
101
+
102
+
```
103
+
104
+
**Home Page**
105
+
<br>
106
+
Below we have a very basic Home Page with pjax wired up and all `<a>` elements will be intercepted and cached. SSR web application (in most cases) will only ever have a couple of fragments that change between navigation, so keeping to that logic lets begin..
107
+
108
+
<!-- prettier-ignore -->
109
+
```html
110
+
111
+
<!doctype html>
112
+
<html>
113
+
<head>
114
+
<metacharset="UTF-8">
115
+
<title>Home Page</title>
116
+
<scriptsrc="/bundle.js"></script>
117
+
</head>
118
+
<body>
119
+
120
+
<header>
121
+
122
+
<h1>@brixtol/pjax</h1>
123
+
124
+
<!-- THIS FRAGMENT WILL BE REPLACED -->
125
+
<nav>
126
+
127
+
<!-- This link will be intercepted -->
128
+
<a
129
+
href="/home"
130
+
class="active">Home</a>
131
+
132
+
<!-- These links will be pe-fetched on hover -->
133
+
<a
134
+
href="/about"
135
+
data-pjax-prefetch="hover">About</a>
136
+
137
+
<!-- This link will replace the #foo fragment -->
138
+
<a
139
+
href="/contact"
140
+
data-pjax-replace="(['#foo'])"
141
+
data-pjax-prefetch="hover">Faq</a>
142
+
</nav>
143
+
144
+
</header>
145
+
146
+
<!-- THIS FRAGMENT WILL BE REPLACED -->
147
+
<main>
148
+
149
+
<h1>Welcome to the home page</h1>
150
+
151
+
<divclass="container">
152
+
Brixtol Textiles is a Swedish apparel brand!
153
+
</div>
154
+
155
+
</main>
156
+
157
+
<divid="foo">
158
+
This fragment will not be touched until /contact is clicked
159
+
</div>
160
+
161
+
<footer>
162
+
This will not be touched during navigation
163
+
</footer>
164
+
165
+
</body>
166
+
</html>
167
+
168
+
```
169
+
170
+
**About Page**
171
+
<br>
172
+
The about page in our web application would look practically identical to the home page. We instructed pjax to pre-fetch this page upon hover, so navigating to this page will be instantaneous. The about page only has some minor differences, but for the sake of clarity, lets have look:
173
+
174
+
<!-- prettier-ignore -->
175
+
```html
176
+
177
+
<!doctype html>
178
+
<html>
179
+
<head>
180
+
<metacharset="UTF-8">
181
+
<title>About Page</title>
182
+
<scriptsrc="/bundle.js"></script>
183
+
</head>
184
+
<body>
185
+
186
+
<header>
187
+
188
+
<h1>@brixtol/pjax</h1>
189
+
190
+
<!-- THIS FRAGMENT WILL BE REPLACED -->
191
+
<nav>
192
+
193
+
<!-- This link will be intercepted -->
194
+
<a
195
+
href="/home">Home</a>
196
+
197
+
<!-- These links will be pe-fetched on hover -->
198
+
<a
199
+
href="/about"
200
+
class="active"
201
+
data-pjax-prefetch="hover">About</a>
202
+
203
+
<!-- This link will replace the #foo fragment -->
204
+
<a
205
+
href="/contact"
206
+
data-pjax-replace="(['#foo'])"
207
+
data-pjax-prefetch="hover">Contact</a>
208
+
</nav>
209
+
210
+
</header>
211
+
212
+
<!-- THIS FRAGMENT WILL BE REPLACED -->
213
+
<main>
214
+
215
+
<h1>Welcome to the About Page</h1>
216
+
217
+
<divclass="container">
218
+
Brixtol Textiles makes jackets out of recycled PET bottles.
219
+
<p>Producing clothing in a sustainable way is the future!</p>
220
+
</div>
221
+
222
+
</main>
223
+
224
+
<divid="foo">
225
+
This fragment will not be touched until /contact is clicked
226
+
</div>
227
+
228
+
<footer>
229
+
This will not be touched during navigation
230
+
</footer>
231
+
232
+
</body>
233
+
</html>
234
+
```
235
+
236
+
**Contact Page**
237
+
<br>
238
+
The contact page will replace an additional fragment with the id value of `foo` which we instructed via attribute annotation. When the contact page link is hovered the page will be saved to cache, upon visit the `<nav>`, `<main>` and `<div id="foo">` fragments will be replaced.
239
+
240
+
<!-- prettier-ignore -->
241
+
```html
242
+
243
+
<!doctype html>
244
+
<html>
245
+
<head>
246
+
<metacharset="UTF-8">
247
+
<title>Contact Page</title>
248
+
<scriptsrc="/bundle.js"></script>
249
+
</head>
250
+
<body>
251
+
252
+
<header>
253
+
254
+
<h1>@brixtol/pjax</h1>
255
+
256
+
<!-- THIS FRAGMENT WILL BE REPLACED -->
257
+
<nav>
258
+
259
+
<!-- This link will be intercepted -->
260
+
<a
261
+
href="/home">Home</a>
262
+
263
+
<!-- These links will be pe-fetched on hover -->
264
+
<a
265
+
href="/about"
266
+
data-pjax-prefetch="hover">About</a>
267
+
268
+
<!-- This link will replace the #foo fragment -->
269
+
<a
270
+
href="/contact"
271
+
class="active"
272
+
data-pjax-replace="(['#foo'])"
273
+
data-pjax-prefetch="hover">Contact</a>
274
+
</nav>
275
+
276
+
</header>
277
+
278
+
<!-- THIS FRAGMENT WILL BE REPLACED -->
279
+
<main>
280
+
281
+
<h1>Welcome to the Contact Page</h1>
282
+
283
+
<divclass="container">
284
+
This is contact page of our example! The below fragment was replaced too!
285
+
</div>
286
+
287
+
</main>
288
+
289
+
<!-- THIS FRAGMENT WAS REPLACE VIA ATTRIBUTE INSTRUCTION -->
290
+
<divid="foo">
291
+
This fragment was replaced!
292
+
</div>
293
+
294
+
<footer>
295
+
This will not be touched during navigation
296
+
</footer>
297
+
298
+
</body>
299
+
</html>
300
+
```
301
+
302
+
</details>
303
+
67
304
## Lifecycle Events
68
305
69
306
Lifecycle events are dispatched to the document upon each navigation. You can access context information from within `event.detail` or cancel events with `preventDefault()` and prevent execution.
In addition to Lifecycle events, a list of methods are available. Methods will allow you some basic programmatic control of the Pjax session.
332
+
In addition to Lifecycle events, a list of methods are available. Methods will allow you some basic programmatic control of a Pjax session.
96
333
97
334
```javascript
98
335
@@ -415,7 +652,7 @@ Example
415
652
416
653
## State
417
654
418
-
Each page has an object state value. Page state is immutable and created for every unique url `/path` or `/pathname?query=param` value encountered throughout a pjax navigation session. The state value of each page is added to its pertaining History stack record.
655
+
Each page has an object state value. Page state is immutable and created for every unique url `/path` or `/pathname?query=param` value encountered throughout a pjax session. The state value of each page is added to its pertaining History stack record.
419
656
420
657
> Navigation sessions begin once a Pjax connection has been established and ends when a browser refresh is executed or url origin changes.
421
658
@@ -582,9 +819,9 @@ interface IPage {
582
819
583
820
## Contributing
584
821
585
-
This module is written in ES2020 format JavaScript. Production bundles export in ES6 format. Legacy support is provided as an ES5 UMD bundle. This project leverages JSDocs and Type Definition files for its type checking, so all features you enjoy with TypeScript are available.
822
+
This module is written in ES2020 JavaScript. Production bundles export in ES6 format. Legacy support is provided as an ES5 UMD bundle. This project leverages JSDocs and Type Definition files for its type checking, so all features you enjoy with TypeScript are available.
586
823
587
-
This module is consumed by us for a couple of our projects, we will update it according to what we need. Feel free to suggest features or report bugs, PR's are welcome too!
824
+
This module is consumed by us for a couple of our projects and has been open sourced but exists as part of a mono/multi repo. We will update it according to what we need. Feel free to suggest features or report bugs, PR's are of course welcome!
0 commit comments